Correct jumping cycle logic in Unity with 3 jump animations

I have 3 jump animations; jumping up, jumping down, and the loop in between. I want to play them in an order, using logic.

When the player presses space, I should play the "jumping up" animation, followed by the "jump loop" while player is in air, and the "jumping down" animation just before touching the ground. If the player falls from a height, I should play the "jump loop" animation while in air, followed by the "jumping down" animation just before touching the ground.

This is what I have so far, to provide the animation:

void Update ()
{
if (!animator)
{
return;
}

float vSpeed = Input.GetAxis ("Vertical");
animator.SetFloat ("VSpeed", vSpeed);

if (Input.GetButtonDown ("Jump"))
{
animator.SetBool ("Jumping", true);
Invoke ("StopJumping", 0.1f);
}

if (!charController.isGrounded)
{
animator.SetBool ("Grounded", false);
RaycastHit hit;
Ray downRay = new Ray (transform.position, Vector3.down);

if (Physics.Raycast (downRay, out hit))
{
animator.SetFloat ("Distfromground", hit.distance);
}
}
}

void StopJumping()
{
animator.SetBool ("Jumping", false);
}


Am I doing anything wrong, or is there a better way to do this? As it is, it is not giving me the desired effects.

• You say "it doesnt look right" and "it is not giving the desired effects", but you fail to tell us what it does do. Could you give us some information on what it is currently doing, compared to what you actually want it to do, to help diagnose the actual problem? – Gnemlock Oct 18 '16 at 7:03

Based off the information given, here is my answer(which makes plenty of assumptions).

const int landingDistance = 0.2f;
void Update ()
{
if (!animator)
{
return;
}

animator.SetFloat ("VSpeed", Input.GetAxis ("Vertical"));

if (Input.GetButtonDown ("Jump"))
{
//transition into Jump animation state
animator.SetTrigger("Jump");
}

if (!charController.isGrounded)
{
//transition from Jump to !Grounded animation state
animator.SetBool ("Grounded", false);

if (Physics.Raycast (transform.position, Vector3.down, landingDistance))
{
//transition from !Grounded to Landing animation state
animator.SetTrigger("Landing");
}
}
}


Here is what I did to simplify things:

• You should be using a Trigger instead of manually setting booleans.
• Removed un-needed Raycast information.

Here is what I did to achieve the goal:

• Define three animation states: Jump -> !Grounded -> Landing.

In your animation tree, it will be as simply as a linear progression through the states:

Idle -(Jump Trigger)> Jump -(Grounded is false)> !Grounded -(Landing Trigger)> Landing

In the transition between Jump and !Grounded, you can tweak the transition to provide a nice effect.