- Home /
Finding nearest game object
How do I find the nearest gameobject (or tag) and determine the distance between that object and the player?
clear explanation or C# only
Answer by b1gry4n · Aug 30, 2016 at 02:55 AM
If you have a lot of objects in your scene that you want to find the distance from but there are other objects you dont care about (for example: many enemies across the map, but you only want to get the enemies near the player) I would recomment using OnTriggerEnter to gather a list of nearby objects first.
You would do this by:
creating an empty game object as a child of your player
attaching a sphere collider and setting "trigger" to true
setting the radius to a "search radius" size determined by you
Attach a script to the object with OnTriggerEnter and do a check to determine if you care about this object (compare tag)
adding that object to a list.
OnTriggerExit remove that object from the list.
How to do this can be found here
You could also do a Physics.SphereCast invoking to obtain this list.
The next step would be to read from that list and find the closest object.
Transform GetClosestEnemy (List<Transform> enemies, Transform fromThis)
{
Transform bestTarget = null;
float closestDistanceSqr = Mathf.Infinity;
Vector3 currentPosition = fromThis.position;
foreach(Transform potentialTarget in enemies)
{
Vector3 directionToTarget = potentialTarget.position - currentPosition;
float dSqrToTarget = directionToTarget.sqrMagnitude;
if(dSqrToTarget < closestDistanceSqr)
{
closestDistanceSqr = dSqrToTarget;
bestTarget = potentialTarget;
}
}
return bestTarget;
}
This will return the closest transform from the list you plug in. So you would need to manually call this function to do the check.
closestEnemy = GetClosestEnemy (yourscript.yourtransformlist, this.transform);
Finding the distance from the closest enemy to the player is a simple Vector3.Distance
Hi. I dont understand one thing. closestEnemy = GetClosestEnemy (yourscript.yourtransformlist, this.transform);
yourtransformlist is "enemies" list from that script ? It does not work like that. Thanks.
The enemies
variable is local to the GetClosestEnemies function. Whatever you pass through when you call the function is referred to as enemies
within that function, nowhere else.
What b1gry4n is suggesting that you pass through to it is the List of candidate objects that you've constructed using the method described in the second part of their answer.
As a first step, to see how it all works, you could try using Arkaid's answer to construct the list. So for example you could just try
closestEnemy = GetClosestEnemy( new List< GameObject > (GameObject.FindGameObjectsWithTag("Enemy")), this.transform );
This will be much less efficient but, depending on how often you make the call and how many objects you have, it might not matter too much.
One warning - if the object you're finding the distance from (the this
in the call) has the same tag, then you'd either have to add something to the GetClosest function to make it ignore that object, or remove it from the list before passing it through. (otherwise it'd always just find itself - because the distance from itself is zero)
closestEnemy = GetClosestEnemy( new List (GameObject.FindGameObjectsWithTag("Enemy"), this.transform ); does not work because you cant turn new List into List
closestEnemy = GetClosestEnemy( new List (GameObject.FindGameObjectsWithTag("Enemy"), this.transform );
does not work because it is trying to cannot convert from 'System.Collections.Generic.List' to 'System.Collections.Generic.List'
That code still has the error that I fixed yesterday (see comment above). You need a second closing bracket after ("Enemy")
. I just checked the fixed code, it compiles for me.
Also worth noting that the error you're posting looks odd - it's saying "can't convert from X to X" when obviously no conversion would be needed as X=X.
It's possible that something's getting lost when you're copy-pasting the error message, due to the <
and >
not getting rendered in html. You can use back-quotes or code blocks to get around this.
Answer by Arkaid · Aug 30, 2016 at 01:51 AM
20 seconds in google turn this up:
https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html
Second example. Not to bash, but search a bit before posting a question next time? :3~
It should. The code is solid. You must have a bug somewhere.
There's no bug in my game. The issue with unity API documents, is the instructions or information can sometimes be very vague. The code may not have caused any errors, but nothing in the code seems to work, even when I print or use debug.log, I get nothing.
Looking back, I think the problem is that my version of Unity is different than the one in the example. But yes, I'v tried that and messed with that code several times...
For anyone co$$anonymous$$g across this post via Google:
While the example from the Docs herein works, it's not exactly optimized. All "Find" operations are expensive, and "Find With Tag" requires passing string references which... sucks, to put it bluntly.
Personally, I don't love using the SphereCast in the green answer above, but if you already have your enemies collected in another way (such as having them all spawn into an empty and looping through that, for example), the principles of the rest of that solution should get you what you need.
To be fair, the accepted answer does actually involve collecting your list of enemies "in another way" - by adding/removing them to/from such a collection using a proximity trigger. That's only slightly different from what you're suggesting, and could be more efficient (it'd depend on the numbers). The references to SphereCast and (in the comments) Find functions are only there as examples of ways of collecting the objects using simpler (but yes, less efficient) code.
Answer by studiomilk_ · Mar 15, 2020 at 07:43 PM
Hi, I wrote this 5 minutes ago and it works for me:
minions = GameObject.FindGameObjectsWithTag("Minion");
Vector3 shortestDistaceToMinion = new Vector3(1000, 1000, 1000);
for (int i = 0; i < minions.Length; i++)
{
Vector3 distanceToMinion = transform.position - minions[i].transform.position;
if (distanceToMinion.magnitude < shortestDistaceToMinion.magnitude)
{
shortestDistaceToMinion = transform.position - minions[i].transform.position;
target = minions[i];
}
}
Sorry, but there is already an accepted answer which does the same thing better. It
- stores the shortest distance so far as a float not a vector (because that's what it is, and because it means you don't have to recalculate that shortest distance so far every time you go through the loop)
- initialises it to its largest possible value (more robust)
- compares square magnitude ins$$anonymous$$d of magnitude (more efficient)
You would be advised to alter your function so it looks more like the one above
Thanks for your feedback but as I said, it works for me. Sometimes people who are still learning appreciate a simpler breakdown of a thing and care less about efficiency and robustness. You would be advised to keep that in $$anonymous$$d or better yet, don't discourage people who are trying to help.
Sorry but on this site, you have to expect your answers to be critiqued.
Your version is not simpler, it's more complex. And what people who are still learning need are explanations of why one solution is better than another, which is what I provided.
can you send Definition of $$anonymous$$ions and target???
studiomilk_ can you send Definition of $$anonymous$$ions and shortestDistaceTo$$anonymous$$inion and target ??
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# Creating a 2d boundary 1 Answer