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");
	}
		
}

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 :slight_smile: