© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
4 replies
Kaelen

❔ Animation never stop c#

Hey, I have an animation on my character, and when I'm starting walking, my character my game doesnt stop the animation, here is myy code :

public class PlayerMovement : MonoBehaviour
{
    float speed = 4;
    private CharacterController controller;
    private Vector3 direction;
    private Vector2 move;
    private float gravity = 9.81f;
    public static float velocity;
    private string WALK_ANIMATION = "Walk";
    private Animator anim;
    PlayerInput input;
    private Vector2 currentMovement;
    private bool movementPressed;

    void OnMove(InputValue value){
       move = value.Get<Vector2>();

    }

    void Awake(){
        input = new PlayerInput();
    }

    void handleMovement() {
        input.Player.Move.performed += ctx => {
            currentMovement = ctx.ReadValue<Vector2>();
            movementPressed = currentMovement.x != 0 || currentMovement.y != 0;
        };

        bool isWalking = anim.GetBool(WALK_ANIMATION);
        if(movementPressed && !isWalking){
            anim.SetBool(WALK_ANIMATION, true);
        }
        if(!movementPressed && isWalking){
            anim.SetBool(WALK_ANIMATION, false);
        }

        Debug.Log(anim.GetBool(WALK_ANIMATION));
        Debug.Log(currentMovement);
    }

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
        controller = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        
        if(!controller.isGrounded){
                velocity += gravity * Time.deltaTime;
        } 

       direction = new Vector3(move.x, velocity, move.y);
       controller.Move(-direction * speed * Time.deltaTime);
       handleMovement();

    }

    void OnEnable(){
        input.Player.Enable();
    }

    void OnDisable(){
        input.Player.Disable();
    }


}
public class PlayerMovement : MonoBehaviour
{
    float speed = 4;
    private CharacterController controller;
    private Vector3 direction;
    private Vector2 move;
    private float gravity = 9.81f;
    public static float velocity;
    private string WALK_ANIMATION = "Walk";
    private Animator anim;
    PlayerInput input;
    private Vector2 currentMovement;
    private bool movementPressed;

    void OnMove(InputValue value){
       move = value.Get<Vector2>();

    }

    void Awake(){
        input = new PlayerInput();
    }

    void handleMovement() {
        input.Player.Move.performed += ctx => {
            currentMovement = ctx.ReadValue<Vector2>();
            movementPressed = currentMovement.x != 0 || currentMovement.y != 0;
        };

        bool isWalking = anim.GetBool(WALK_ANIMATION);
        if(movementPressed && !isWalking){
            anim.SetBool(WALK_ANIMATION, true);
        }
        if(!movementPressed && isWalking){
            anim.SetBool(WALK_ANIMATION, false);
        }

        Debug.Log(anim.GetBool(WALK_ANIMATION));
        Debug.Log(currentMovement);
    }

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
        controller = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        
        if(!controller.isGrounded){
                velocity += gravity * Time.deltaTime;
        } 

       direction = new Vector3(move.x, velocity, move.y);
       controller.Move(-direction * speed * Time.deltaTime);
       handleMovement();

    }

    void OnEnable(){
        input.Player.Enable();
    }

    void OnDisable(){
        input.Player.Disable();
    }


}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

✅ C# Issue with animation
C#CC# / help
14mo ago
❔ Stop animation (if and else)
C#CC# / help
3y ago
C# decided to stop working
C#CC# / help
2mo ago
NetMQ: SendFrame never sends, never returns.
C#CC# / help
16mo ago