Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(Animator))]
- public class Player : MonoBehaviour
- {
- Animator anim;
- private Rigidbody rb;
- public int speed;
- bool isMovementCalled = false;
- public float decelleration = 1.5f;
- // Start is called before the first frame update
- void Start()
- {
- anim = GetComponent<Animator>();
- rb = GetComponent <Rigidbody>();
- }
- private void FixedUpdate()
- {
- Movement();
- }
- private void Movement()
- {
- //calling our floats
- float moveHorizontal = Input.GetAxis("Horizontal");
- float moveVertical = Input.GetAxis("Vertical");
- //tell us which way we're plannig to move
- Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
- //apply force to start moving
- rb.AddForce(movement * speed);
- rb.AddRelativeForce(-rb.velocity * decelleration);
- if (rb.velocity.magnitude >= 0)
- {
- // Player isn't moving
- isMovementCalled = false;
- }
- }
- // Update is called once per frame
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.W) == true || Input.GetKeyDown(KeyCode.S) == true || Input.GetKeyDown(KeyCode.A) == true || Input.GetKeyDown(KeyCode.D) == true)
- {
- isMovementCalled = true;
- }
- if (isMovementCalled == true)
- {
- anim.SetBool("Walking", true);
- anim.SetBool("Idle", false);
- }
- if (isMovementCalled == false)
- {
- anim.SetBool("Walking", false);
- anim.SetBool("Idle", true);
- }
- }
- void OnAnimatorMove()
- {
- Animator animator = GetComponent<Animator>();
- if (animator)
- {
- Vector3 newPosition = transform.position;
- newPosition.z += animator.GetFloat("Runspeed") * Time.deltaTime;
- transform.position = newPosition;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement