Calculate Distance Between two Game objects

Hello
I am here finding group of sphere of same tag…

For that oncollision , i have got array of all the sphere which are in contact of collided sphere…

Method i have used : var hitColliders = Physics.OverlapSphere(collision.transform.position, 0.6);

By this method , I am getting group of sphere connected to each other.
But i want to find the closest sphere among this array. So i need to calculate the distance between bullet sphere and all the game objects within this array.
From that i want to find minimun distance object.

But i am not getting the way to calculate distance??
Please Help me out. Thanks in advance for ur support and help…

type "Distance" in t the documentation

1 Answer

1

vector3.distance(sphere1.transform.position,sphere2.transform.positon)

mark as answered and have a nice day

Oh Thanks.. :-)

I ummarked it as answer for two reasons, first it is not vector3.distance but Vector3.Distance(). Second because Distance is to be avoided if you want to optimize your game (which you should want to). Distance is based on square root which is a recursive function and can slow down if used every frame on many objects. Instead you may use: Vector3 distanceSqr = (target.position - player.position).sqrMagnitude; if(distanceSqr < rangeSqr) //Within range