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

Check which animation is being played

Discussion in 'Animation' started by SpaceUnicorn, Feb 11, 2021.

  1. SpaceUnicorn

    SpaceUnicorn

    Joined:
    Apr 15, 2016
    Posts:
    2
    I am trying to give movement control to a player while jumping, but I run into an issue because of the transition from walking to jumping.
    Basically what I did is check if the animator is in the jumping state, and in that case it's moving while in the air. But it does not work at the start of the jump because of the transition. I just wanted to know if there is a way to check for a specific transition here? I'm fairly new to Unity so maybe there is something I'm doing wrong.

    While writing this I found another way to do it by simply removing the transition time in the editor. I don't think it's a great solution so I'm still curious about answers to my question.

    Code (CSharp):
    1. if (Input.GetKey(KeyCode.Z))
    2.         {
    3.             if (_animator.GetCurrentAnimatorStateInfo(0).IsName("Jump")
    4.                 && _animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 0.4f)
    5.             {
    6.                 Vector3 position = this.transform.position;
    7.                 position.z += 0.1f;
    8.                 this.transform.position = position;
    9.             }
    10.         }
     
  2. M-Elwy

    M-Elwy

    Joined:
    Jan 28, 2021
    Posts:
    38
    Option#1: You can use StateMachineBehaviours
    https://docs.unity3d.com/Manual/StateMachineBehaviours.html

    Option#2: create local boolean isJumping and assign True value after jump button is pressed, then False after collision with ground.

    Option#3: (I use personally) create method (isGrounded) which checks distance to ground, if its over certian threshold then the player is jumping and returns False, otherwise returns True.