Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Animation States transitioning too fast

Discussion in 'Animation' started by Kanassa, Jul 24, 2020.

  1. Kanassa

    Kanassa

    Joined:
    Nov 1, 2018
    Posts:
    6
    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.



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CharacterController : MonoBehaviour
    6. {
    7.     public float Speed;
    8.     public Transform Character;
    9.     public bool dodging;
    10.     public Animator animator;
    11.     private Vector3 moveDirection = Vector3.zero;
    12.  
    13.     public Vector3 jumpVector;
    14.  
    15.     private void Awake()
    16.     {
    17.         animator = GetComponent<Animator>();
    18.     }
    19.  
    20.     //Dodging state is recorded so that the player will be unable to attack when doing a dodging move
    21.     void OnTriggerEnter(Collider other)
    22.     {
    23.         if (other.CompareTag("Center"))
    24.         {
    25.             dodging = false;
    26.         }
    27.     }
    28.  
    29.     void OnTriggerExit(Collider other)
    30.     {
    31.         if (other.CompareTag("Center"))
    32.         {
    33.             dodging = true;
    34.         }
    35.     }
    36.  
    37.     void Update()
    38.     {
    39.         //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
    40.         //AnimState to determine animation. 0 = Idle, 1 = DodgeLeft, 2 = DodgeRight, 3 = DodgeDown, 4 = DodgeUp,
    41.         if (Input.GetKeyDown(KeyCode.A))
    42.         {
    43.             transform.position += Vector3.back * Speed;
    44.             animator.SetInteger("AnimState", 1);
    45.             Debug.Log("Button pressed");
    46.         }
    47.         else if (Input.GetKeyUp(KeyCode.A))
    48.         {
    49.             transform.position += Vector3.forward * Speed;
    50.             Debug.Log("Button no longer pressed");
    51.         }
    52.         else if (Input.GetKeyDown(KeyCode.D))
    53.         {
    54.             animator.SetInteger("AnimState", 2);
    55.             transform.position += Vector3.forward * Speed;
    56.             Debug.Log("Button pressed");
    57.         }
    58.         else if (Input.GetKeyUp(KeyCode.D))
    59.         {
    60.             transform.position += Vector3.back * Speed;
    61.             Debug.Log("Button no longer pressed");
    62.         }
    63.         else if (Input.GetKeyUp(KeyCode.S))
    64.         {
    65.             animator.SetInteger("AnimState", 3);
    66.             Debug.Log("Button no longer pressed");
    67.         }
    68.         else if (Input.GetKeyUp(KeyCode.W))
    69.         {
    70.             animator.SetInteger("AnimState", 4);
    71.             GetComponent<Rigidbody>().AddForce(jumpVector, ForceMode.VelocityChange);
    72.             Debug.Log("Button no longer pressed");
    73.         else
    74.         {
    75.             animator.SetInteger("AnimState", 0);
    76.         }
    77.     }
    78. }
    79.  
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,554
    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.
     
  3. Kanassa

    Kanassa

    Joined:
    Nov 1, 2018
    Posts:
    6
    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.