Enemy AI Animation

Basically, I want my AI to play his "run" animation when running towards the player, and the "Idle" animation (or Take 001 in this case) when he isn't. Here's my script, currently, he will run up the player, and stop, but continue his run animation.

public var target : Transform;
public var moveSpeed : float = 3.0;
public var attackRange : float = 10.0;
public var stopRange : float = 2.0;
private var isRunning:boolean = true;

function Start(){
    Patrol();
   animation.wrapMode = WrapMode.Loop;
   animation["Take 001"].layer = 1;
   animation["run"].layer = 2;
   animation.Stop();
}

function Update()
{
    if(isRunning == false){
        animation.CrossFade("Take 001");
    }
    if(isRunning == true)
    {
        animation.CrossFade("run");
    }
    var distanceToTarget = Vector3.Distance(transform.position, target.position);
    if(distanceToTarget <= stopRange)
    {
        isRunning = false;
        animation.CrossFade("Take 001");
    }
}

function Patrol(){
    while(true){
        if( AttackRangeCheck() ){ // target object is within range
            if( !StopRangeCheck() ){
                transform.LookAt(target);
                transform.Translate(Vector3.forward*Time.deltaTime*moveSpeed);
            }
        }
        yield;
    }
}

function AttackRangeCheck() : boolean{
    var distanceToTarget = Vector3.Distance(transform.position, target.position);
    if(distanceToTarget <= attackRange)
        return true;
    else
        return false;   
}

function StopRangeCheck() : boolean{
    var distanceToTarget = Vector3.Distance(transform.position, target.position);
    if(distanceToTarget <= stopRange)
        return true;
    else
        return false;   
}

Unsure on what the cause is, though make it go

function Update(){

   if(isRunning == true)    
  {        
  animation.CrossFade("run");    
  }    

  else if(isRunning == false)
  {        
    animation.CrossFade("Take 001");    
  } 

  var distanceToTarget = Vector3.Distance(transform.position, target.position);    
  if(distanceToTarget <= stopRange)    
  {        
  isRunning = false;       
  animation.CrossFade("Take 001");    
  }
}

That way it does either run or not.

(Unsure if it fixes your problem).

May I also add that when I script AI functions use a finte state machine. Or just use states/cases inside the same script.

Example:

enum animals {    
dog,    
cat,    
bird
}

function Start(){    
var animals : animals = animals.dog;    

switch(choice)    {        
case animals.dog:            
 Debug.Log("dog");            
break;        
case animals.cat:           
 Debug.Log("cat");            
break;        
case animal.bird:            
Debug.Log("bird");            
break;        
default:            
Debug.Log("nothing");           
break;    
}

Peace, Aaron Lewis

Put run and Take001 on the same layer.

Each layer can play one animation, and higher layers beat lower ones. Since Run is on layer2, starting Take001 on layer 1 ALSO plays it, but the game uses only Run. If you leave them on the same layer, Take001 will automatically cancel (and fade from) Run. You could stop run before starting Take001, but that's looks worse and is more work.

Higher layers are good for a non-looping animations, like shoot. When it stops, the lower level one, which was always secretly playing, "comes back."

You can skip setting layers for Run/Take001, since the starting layer is 0. I also prefer to set Loop individually for each animation clip, in the Inspector for the model. That way you can set shoot or die to non-looping.

See http://unity3d.com/support/documentation/Manual/Animations.html for a better explanation.