I’m making my player strafe left and right not just one running animation for moving.
So to do that I want to know how to check if the player is moving left or right to add the animation easily.
public CharacterController controller;
public Transform groundCheck;
public LayerMask groundMask;
public float groundDistance = 0.4f;
public float speed = 12f;
public float gravity = -10f;
public float jumpHeight = 3f;
private Animator animator;
Vector3 velocity;
bool isGrounded;
void Start()
{
animator = GetComponent<Animator>();
}
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
animator.SetBool("isGrounded", true);
animator.SetBool("isJumping", false);
}
else
{
animator.SetBool("isGrounded", false);
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
animator.SetFloat("speed", move.z);
animator.SetFloat("xSpeed", move.x);
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
animator.SetBool("isJumping", true);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}