- Home /
Need help finding the closet player to an enemy
Hi, my game can have upto 4 players in it, and I am trying to work out how to determine which player is closest to an enemy, but I cannot seem to make the following script work.
I was trying to search for all tagged players, store them and then iterate through them one by one.
It seems that you cannot pass multiple tags into FindGameObjectsWithTag and that is where my problem is as my players are tagged as the following:
Player1 Player2 Player3 Player4
Here is the script I am trying to use (i have hard coded "Player1" into the FindGameObjectsWithTag as I only have 1 player at the mo, so I can test enemy AI.
// Find the closest enemy
void FindClosestEnemy()
{
// Find all game objects with tag Player
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag("Player1");
var distance = Mathf.Infinity;
var position = transform.position;
// Iterate through them and find the closest one
foreach (GameObject go in gos)
{
var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
target = closest;
}
Please can someone help?
If there is no need for real-time update of the closest enemy (like only get the closest before making the attack or smth), you could try Physics.SphereCast with a specific layer mask of the enemies (asu$$anonymous$$g they have colliders attached).
I see what you mean, but I need to do this realtime as it sets the AI target of the closest player as they move around...
Answer by fafase · Feb 02, 2015 at 11:08 AM
if all your enemy have a common script like Enemy.cs you can find all active instances in the scene:
void FindClosestEnemy()
{
// Find all game objects with tag Player
Enemy[] gos = FindObjectsOfType<Enemy>(); // Change here
var distance = Mathf.Infinity;
var position = transform.position;
// Iterate through them and find the closest one
foreach (Enemy go in gos) // Change here the first type in the foreach (or use var instead)
{
var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
target = closest;
}
Thanks, but I am not looking to find all objects with a common tag. I am trying to find 4 objects, each with a seperate tag.
If you seek for help you need to read the answer. In the way I provide, there is no tag anymore, there is a type of script. If you have Georges (Guitar1), Paul (Bass), John (Guitar2) and Ringo (Drums), they have different names and different tags but they all four got a Beatle script on them:
Beatle[] beatles = FindObjectsOfType<Beatle>();
beatles contains all four items.
Ah, I see what you are trying to show, use find objects with Type and look for the player manager script (which is common).
I tried that, but I cannot create an array of GameObjects using that :(
thanks for explaining, but that syntax doesnt work because you cannot create an array of script objects
Again, read the damn answer...or learn Unity (not being rude trust me). What you need is the Transform component since you are after the position, gameObject gets you there with
gameObject.transform.position;
well you can do the same with any $$anonymous$$onoBehaviour component
beatle.transform.position;
Did you notice line 10 of my answer?
Answer by MurcsRoyce · Jul 16, 2018 at 08:05 PM
i don't use Var but i did find this document helpful. thanks
Your answer
Follow this Question
Related Questions
What's the most efficient way of finding child objects at runtime? 1 Answer
Is "GameObject.FindAllGameObjectsWithTag" related to the hierachy? 1 Answer
Find one inactive player (gameobject) 2 Answers
Toggling a game object between an active and inactive state 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers