animation wont play whilst object is following player

hi, first post I’ve made but ive been using unty for about a year now making simple games but now I have a script to make an object follow me but I have robot following me its just got its arms out in the T shape and it just follows me if I take the following off it works fine any suggestions?

my code:

var target : Transform; //cant't be bothered to do any commments
var moveSpeed = 3;
var rotationSpeed = 3;
var range : float=10f;
var range2 : float=10f;
var stop : float=0;
var myTransform : Transform;
function Awake()
{
     myTransform = transform;
}
 
function Start()
{
      target = GameObject.FindWithTag("soldier").transform;
 
}
 
function Update () {
     //rotate to look at the player
     var distance = Vector3.Distance(myTransform.position, target.position);
     if (distance<=range2 &&  distance>=range){
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
     }
 
 
     else if(distance<=range && distance>stop){
 
     //move towards the player
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
     }
     else if (distance<=stop) {
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
     }
 
 
}

ok so I fixed 1 think now the animation plays but its strange cause when he chases me with animation it looks great but when the animation is finished he just goes back to where the animation started instead of chasing without a stop I want the animation to loop but not go back to the position it started in for example:

E = enemy, P = player, _ = distance

Before Seen: E________________P

Animation starts: ____E_________P

Animation Stops: E_____________P

and loops that forever

please help :slight_smile:

if it’s any more help the only way I can get the thing to animate at all is with the animator the animation one doesn’t run it for some reason

ive fixed most of this now, 1 more problem with animations is that I have 6 animations:

idle
crouch + idle
suspicious
walk
walk + aim
run + aim

I want them to be idle and then when I get within a range of them they walk and aim so is there a way to change the animation in JS

the animation seems to only play with the animator