- Home /
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!!
assu$$anonymous$$g your enemy is sitting a single place the most simple way is to store is spawn spot and when distance is far enough return him to the vector3 you have assigned using a simple lerp I think or a smoothdamp function for where he is and where you want him and use transform.lookat for rotation.
You did ask for the simplest way and said you were not worried about the obstacles atm so I gave you the simplest way to do it
you're right. I did ask for the simplest way. I also wanted to have my code exa$$anonymous$$ed for any mistakes, with particular attention paid to the enemy rotating towards the start point and then walking back to the start point. That is what I meant when I asked for the simplest way to solve my problem.
Answer by hotshot03 · Jun 14, 2013 at 11:25 AM
var enemyStartPosition; function Start() { enemyStartPosition = enemyTransform.transform.position; }
function ReturnToStart() { enemyTransform.transform.position = enemyStartPosition; }
@aldonaletto @Benproductions1 I'm wondering if you might take a look at my code, and give me some insight into where I went wrong in its logic. $$anonymous$$UCH APPRECIATED!