Follow Nearest Target

I want my missle when launched to fallow nearest target (enemy)

I thought about somehow using Ray but Iam not sure how to get to nearest one.

Any sugestions ?

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;
}