Animating problems

If I press “w” my character will do a walk animation, but if I let go of it, it will go bag to its idle position. However when I tried running if I let go of the shift key the W key or both it will still continue to do the running animation. Why is this? Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Anim : MonoBehaviour
{
    Animator animator;

   

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

    // Update is called once per frame
    void Update()
    {
        bool isWalking = animator.GetBool("Walking");
        bool isRunning = animator.GetBool("Running");
        bool walking = Input.GetKey("w");
        bool runPressed = Input.GetKey("left shift");
        if (!isWalking && walking)
        {
            animator.SetBool("Walking", true);
        }
        if (isWalking &!walking)
        {
            animator.SetBool("Walking", false);
        }
         if (!isRunning && (walking && runPressed))
        {
            animator.SetBool("Running", true);
        }
        if (!isRunning && (!walking || !runPressed))
        {
            animator.SetBool("Running", false);
        }

    

    }

    
}

1 Answer

1

Your code to set Running to false is checking for !isRunning. I think you probably want this to check for isRunning.

As a side note, it is not a big deal to set an animator bool to the same value. You could simply your code to:

void Update()
{
    bool walking = Input.GetKey("w");
    bool runPressed = Input.GetKey("left shift")
    bool running = walking && runPressed;
    animator.SetBool("Walking", walking);
    animator.SetBool("Running", running);
}