How do I find the nearest gameobject (or tag) and determine the distance between that object and the player?
clear explanation or C# only
How do I find the nearest gameobject (or tag) and determine the distance between that object and the player?
clear explanation or C# only
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:
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 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*.transform.position;*
if (distanceToMinion.magnitude < shortestDistaceToMinion.magnitude)
{
shortestDistaceToMinion = transform.position - minions*.transform.position;*
target = minions*;*
}
}
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 instead of magnitude (more efficient) You would be advised to alter your function so it looks more like the one above
– Bonfire-Boystudiomilk_ can you send Definition of minions and shortestDistaceToMinion and target ??
– hdihd9162
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.
– Adam_BenkoclosestEnemy = 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'
– bryanhatami27