My Animations are delayed, is this because I am accessing them through a script?

I’m having a problem with my animation transitions, and I think I might not be using the animator correctly. My animations do not play immediately on the button press, but are delayed for about a second. In order to change between animations I’m accessing the animator through a C# script. What I’m doing is using GetComponent to access animator. I’m then setting a Bool for each different animation in the script, and making the same bool parameter in the animator then setting that bool condition to true on the transition. So for example when the right arrow key is pressed then the bool “IsWalkingRight” is true in the script, so it then finds the “IsWalkingRight” condition in the animator and plays the walking right animation, which it is transitioning to. This works but the problem is that there’s a very noticeable delay between when the button is pressed and when the animation changes. I’m sure I’m missing something very simple but I don’t know what, it can’t be this complicated to set up animation.

Here’s the script I’m using to access the animator:

public class AnimateOnCol : MonoBehaviour

{

Animator animator;

public bool Grounded = true;

public bool isWalkingRight = false;

public bool isLeft = false;

public bool isWalkingLeft = false;

public bool isCrouchingRight = false;

public bool isCrouchingLeft = false;


void Start()
{
    animator = GetComponent<Animator>();
}

void Update()
{
    animator.SetBool("Grounded", Grounded);

    if (Input.GetKey(KeyCode.RightArrow))
    {
        isWalkingRight = true;
        Debug.Log(" walking is true");
        isLeft = false;
        
    }
    else
    {
        isWalkingRight = false;
    }
    animator.SetBool("isWalkingRight", isWalkingRight);

    ///

    if (Input.GetKey(KeyCode.LeftArrow))
    {
        isLeft = true;
        
    }
    
    animator.SetBool("isLeft", isLeft);

    ///

    if (Input.GetKey(KeyCode.LeftArrow))
    {
        isWalkingLeft = true;
        
    }
    else
    {
        isWalkingLeft = false;
    }

    animator.SetBool("isWalkingLeft", isWalkingLeft);

    ///

    if (Input.GetKey(KeyCode.DownArrow))
    {
        isCrouchingRight = true;

    }
    else
    {
        isCrouchingRight = false;
    }

    animator.SetBool("isCrouchingRight", isCrouchingRight);

    ///

    if (Input.GetKey(KeyCode.DownArrow))
    {
        isCrouchingLeft = true;

    }
    else
    {
        isCrouchingLeft = false;
    }

    animator.SetBool("isCrouchingLeft", isCrouchingLeft);

    ///
}

public void OnCollisionEnter2D(Collision2D col)
{
    Debug.Log("enter function running");
    if(col.gameObject.tag == "Ground")
    {
        Grounded = true;
        Debug.Log(" collsion detected");
        Debug.Log("Grounded is " + Grounded);
    }

}

public void OnCollisionExit2D(Collision2D col)
{
    Debug.Log("exit function running");
    if (col.gameObject.tag == "Ground")
    {
        Grounded = false;
        Debug.Log("Grounded is " + Grounded);
    }

}

}

There could be a bit of dead time in the animation itself that just doesn’t move anything for a brief while but is still actually playing. These sorts of “dead” spaces can be trimmed off in the animation editor.

Also, setting values in the animator can be kind of slowish. Animator.setValue/getValue slow? - Questions & Answers - Unity Discussions

And you’re doing it more often than necessary.