- Home /
play animation when moving,and idle animation when stand still
i have this script but the object still play run animation when not moving...what should i change??
private var lastPosition : Vector3;
function Update () {
if(Vector3.Distance(transform.position, lastPosition)>=1.0){
animation.Play("Run");
}
else{
animation.Play("Idle");
}
}
Answer by MaVCArt · Aug 08, 2013 at 10:00 AM
Where does your lastPosition get updated? If it's on its uninitialised state, the distance will most likely always be greater than 1 - the Vector3 will most likely be initialised by Unity at (0,0,0).
If this happens in a piece of code you have not posted here, the result you're trying to achieve would probably work better if you used something like:
if ( speed != 0 ) { play running animation; }
else { play idle animation; }
this is pseudo code of course, but you get the point :)
Your answer
Follow this Question
Related Questions
Enemy Animation Freezing in first frame 2 Answers
Using multiple buttons in script 3 Answers
scripting Animations 1 Answer
Slow down and play walk animation on left shift? 3 Answers
Enemy AI Animation 2 Answers