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

Bug A bug in my simple character animation setup

Discussion in 'Animation' started by KasunL, Feb 1, 2023.

  1. KasunL

    KasunL

    Joined:
    Apr 19, 2015
    Posts:
    64
    Hello.

    I'm trying to work out a very simple animation setup for my character. There's a bug or a glitch either in my code or in my animator setup. For example, if I hit 'W' and walk forwards and then quickly press 'S' to walk backwards, the model moves backwards according to the code but the Forward Animation does not change to Backward animation. i.e. the character moves Backwards with the Forward animation still running. But if I press 'W' and give it a few second and press 'S', the movement and animation work properly in-sync. It looks like there's a slight lag between Animator transitions.

    Here is my basic code for movements and animations:
    Code (CSharp):
    1. void Movement()
    2.     {
    3.         if (controller.isGrounded) // only move player when it is grounded
    4.         {
    5.             // get move directions from user
    6.             float horizontal = Input.GetAxis("Horizontal");
    7.             float vertical = Input.GetAxis("Vertical");
    8.  
    9.             /// Animations
    10.             // Forwards and Backwards
    11.             if (vertical > 0f) // FWD anim: vertical > 0 when user hits 'W'
    12.             {
    13.                 cAnimator.SetBool("bool_WalkFWD", true);
    14.             }
    15.             else if (vertical < 0f) // BWD anim: -vertical 'S'
    16.             {
    17.                 cAnimator.SetBool("bool_WalkBWD", true);
    18.             }
    19.             else if (vertical == 0) // vertical = 0, transition to Idle-animation either from fwd or bwd anim
    20.             {
    21.                 cAnimator.SetBool("bool_WalkFWD", false);
    22.                 cAnimator.SetBool("bool_WalkBWD", false);
    23.             }
    24.             // Left and Right
    25.             if (horizontal > 0f)
    26.             {
    27.                 cAnimator.SetBool("bool_WalkR", true);
    28.             }
    29.             else if (horizontal < 0f)
    30.             {
    31.                 cAnimator.SetBool("bool_WalkL", true);
    32.             }
    33.             else if(horizontal == 0f)
    34.             {
    35.                 cAnimator.SetBool("bool_WalkR", false);
    36.                 cAnimator.SetBool("bool_WalkL", false);
    37.             }
    38.             //
    39.  
    40.             direction = new Vector3(horizontal, 0, vertical); // assign directions to the vector3 var
    41.             direction *= speed; // add the player movement speed
    42.  
    43.             if (Input.GetKeyDown(KeyCode.Space))
    44.             {
    45.                 direction.y = jumpHeight; // user hits Jump, assign the hight-to-jump to Y axis
    46.                 cAnimator.SetBool("bool_Jump", true);
    47.             }
    48.             if(Input.GetKeyUp(KeyCode.Space))
    49.             {
    50.                 cAnimator.SetBool("bool_Jump", false);
    51.             }
    52.             else
    53.             {
    54.                 if (Input.GetKeyUp(KeyCode.Space))
    55.                 {
    56.                     cAnimator.SetBool("bool_Jump", false);
    57.                 }
    58.             }
    59.         }
    Here is my simple Animator:

    upload_2023-2-1_11-12-2.png

    Here are the Properties for every Animation state (They are all the same):

    upload_2023-2-1_11-15-42.png
    upload_2023-2-1_11-16-30.png

    The Conditions:
    For Idle to walking FWD (No exit time):

    upload_2023-2-1_11-17-51.png

    FWD to Idle:

    upload_2023-2-1_11-19-18.png

    Above are the same for the other animations, with different conditions states obviously. Especially when I Jump, the glitch is more prominent: Once I hit Jump, the animation loops without stopping. It only stops if I hold the spacebar for a few seconds.

    Can anyone please point me to what I'm doing wrong?
    Hope this post makes sense.

    Thanks.