- Home /
How to disable movement when animation is playing?
I would like to make my player stop from being able to move while playing the animation of pooping. Anyone could tell what i should add to my code?
public class PlayerMovement : MonoBehaviour
{
//VARIABLES
[SerializeField] private float moveSpeed;
[SerializeField] private float walkSpeed;
[SerializeField] private float runSpeed;
[SerializeField] private float rotationSpeed;
private Vector3 moveDirection;
private Vector3 velocity;
[SerializeField] private bool isGrounded;
[SerializeField] private float groundCheckDistance;
[SerializeField] private LayerMask groundMask;
[SerializeField] private float gravity;
[SerializeField] private float jumpHeight;
//REFERENCIES
private CharacterController controller;
private Animator anim;
private void Start()
{
controller = GetComponent<CharacterController>();
anim = GetComponentInChildren<Animator>();
}
private void Update()
{
Move();
//POOP
if(Input.GetButtonDown("Poopkey"))
{
anim.Play("Poop");
}
}
//GRAVITATION
private void Move()
{
isGrounded = Physics.CheckSphere(transform.position, groundCheckDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float moveZ = Input.GetAxis("Vertical");
moveDirection = new Vector3(0, 0, moveZ);
moveDirection = transform.TransformDirection(moveDirection);
//MOVEMENT
if (moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift))
{
Walk();
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(0, -rotationSpeed * Time.deltaTime, 0);
}
if (Input.GetKey(KeyCode.D))
{
transform.Rotate(0, rotationSpeed * Time.deltaTime, 0);
}
}
else if (moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift))
{
Run();
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(0, -rotationSpeed * Time.deltaTime, 0);
}
if (Input.GetKey(KeyCode.D))
{
transform.Rotate(0, rotationSpeed * Time.deltaTime, 0);
}
}
else if (moveDirection == Vector3.zero)
{
Idle();
}
moveDirection *= moveSpeed;
if (Input.GetKey(KeyCode.Space) && isGrounded)
{
Jump();
}
if (velocity.y < 0 && !isGrounded)
{
Fall();
}
controller.Move(moveDirection * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613060402im_/https://answers.unity.com/themes/thub/images/avi.jpg)