Search code examples
c#animationunity-game-engineunity3d-2dtools

Unity3D - How to detect whether a character has landed after a jump


In my Update() function I have:

isGrounded = Physics2D.Linecast (transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));

if (Input.GetButtonDown ("Jump") && isGrounded) {
    jump = true;
}

I make my character jump using this code in FixedUpdate():

if (jump) {
    anim.SetTrigger("Jump");
    rb.AddForce(new Vector2 (0f, jumpForce));
    jump = false;
}

I have to detect the moment at which my character lands on the ground (isGrounded will then become true) to switch the animation back to Idle. I can't use isGrounded to detect it because as you can see the jump is performed when isGrounded is true, so the Idle animation trigger would launch at the same time as the jump animation giving either little or no jump animation at all.

Could you please help me with this?


Solution

  • I've solved this by dividing the jumping animation into jumping that transitions into falling. Animation switches to those states based on whether the y axis movement is positive or negative.