- Home /
Sprite staying on idle animation
Hey guys, in my 2d game I'm trying to get my sprites to use their directional animations when moving. When using the script im working on below it works fine for the downward direction but when the right key is pressed it just sticks in the idle animation and slides across. I tried removing the downward animation and then the issue went away.
But I need to have more than one direction D:
void Update () {
if (Input.GetKey(KeyCode.D))
{
if (!anim.IsPlaying("walking_right"))
{
anim.Play("walking_right");
}
this.transform.Translate(Vector3.right*0.5f*Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
if (!anim.IsPlaying("walking_towards"))
{
anim.Play("walking_towards");
}
this.transform.Translate(Vector3.down*0.5f*Time.deltaTime);
}
else
{
anim.Play ("idle");
}
}
}
Else if fixed it!
Thank you very much :) if you want to redo your comment as an answer i'll give it a thumbs up.
Answer by AlucardJay · Apr 15, 2013 at 07:43 AM
missing an else :
if ( Input.GetKey(KeyCode.D) )
{
// ....
}
else if ( Input.GetKey(KeyCode.S) )
{
// ....
}
else
{
// ....
}
or just use if with a return method ....
if ( Input.GetKey(KeyCode.D) )
{
// ....
return;
}
if ( Input.GetKey(KeyCode.S) )
{
// ....
return;
}
// ....
anim.Play ("idle");
Your answer
Follow this Question
Related Questions
Animator trigger stuck 4 Answers
Non-symmetrical Character walk left animation. 0 Answers
When i make my character animated, he can only move up, down left and right (2D) (JS) 0 Answers
Animation Lag when Changing Movement Direction 2D (4.3) 2 Answers
Help Wanted. Switching localscale starts unwanted animation state. 1 Answer