Enemy Follow Issue

My issue resides with the Enemy Ghosts in my game, I have created a targetarea (trigger sphere) around each enemy. When the player enters the sphere of one enemy, this script is called:

function OnTriggerStay (other3 : Collider) {

	
	if (other3.gameObject.tag == "Player")
	{
		Safe = true;
		Debug.Log("Safe");
	}
	
}

Which leads then onto the safe function,

if (EnemyTargetArea.Safe)
{
	EnemyGhostMovement.speed = 0.55;

}

Unfortunately for some odd reason, when the player enters one targetarea sphere of a single enemy. All enemies follow. How do I fix this issue?

  • Sythen.

Haha what a coincidence I was working on a enemy follow script today too! Here’s what I did and it works fine:


var speed : int = 0;
var followrange : int;
var stoprange : int;
static var canmove : boolean = true;
var target : Transform;
private var stopped : boolean = false; 

function Update(){

if(Vector3.Distance(target.position, transform.position) <= followrange && stopped == false)
{
canmove = false;
transform.LookAt(target);
transform.Translate(Vector3(0,0,1) * speed * Time.deltaTime);
}

if(Vector3.Distance(target.position, transform.position) <= stoprange)
{
canmove = false;
transform.LookAt(target);
stopped = true;
}

if(Vector3.Distance(target.position, transform.position) > stoprange)
{
stopped = false;
}

if(Vector3.Distance(target.position, transform.position) > followrange)
{
canmove = true;
}

}

I just used vector3 distances instead of triggers. You can edit the speed at which the enemy follows you, the range with followrange, and stoprange is if you want him to stop at a certain range when he comes close to you. I hope it works!

EnemyTargetArea.Safe is a static variable: a static variable is unique, thus setting it in one script affects all other script instances (it’s the same variable for everybody).

You should remove the static key word from the Safe variable declaration - and I suspect that you should do it with speed in EnemyGhostMovement as well, or all enemies will have the same speed.

The solution depends on your game object hierarchy. If the trigger is an enemy’s child and EnemyGhostMovement is attached to the enemy, you could do the following:

// EnemyTargetArea script:

function OnTriggerEnter (other3 : Collider) {
    if (other3.gameObject.tag == "Player")
    { // call the function SetSpeed(0.55) in the parent or grandparent
       SendMessageUpwards("SetSpeed", 0.55);
    }
}

// in the EnemyGhostMovement script:

var speed: float;

function SetSpeed(vel: float){
  speed = vel;
}

Another alternative that follows your original script (and it’s a little faster too):

// EnemyTargetArea script:

var Safe: boolean = false;

function OnTriggerEnter (other3 : Collider) {
    if (other3.gameObject.tag == "Player")
    {
       Safe = true;
    }
}

// in the EnemyGhostMovement script:

var speed: float; // remove the static keyword!
var areaScript: EnemyTargetArea; // declare a reference to the script
  ...
  // find script if necessary (happens only once):
  if (!areaScript) areaScript = GetComponentInChildren(EnemyTargetArea);
  if (areaScript.Safe) speed = 0.55;
  ...

Notice that I changed OnTriggerStay to OnTriggerEnter: usually you should set Safe true in OnTriggerEnter and false in OnTriggerExit, thus the variable would show whether the player is inside the area or not.

Hey guys,

I fixed the issue; Rather than using a hit sphere around each enemy using the same script I removed it, and searched for the distance between that and the player. In this case, it dealt with each enemy separately and allowed them to move correctly.

In short, aldonaletto, your answer about static variables did allow me to see where I was going wrong, but posly, your script about the difference between target distance and player distance was most correct and helpful.

Thank you guys for your help,

  • Sythen.