# Enemy Follow

Discussion in 'Getting Started' started by KBurkus, Feb 10, 2015.

1. ### KBurkus

Joined:
Feb 1, 2015
Posts:
12
How can I make an enemy follow a character from a distance like in Slenderman.
I have tried, but I can't seem to get it right.

2. ### angrypenguin

Joined:
Dec 29, 2011
Posts:
15,654
What have you tried? We can only give specific guidance if you ask a specific question.

3. ### KBurkus

Joined:
Feb 1, 2015
Posts:
12
I had a little help looking on the internet. it was supposed to follow me when i got in a certain range. Here is the script:

var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var range : float=10f;
var range2 : float=10f;
var stop : float=0;
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
}
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);
}
}

4. ### elmar1028

Joined:
Nov 21, 2013
Posts:
2,359
First. Use code tags.

Second. I would suggest you using Navmesh. It detects obstacles and avoids them as long they're not moving.
There you can set a distance between player and himself at which he can stop.
Also, Navmesh is good for terrains at different levels.

5. ### pKallv

Joined:
Mar 2, 2014
Posts:
1,210
Shouldn't this post be in Scripting?

6. ### Ostwind

Joined:
Mar 22, 2011
Posts:
2,804
Yes it should be. Seems he is posting a lot of questions in this section and not the correct ones...

7. ### Ryiah

Joined:
Oct 11, 2012
Posts:
21,937
Or the Getting Started section.

Joined:
Jun 14, 2013
Posts:
1,718
if (distance<=range2 && distance>=range)

So... is it likely that your distance to the enemy will simultaneously be less than 10 and greater than 10 at the same time? Also, what are the chances that you will ever be exactly 10.0 units away from the enemy?

Is he moving at all? What's actually happening?

9. ### ShilohGames

Joined:
Mar 24, 2014
Posts:
3,032
Yeah, this is an example of where better variable names would really help. Replace range with minRange and range2 with maxRange, and suddenly the code's meaning becomes more clear. With obvious variable names, the OP would have picked better values for the variables. (not 10 for both) I'd also recommend changing the variable name stop to stopDistance.