Enemy Animation Freezing in first frame

Hi I am trying to get an enemy to play 2 animations

the first when he is not moving and the second when he runs at the player. currently the enemy will play the idle animation but only play about 2 frames of the run animation.

Any assistance on this would be greatly appreciated

var distance;
    var target : Transform;    
    var lookAtDistance = 10.0;
    var attackRange = 6.0;
    var moveSpeed = 5.0;
    var damping = 6.0;
animation["idle"].layer = -1;
animation["run"].layer = 1;

    function Update () 
    {
    distance = Vector3.Distance(target.position, transform.position);
    animation.Play("idle", PlayMode.StopAll);
    

    if(distance < lookAtDistance)
    {
    isItAttacking = false;
    renderer.material.color = Color.yellow;
    lookAt ();
    
    }   
    if(distance > lookAtDistance)
    {
    renderer.material.color = Color.green; 
    }
    if(distance < attackRange)
    {
    attack ();
    }
    if(isItAttacking)
    {
    renderer.material.color = Color.red;
    
    }
}


function lookAt ()
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);

}

function attack ()
{
    isItAttacking = true;
    renderer.material.color = Color.red;
    transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
    animation.Play("run", PlayMode.StopAll);
    
}

The problem is this line of code you are calling during Update()
animation.Play(“idle”, PlayMode.StopAll);
It stopps all other animations and plays idle every frame.
Delete that line and try this instead

function Start(){
  animation.CrossFade("idle", PlayMode.StopAll);
}

//inside Update

if(distance < attackRange)
    {
    attack ();
    }
else
   {
    animation.CrossFade("idle", PlayMode.StopAll);
   }

function attack()
{
    //other stuff
    animation.CrossFade("run", PlayMode.StopAll);
}

CrossFade is usually nicer to look at because it fades the animations into eachother in a smooth way. See Unity - Scripting API: Animation.CrossFade . Also make sure the wrapmode is set to loop.

Thanks Thats Great :slight_smile: