# My player starts walking while still in air

Hi everyone!

I’ve been making a simple 2D platformer in Unity, and I’ve made my own character and animations of idle, walk, jump, attack and also jump attack. All of the animations are implemented and a player can be controlled well. However, there is one small issue. When the player is still in air, after a second or two, he starts walking. Here’s the structure of animations in Animator for Grounded Layer and Air Layer.

Also, here is my code
https://paste.ofcode.org/WbX5zBVPFUq9jrqQznYZx7

Any suggestiuons on what the problem might be??

Response would be very appreciated! :)

Also, here is a video of how it looks like in game:eyes:

Lets give a try with these two:

• If you take a look to this piece of code
``````if(myRigidbody.velocity.y <0)        {
myAnimator.SetBool ("land", true);
}
``````

You can see that you set the animation to landed when the player´s y velocity is negative.The player might be on the air (falling down) but your animator thinks its grounded so it plays the grounded animation.

• If thats not the case you might as well check if the involved animation´s transition has an exit time set, that may make it exit the animation before actually landing.
1 Like

Indeed, Exit time was set on transition from Landing to Default State. I turned that off, but he still walks in the air. Do you have any idea on what should I write in my script instead? Sorry, still a beginner…

Does the character start walking in the air right after it reaches it’s highest point, by any coincidence? If so, it’s due what jc-drile77 said.

If you look at the next image. The black line represents the path of the player jumping. When it’s going up, obviously the velocity is positive. When the player reaches the purple point, in theory the velocity.y = 0, because gravity is pulling the player down. Starting from that point onward, the velocity.y will be negative. Since you tell the animator to “land” whenever “velocity.y < 0”, this is probably why your character starts walking in mid-air. What you want is check for whenever the player touches the blue line again, this is usually done with some kind of ground check which you are already using.

I hope this will help.

1 Like

You were right, the logic behind the IsGrounded function wasn’t good…Here’s how function looks now.

``````    private bool IsGrounded()
{
RaycastHit hit = new RaycastHit ();

Vector2 playerLevel = new Vector2 ();

Debug.Log (myRigidbody.position.y);

if (myRigidbody.velocity.y < 0) myAnimator.SetBool ("land", true);

if (myRigidbody.velocity.y <= 0)
{
foreach (Transform point in groundPoints)
{
Collider2D[] colliders = Physics2D.OverlapCircleAll (point.position, GroundRadius, whatIsGround);

Vector2 real = new Vector2 ();
real.x = point.position.x;
real.y = point.position.y;
Collider2D collider = Physics2D.OverlapCircle(real, GroundRadius, whatIsGround);

for (int i = 0; i <colliders.Length; i++)
{
if (colliders[i].gameObject.name.Contains ("platform") || colliders[i].gameObject.name.Contains ("ground")  || colliders[i].gameObject.name.Contains("Stone")
|| colliders[i].gameObject.name.Contains("Ground")|| colliders[i].gameObject.name.Contains("dirt"))
{

myAnimator.SetBool ("land",false);
Debug.Log ("True");
isInAir = false;
return true;
}
}

}
}

return false;
}
``````

I’ve added two if conditions for velocity and one for recognizing all the colliders for game objects by name where the player is grounded.