Find closest object with a certain tag

Im trying to find the closest object from a gameobject to another gameobject with the tag “Blocks”. How would I do this?

2 Answers

2

Look at Physics.Spherecast in the docs.
It should return the first collider.
all you need to do is do a “If” to check the tag “Blocks”.

I think you misunderstand me :D I dont want it when it is collided I want it just in general

Physics.Spherecast doesn't check for a collision with player. it send a invisable sphere around your gameobject and collect information from all the gameobjects (if they have a colllider) inside the invisable Sphere. Its sounds more complicated than it is , Check out the Docs.

Try this: geting the closest object from a array - Questions & Answers - Unity Discussions

And use Physics.OverlapSphere to get all the colliders nearby. Then just use a simple if to check for the collider’s tag.