Animation States transitioning too fast

Hey there, in the game concept I’m developing, the player has only two options; dodge or attack. Currently, I’m working on the dodging, where the player will return to their original position after the dodge has occurred. I tried to add animations to this, using an integer to decide which ‘state’ the player is in to determine which animation plays. However, when testing this in play, the animation doesn’t occur. Well, technically, it does happen, but it resets to the 0/Idle state so fast that the only way you notice is that the number shakes. I’ve tried fiddling with the settings to slow down the animation, the transition, ect. But it still persists.

In the spoiler, you’ll find the code.

Sorry in advance if this is obvious.

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

public class CharacterController : MonoBehaviour
{
    public float Speed;
    public Transform Character;
    public bool dodging;
    public Animator animator;
    private Vector3 moveDirection = Vector3.zero;

    public Vector3 jumpVector;

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

    //Dodging state is recorded so that the player will be unable to attack when doing a dodging move
    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Center"))
        {
            dodging = false;
        }
    }

    void OnTriggerExit(Collider other)
    {
        if (other.CompareTag("Center"))
        {
            dodging = true;
        }
    }

    void Update()
    {
        //If chacter presses any dodge keys, they move the same amount each time. The moment the key stops being held down, player returns to the center
        //AnimState to determine animation. 0 = Idle, 1 = DodgeLeft, 2 = DodgeRight, 3 = DodgeDown, 4 = DodgeUp,
        if (Input.GetKeyDown(KeyCode.A))
        {
            transform.position += Vector3.back * Speed;
            animator.SetInteger("AnimState", 1);
            Debug.Log("Button pressed");
        }
        else if (Input.GetKeyUp(KeyCode.A))
        {
            transform.position += Vector3.forward * Speed;
            Debug.Log("Button no longer pressed");
        }
        else if (Input.GetKeyDown(KeyCode.D))
        {
            animator.SetInteger("AnimState", 2);
            transform.position += Vector3.forward * Speed;
            Debug.Log("Button pressed");
        }
        else if (Input.GetKeyUp(KeyCode.D))
        {
            transform.position += Vector3.back * Speed;
            Debug.Log("Button no longer pressed");
        }
        else if (Input.GetKeyUp(KeyCode.S))
        {
            animator.SetInteger("AnimState", 3);
            Debug.Log("Button no longer pressed");
        }
        else if (Input.GetKeyUp(KeyCode.W))
        {
            animator.SetInteger("AnimState", 4);
            GetComponent<Rigidbody>().AddForce(jumpVector, ForceMode.VelocityChange);
            Debug.Log("Button no longer pressed");
        else
        {
            animator.SetInteger("AnimState", 0);
        }
    }
}

GetKeyUp and Down only return true for one frame, so you are always setting it back to 0 on the next frame and causing it to interrupt the transition you just started to transition back to Idle.

If you want to use a setup like that, you need to remove the Any State transition to Idle and give all states an Exit Time transition to it instead.

But if you just want your script to be able to freely play any animation, I recommend checking out Animancer (link in my signature) so that you don’t have to waste time messing around with Animator Controllers at all.

Took me a bit to get it to work (I have all my player stuff under an Empty Game Object called ‘Player’, so there was a bit of trying to figure out where everything needed to go without making the player sink through the floor), but it works! Thanks for the link.