- Home /
Enemies running when they should be following.
I've got a basic follow script set up for a set of enemies. The script is supposed to make the enemy look at the player up to a certain distance and, when the player gets close enough, follow the player. The script sort of works, but for some reason, when I set the maximum follow distance to anything above 10, the enemies will run away from the player instead. If I somehow get the player to the with the 10 range of the enemy before they spawn they will follow as they are supposed to.
Here's the entire enemy code:
public float MaxDistance = 10.0f;
public float MoveSpeed = 5.0f;
public float MinDistance = 0.0f;
public Transform Target;
public float LookAtDistance = 10.0f;
public float Distance;
public float Damping = 5.0f;
private Quaternion rotation;
public bool parenting = false;
private PlayerHealth_Test playerHealth;
private float nextDam;
//private Bank rolling;
public bool rolling = false;
public float DamDelay = 2.0f;
private Leech_Mother_AI_Test spawnCount;
private GameObject LeechMother;
void Start (){
Target = GameObject.FindGameObjectWithTag ("Player").transform;
playerHealth = Target.GetComponent<PlayerHealth_Test>();
parenting = false;
// rolling = Target.GetComponent<Bank>();
nextDam = Time.time + DamDelay;
GameObject spawner = GameObject.Find ("Leech_Mother");
spawnCount = spawner.GetComponent<Leech_Mother_AI_Test>();
}
void OnTriggerEnter(Collider Player)
{
transform.parent = GameObject.FindGameObjectWithTag("Player").transform;
parenting = true;
}
void Update () {
Distance = Vector3.Distance (Target.position + Target.transform.forward, transform.position);
if (Distance < LookAtDistance)
{
LookAt();
}
if (Distance > MinDistance && Distance < MaxDistance && parenting == false)
{
follow ();
}
if (rolling == true)
{
parenting = false;
GameObject.Destroy(this.gameObject);
spawnCount.SpawnDown(1f);
}
if (parenting == true && nextDam <= Time.time)
{
nextDam = Time.time + DamDelay;
playerHealth.CurrentHealth -= 1.0f;
}
if (parenting == true && playerHealth.CurrentHealth <= 0)
{
parenting = false;
}
}
void LookAt(){
rotation = Quaternion.LookRotation (Target.localPosition - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
void follow(){
transform.Translate(Vector3.forward * MoveSpeed * Time.deltaTime);
}
}
Wow, you wrote quite a code to do this... Just a small tought before I think of an answer, you know that the transform have a lookAt function? Plus I'm not sure if your distance calculation is correct.
This is technically the entire enemy AI which includes dealing damage, dying, parenting and the like. I just like putting the entire code up just in case my problem is somewhere else in the code that I didn't think to look. Also, this is one of the first $$anonymous$$ajor pieces of code that I've done so it's rather...messy.
The follow and LookAt function I got from a tutorial on the subject, I basically copied those parts of the code over. And again, unless I put the $$anonymous$$axDistance above 10 the enemy will follow and if I somehow get within 10 before the enemy spawns they still follow even if the $$anonymous$$axDistance is above that.
Answer by tanoshimi · Nov 18, 2013 at 08:54 PM
"when I set the maximum follow distance to anything above 10, the enemies will run away from the player instead"
The reason your enemies "run away" is because, based on these two lines, your enemies only start looking at the player when they are less than 10 units away:
public float LookAtDistance = 10.0f;
if (Distance < LookAtDistance)
{
LookAt();
}
If you set the "max follow Distance" anything greater than the "LookAtDistance" (currently 10), then they're still attempting to follow the player (i.e. the follow() routine still executes), but they do so by just walking forward in whatever direction they happen to be facing at the time:
transform.Translate(Vector3.forward * MoveSpeed * Time.deltaTime);
I...I just...GOD I'$$anonymous$$ DU$$anonymous$$B! Deletes code, burns laptop, becomes hermit
I honestly didn't even thing about the fact that the "forward" of the enemies was the issue. I guess you learn something new about coding every day. ...Even if that thing is something you should have known months ago.
Thank you very much for clearing that up.
Your answer
![](https://koobas.hobune.stream/wayback/20220613122147im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Enemy Targetting Help 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Enemy Pirate Ship AI 1 Answer