Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

find closest gameobject unity

GameObject FindClosestTarget(string trgt)
{
    Vector3 position = transform.position;
    return GameObject.FindGameObjectsWithTag(trgt)
        .OrderBy(o => (o.transform.position - position).sqrMagnitude)
        .FirstOrDefault();
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #find #closest #gameobject #unity
ADD COMMENT
Topic
Name
6+6 =