Return to Start Position

Im trying to make my enemies return to original point of spawn when the player is more than 25 units away, however, i suspect there´s a slight error somewhere in my method as it does NOT waddle back to it´s point of origin, and just sits there staring into my soul with it´s empty cold eyes.

var target : Transform;
var moveSpeed = 20;
var rotationSpeed = 5;
var myTransform : Transform;
var follow : boolean = true;
private var startPosition : Vector3;
var ReturnToPosition : boolean = false;
 
function Awake()
{
myTransform = transform;
}
 
function Start()
{
startPosition = transform.position; //store the start position
target = GameObject.FindWithTag("Player").transform;
 
}


 
function Update () { 
var distance = Vector3.Distance(target.position,myTransform.position); 
 	if(distance < 2){ follow = false; } 
 	if(distance > 2){ follow = true; }
 	if(distance > 25){ follow = false; } 
 	if(follow == true) { myTransform.rotation = Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
   	if(distance > 25) { ReturnToPosition = true; }
   	if(ReturnToPosition == true) {
//rotate to look at the start position
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(startPosition.position - myTransform.position),rotationSpeed*Time.deltaTime);
 
//move back to start position
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}

   
   
 
 
}

}

Some of this code seems pointless. Why are you use “myTransform”, and not the built-in “transform” that’s given to you? Also, you’re testing for distance > 25 twice, which is a waste, and you should probably use else statements (for example, if(distance < 2)… else if(distance > 2) makes more sense, because distance can only be bigger than 2 if it’s not less than 2, and it saves a comparison).

As for your problem, it’s actually fairly obvious. The key lines are these:

if(distance > 25){ follow = false; } 

and

if(follow == true) {

First of all, when the player walks out of range, you’re setting follow to false. That’s good, because it stops the enemy from following the player… but have a look at what else it does. Remember that, when you use an if statement, it acts on all the code within the { }, so let’s have a look at what code is inside the brackets for if(follow == true).

 if(follow == true) 
 { 
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;

     if(distance > 25) { ReturnToPosition = true; }
     if(ReturnToPosition == true) 
     {
         //rotate to look at the start position
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(startPosition.position - myTransform.position),rotationSpeed*Time.deltaTime);
 
         //move back to start position
         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
     }
}

(I’ve reformatted it slightly to make it easier to read).

Now, we can see that all the code to return the NPC to the start point is within this if statement… but you set “follow” to false earlier in the code, meaning that this code will never be run when the distance is greater than 25, meaning that “ReturnToPosition” is never set, and thus the rest of the code isn’t run. You just need to take the last bit out of the “follow” condition.