animation problem (do animation in loops when it's don't need)

Third time I asking that question because I didn't get any answer or even comment...

In my game there is an enemy that have 4 animations: walk, aim, put the gun away and hold the gun. When the game starts - the enemy hold he's gun ("HoldGun animation". I set this part on the inspector by "Play Atomatically"). So far so good... When I get closer - the enemy should aim on me ("GoingToShoot animation"). Now the problem starts - the enemy do the aniamtion in loops, thing he shouldn'd do so he seems "dancing". When I get closer - the enemy should gut the gun away ("StopShoot animation") walk a bit to me ("R.H.Walk animation"). When he needs to do the "R.H.Walk animation" he just stop do the "GoingToShoot animation" that was in loop and go forward without do any animation. When he get to the range he needs to shoot he should do the "GoingToShoot animation" again and shoot, he don't do that - he just start "dancing" again.

I didn't set the shooting yet.

Here Is the code:

var distance; 
 var target : Transform;  
 var lookAtDistance = 70.0;
 var attackRange = 60.0;
 var deadRange = 50.0;
 var moveSpeed = 5.0;    
 var damping = 4.0;
 var shootTest = false;
 static var ableFire = false;

 function Update ()   
 {  
     distance = Vector3.Distance(target.position, transform.position);
     if(distance < lookAtDistance)
     {    
         renderer.material.color = Color.yellow;
         animation.Play("GoingToShoot");
         lookAt ();   
     }   

     if(distance > lookAtDistance) 
     {   
         renderer.material.color = Color.green;
     }   

     if((distance < attackRange)&&(distance > deadRange)) 
     {   
         if (shootTest == true)
         {
             animation.Play("StopShoot");
             shootTest = false;
             attack ();
         }
         else
         {
             attack ();
         }
     }   

     if(distance < deadRange)
     {
         stop ();
         shootTest = true;
         ableFire = true;
     }
 }

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

 function attack ()
 {  
     animation.Play("R.H.Walk");
     ableFire = false;
     renderer.material.color = Color.red;   
     //transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
     var offsetDirection = target.position - transform.position;
     offsetDirection.y = 0;
     var rotation = Quaternion.LookRotation(offsetDirection);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
     transform.Translate(offsetDirection.normalized * moveSpeed *Time.deltaTime, Space.World);
 }

 function stop ()
 {
     renderer.material.color = Color.black;
 }

My Question is this: Why the enemy keep "dancing" and how I can get him stop do that???

This is an enemy that I created in Blender and imported it with hes animations. I never had problems with imported animations before...

Sorry for poor english as allways... :(

Just play the animation you want him to play and don't start multiple animations at the same time. Your script starts "GoingToShoot" and "StopShoot" / "R.H.Walk" at the same time.

That will seperate your script into your 4 cases:

  1. outside lookAtDistance
  2. inside lookAtDistance but outside attackRange
  3. inside attackRange but outside deadRange
  4. inside deadRange

Just make sure you play only one animation at a time.

 if (distance > lookAtDistance)
 {
     renderer.material.color = Color.green;
 }
 else if(distance > attackRange)
 {    
     renderer.material.color = Color.yellow;
     animation.Play("GoingToShoot");
     lookAt ();   
 }
 else if(distance > deadRange) 
 {   
     if (shootTest == true)
     {
         animation.Play("StopShoot");
         shootTest = false;
         attack ();
     }
     else
     {
         attack ();
     }
 }
 else
 {
     stop ();
     shootTest = true;
     ableFire = true;
 }

I solved it buy my self! The problem was that I standed in the range some time... It like, when the enemy walk to you if you in his range he need to walk, he will do the walking animation atomatically in loops because the function ends every time the animation that in the function ends, after that he checks again if I still in that range, if I am - he will start the function again and that inculde the animation. That how the codes works...

That what I did to solve that:

var distance; 
 var target : Transform;  
 var lookAtDistance = 100.0;
 var attackRange = 80.0;
 var deadRange = 60.0;
 var moveSpeed = 5.0;    
 var damping = 4.0;
 var shootTest = false;
 var ableWalk = true;
 static var ableFire = false;

 function Update ()   
 {  
     distance = Vector3.Distance(target.position, transform.position);
      if (distance > lookAtDistance)
     {     
         renderer.material.color = Color.green;
     }
     else
     {
         if (distance > deadRange)
         {
             attack ();
         }
         else 
         {
             StandAndShoot ();
         }
     }
 }

 function attack ()
 {  
     //to make shure the animation will play only ones
     if (shootTest)
     {
         ableFire = false;
         animation.Play("StopShoot");
         yield WaitForSeconds (0.4);
         ableWalk = true;
         shootTest = false;
     }

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

         animation.Play("R.H.Walk");
         ableFire = false;
         renderer.material.color = Color.red;   
         //transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
         var offsetDirection = target.position - transform.position;
         offsetDirection.y = 0;
         var rotation1 = Quaternion.LookRotation(offsetDirection);
         transform.rotation = Quaternion.Slerp(transform.rotation, rotation1, Time.deltaTime * damping);
         transform.Translate(offsetDirection.normalized * moveSpeed *Time.deltaTime, Space.World);
     }
 }

 function StandAndShoot ()
 {
     //to make shure the animation will play only ones
     if (shootTest == false)
     {
         animation.Play("GoingToShoot");
         yield WaitForSeconds (0.4);
         ableWalk = false;
         shootTest = true;
     }

     ableFire = true;
 }

(Who will think that this is the problem???)

Sorry for poor english and that question...