- Home /
Transition from jump to walk animation.
Hi all,
I am recreating level 1-1 of Super Mario Bros in unity and am using tk2d for the sprite management and c# for scripting.
I have the stage setup and am currently animating Mario who is being controlled by a character controller.
He can idle left and right, walk left and right, run left and right and jump left and right.
My problem is when i jump left or right the jump animation plays (Mario with his fist up) but when i land on the ground the animation does not switch to the walk or run animation instead it keeps the jump animation and just slides along the ground until i let go of the direction button then Mario returns to his idle pose and then if i hit the left or right direction button he will walk as normal.
I am unsure how to solve this transition from jump animation to walk/run animation.
Here is the code i am using for the walk and jump animations.
//Walk to the left.
if (Input.GetKeyDown(KeyCode.A))
{
velocity *= walkSpeed;
anim.Play("MarioSmallWalkLeft");
}
//Walk to the right.
if (Input.GetKeyDown(KeyCode.D))
{
velocity *= walkSpeed;
anim.Play("MarioSmallWalkRight");
}
//Jump.
if (Input.GetButtonDown("Jump")
{
velocity.y = walkJump;
}
//Jump left animation.
if (velocity.y > 0 && moveDirection == 0)
{
anim.Play("SmallMarioJumpLeft");
}
//Jump right animation.
if (velocity.y > 0 && moveDirection == 1)
{
anim.Play("SmallMarioJumpRight");
}
moveDirection 0 and 1 are for determining whether Mario is facing left or right.
Any help would be much appreciated.
Thanks.
perhaps you could check if the player is grounded, then check if a 'Small$$anonymous$$arioJump' animation is playing, if it is not playing or has stopped playing then play/crossfade the Idle animation =]
Thanks for the reply. :)
I have been messing around with what you said trying to figure out how to make it work.
The problem i see is that $$anonymous$$ario has to be grounded to jump and he is grounded again when he lands on that second grounded the animation needs to switch, but i don't know how to switch the animation and distinguish between the two different times when $$anonymous$$ario is on the ground.
I found the Play$$anonymous$$ode.StopAll which can be used in animation, but i could not get it to work.
And i just found animation.Stop() perhaps if i put that with the jump animation and an if(!controller.isGrounded && velocity.y < 0) it will stop the animation and then i could use the walk animation.
Anyway i buggered it's 4:10am and i think i will sleep on it and tackle the problem again when i get up.
Thanks again for the help and if you or anyone else has any other suggestions please feel free. :)
Answer by drizztmainsword · Sep 04, 2012 at 10:39 PM
I would distinguish moving on the ground and moving in the air more.
if (Input.GetKey(KeyCode.D))
{
// Set velocity to the right
// Set moveDirection
}
if (Input.GetKey(KeyCode.A))
{
// Set velocity to the left
// Set moveDirection
}
if (controller.isGrounded)
{
// Animations for walking
}
else
{
//In air animations.
}
Hi Taylor,
Thanks for the info it is most useful. :)
I put this in a else statement outside of my if (controller.isGrounded) statement.
if (!controller.isGrounded && velocity.y > 0 && moveDirection == 0) { anim.Play("Small$$anonymous$$arioJumpLeft"); } else if (moveDirection == 0) { anim.Play("$$anonymous$$arioSmallWalkLeft"); }
if (!controller.isGrounded && velocity > 0 && moveDirection == 1) { anim.Play("Small$$anonymous$$arioJumpRight"); } else if (moveDirection == 1) { anim.Play("$$anonymous$$arioSmallWalkRight"); }
So far the results are much better, $$anonymous$$ario now lands and walks/runs, the problem now is that he will only play the jump animation on the way up, not on the way up and the way down like he is supposed to.
Any idea's?
The reason for that is you are checking to see if the velocity of $$anonymous$$ario is positive, or moving up.
You should be able to get away with just checking to see if $$anonymous$$ario is on the ground and his moveDirection value.
Also, having else statements after the above statements are redundant. I looked at your code again, and what you want to do is separate the inputs from the animations. I've edited my post with a better breakdown.
Your answer
Follow this Question
Related Questions
How can ı activate my jump script only at animation 0 Answers
3d Animated Jump Script 0 Answers
How to fluently transition into an animation state? 0 Answers