Realtime distance check with enemies

Hi,

I have a script (from the Unity docs) that checks the nearest enemy. The script works fine but it only checks every object (enemy) ones. When the player moves close to a previous object the script does not detect (update) it.

Script:

print(FindClosestEnemy().name);
// Find the name of the closest enemy
function FindClosestEnemy () : GameObject {
// Find all game objects with tag Enemy
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Enemy");
var closest : GameObject;
var distance = Mathf.Infinity;
var position = transform.position;
// Iterate through them and find the closest one
for (var go : GameObject in gos) {
var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
}
return closest;
}

I think it has to do with the for loop that is not being reset. Can someone please help me with this?

Thanks!

Any code not in an event handler will be executed ONCE when the object is first activated, so there’s your problem.

If you put:

print(FindClosestEnemy().name);

inside:

function FixedUpdate(){

}

it would spam it constantly.

You could also put it in a coroutine that only wakes up periodically (e.g. every 0.5 seconds)

e.g.

function CheckForEnemies(){
  while( true ){
    print(FindClosestEnemy().name);
    yield new WaitForSeconds( 0.5 );
  }
}

Now, call this function once and it will keep checking for the nearest enemy periodically.

Thanks! What a fast reply! Both methods are working fine.

Hi,

This looks promising, I’m trying to make an auto-aim script for the player and it needs to look at the closest targets.

I’ll put it in a button function. Hope it will work. Probably going to post if it works, or if it doesn’t.

Hi I having a same issue Frido. but I tried the way that is being mentioned. like putting inside a fixedupdate but still nothing happened it still detect my first object only. Even i move near to the other object it still does not update to the other object that i am near it.

So frido can i ask how you solve the issue. Thanks.

Yeah, could you please post your script so we could see what you came up with ? I need this too :smile:

And I think it could be useful for more people too :slight_smile:

Okay, I got an error message for the part that says:

function FindClosestEnemy () : GameObject {

Seems like unity dislikes the “: GameObject”… :?

someone know how he did it, or how to make it work ?

I used this code, and it works perfectly. Of course, you have to actually do something with the GameObject that gets returned from calling this function, but here it is:

// Find the closest enemy 
function FindClosestEnemy () : GameObject { 
	// Find all game objects with tag Enemy 
	var gos : GameObject[]; 
	gos = GameObject.FindGameObjectsWithTag("Enemies"); 
	var closest : GameObject; 
	var distance = Mathf.Infinity; 
	var position = transform.position; 
	// Iterate through them and find the closest one 
	for (var go : GameObject in gos) { 
		var diff = (go.transform.position - position); 
		var curDistance = diff.sqrMagnitude; 
		if (curDistance < distance) { 
			closest = go; 
			distance = curDistance; 
		} 
	} 
	return closest; 
}

So for example, you could have something similar to this in Update:

function Update() {
	if (Input.GetMouseButton(1)) 
	{
			isEnemyTargeted = true;
			targetedEnemy = FindClosestEnemy();
			DeselectOtherEnemies();
			var script : MouseInteractionObject = targetedEnemy.GetComponent(MouseInteractionObject);
			script.isSelected = true;
   }		
}

This makes it so if you click the Right Mouse button it targets the closest enemy. It then sets isSelected to true on a script called MouseInteractionObject that is attached to all Enemies. You can then do something to the enemy if isSelected = true. I also have a function here called DeselectOtherEnemies() that goes through and deselects all of them before selecting the single correct one.

Ok thanks :smile: I don’t have the time to try this right now but does this code updates everytime ?

like if I move are the closest enemy updated ?