The first example uses Vector3.Distance
one that requires a rather expensive operation Sqrt
, and the second uses code that I would prefer to use in favor of a simpler LINQ form.
Here is an excerpt from the API documentation for Unity Scripting for sqrMagnitude
:
v Mathf.Sqrt(Vector3.Dot(v, v))
. Sqrt
, . - Sqrt
. , , .
, , sqrMagnitude
... Sqrt
- , , , .
:
GameObject FindClosestTarget(string trgt)
{
Vector3 position = transform.position;
return GameObject.FinndGameObjectsWithTag(trgt)
.OrderBy(o => (o.transform.position - position).sqrMagnitude)
.FirstOrDefault();
}
... ( ) LINQ Sqrt
, .
, , , , , . # .
, , sqrMaginitude
, Sqrt
.