- Home /
Textured jump animation moving left and right while in the air -help!
okay so, im making a simple 2D platform game and i have this problem.
Basically i used the Animating tiled texture script from Unity to add a run, idle and jump animation to my player, heres what it looks like:
#pragma strict
function Start () {
}
function Update () {
var at = gameObject.GetComponent(animatetexture); //Store AnimateTexture Script
if(Input.GetAxis("Horizontal")){ //Player moves left
at.rowNumber = 1; //Change to running animation
} else if(Input.GetAxis("Horizontal")){ //Player moves right
at.rowNumber = 1; //Change to running animation
} else if(Input.GetButton("Jump")){
at.rowNumber = 2; //Change to jump animation
} else { //Player doesnt move
at.rowNumber = 0; //Change to idle
}
}
but the problem is, when i jump and move left or right while still in the air, my jump animation stops and goes to the running animation, but if i were to press the space bar alone to jump, it works fine.
i also want to make my jump animation continue on until my player touched the ground ( so my fall animation as well)
please, i hope someone can help me figure out how to fix that, because it really bothers me lol
thank you!
For future posts, please format your code. After pasting, select your code and use the 101/010 button. I did it for you this time.
Answer by Baste · Sep 27, 2014 at 08:50 AM
Your code makes no sense!
This stuff:
if(Input.GetAxis("Horizontal")){ //Player moves left
at.rowNumber = 1; //Change to running animation
} else if(Input.GetAxis("Horizontal")){ //Player moves right
at.rowNumber = 1; //Change to running animation
Checks the exact same thing twice, and does the exact same assignment twice. The second check will actually never be run, and does nothing. What you want to check is if Input.GetAxis("Horizontal") is returning a value that's larger or smaller than 0, to get whether your player is moving to the right or left.
To fix your actual problem, you need to check if your player is in the air or not, and only play the run animation if the player is grounded. There's two main ways of doing this:
Use a raycast to check how far away from the ground your player is. If it is further than a certain amount, you're in the air.
Attach a ball-shaped trigger collider to your player's feet. Give it a script that notifies the player of when it is touching the ground or not.
I would generally recommend the second one, as it's easier to modify where the collider is than to modify the distance of the raycast, as you can see the collider in the inspector.
Your answer
Follow this Question
Related Questions
Fall animation when character is falling 3 Answers
Need help with sprite animating 1 Answer
Scripting and Animation problems 1 Answer
how to manage fbx files animation (blender) in unity using favascript 0 Answers
How do I animate a 2D sprite? 1 Answer