- Home /
Best Answer
Answer by DoktorAce · Nov 10, 2011 at 04:06 PM
I think you're going to have to loop through any potential target and figure out which one is closest. It would be even simpler if your targets were tagged, then you could do it like this in C#:
private GameObject GetClosestTarget( string targetTag )
{
GameObject closestTarget = null;
float closestDistance = 9999999.0F;
foreach( GameObject target in GameObject.FindGameObjectsWithTag( targetTag ) )
{
float dist = Vector3.Distance( transform.position, target.transform.position );
if( dist < closestDistance )
{
closestTarget = target;
closestDistance = dist;
}
}
return closestTarget;
}
Your answer
Follow this Question
Related Questions
Ray Cast Click on Enemy - Change Current Target 1 Answer
Raycast target sight? 0 Answers
How can I ground enemies and use target transform? 1 Answer
Hit enemy life with raycast 1 Answer
GUI Follow RaycastHit 2 Answers