# How to Return Enemy to startPosition

Hi everybody,

I’m an amateur and a hobbyist, so I appreciate any and all help. What I am hoping to accomplish is have the enemy start at a certain point, pursue the hero, and if the hero flees, have the enemy return to its starting point.

Off the top of my head, I am thinking the coding should look something like this, but I am not getting the results that I want. I will provide code that displays what I am thinking…

``````if (distance > alertDistance)
{
ReturnToStart();
}
``````

then further down into the function, I’d like the enemy to be satisfied that the hero has left the problematic area, rotate back to the starting point, and return to the starting point. This is where the code gets away from me because on top of the basic concept, there is the additional problem of trying to avoid obstacles in the way. I’ll leave that until later. I only really want the simplest version of this concept. This is kind of what I think makes sense:

``````function ReturnToStart()
{
attackTimer = 0.0;
//rotate to look at the start position
var rotation = Quaternion.LookRotation(target.position - transform.position);
var enemyTransform = transform.position;
enemyTransform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
Quaternion.LookRotation(startPosition - enemyTransform);

//move back to start position
enemyTransform += enemyTransform.forward * moveSpeed * Time.deltaTime;
}
``````

don’t worry about attackTimer, it serves a function in the code for something else than what applies here. Please forgive the gaps in thinking or if this is irrational or anything like that. I was trying to wrap my head around a few concepts, and this is what I thought made sense.

THANK YOU ALL SO MUCH!!

var enemyStartPosition;
function Start()
{
enemyStartPosition = enemyTransform.transform.position;
}

function ReturnToStart()
{
enemyTransform.transform.position = enemyStartPosition;
}