- Home /
Get walk animation to play on horizontal and vertical axis presses.
I have this script:
function Update () { if (Input.GetAxis("Vertical")) {
animation.CrossFade("Walk"); } else { animation.CrossFade("Idle"); } if (Input.GetAxis("Horizontal")) { animation.CrossFade("Walk"); } else { animation.CrossFade("Idle"); } }
what i want to happen is that if the player presses either the horizontal or vertical axis buttons, to play the walk cycle, and if not, play the idle. However when i use this script it will play the walk on the horizontal key presses but not on the vertical ones. How do I solve this?
Answer by VS48 · Oct 21, 2010 at 10:19 PM
Your vertical input is completely ignored because your "if-else" block for horizontal input will always override what the vertical input did.
What you probably want is:
function Update ()
{
if (Input.GetAxis("Vertical"))
{
animation.CrossFade("Walk");
}
else if(Input.GetAxis("Horizontal"))
{
animation.CrossFade("Walk");
}
else
{
animation.CrossFade("Idle");
}
}
Or something similar
Answer by Setzer · Mar 30, 2011 at 10:13 PM
function Update ()
{
if (Input.GetAxis("Vertical") || Input.GetAxis("Horizontal"))
{
animation.CrossFade("Walk");
}
else
{
animation.CrossFade("Idle");
}
}
Same as above, honestly, except you only have one condition and one default. If you're unfamiliar with script, "||" means "OR".
Your answer
Follow this Question
Related Questions
How can I play different animations depending on the direction my character is moving? 1 Answer
Play animation for horizontal and vertical movement? 1 Answer
Translate a game object on horizontal and vertical axis? 3 Answers
Vertically Stationary Camera for 2D? 0 Answers
Simple controls for a helicopter 2 Answers