- Home /
Are Unity distances that small?
Hi, recently I've created this two methods for returning the closest and furthest object of a given list:
private T GetFurthest<T>(List<T> players) where T : MonoBehaviour
{
float furthestDistance = 0;
T furthestPlayer = null;
foreach (var player in players)
{
if (player != this)
{
float objectDistance = Vector3.Distance(gameObject.transform.position, player.transform.position);
if (objectDistance > furthestDistance)
{
furthestPlayer = player;
furthestDistance = objectDistance;
}
}
}
return furthestPlayer;
}
private T GetClosest<T>(List<T> players) where T : MonoBehaviour
{
float closestDistance = Mathf.Infinity;
T closestPlayer = null;
foreach (var player in players)
{
if (player != this)
{
float objectDistance = Vector3.Distance(gameObject.transform.position, player.transform.position);
Debug.Log(objectDistance);
if (objectDistance < closestDistance)
{
closestPlayer = player;
closestDistance = objectDistance;
}
}
}
return closestPlayer;
}
The methods do well their jobs of returning the closest and furthest however I tried to use them to return the distance from the closest and furthest and the returned me values with exponetials of -5, while being the objects clearly separated more than one unit.
Are Unity distances that small? Is my method reducing the distances anywhere?
Answer by unity_ek98vnTRplGj8Q · Feb 22, 2021 at 08:13 PM
Unity distances are arbitrary, in other words you could make a game where 1 Unity Unit = 1 meter or you could make a game where 1 Unity unit = 1 kilometer, it really depends on how your models are scaled, although most 3rd party models you find will be scaled to either 1 unit / meter or 1 unit / foot, as far as I can tell.
That being said, 1e-5 is very small for a distance between 2 objects. Have you scaled down your scene significantly? Are you sure that the object doing the distance search is not returning the distance to itself? (I see you have the line that checks but are you sure that you have implemented that correctly? Is this code in your Player script?)