- Home /
Detect if game object is near a specific tag
How can I detect if my game object (player) is near an object with a specific tag?
I can detect single objects like this:
float distance = Vector3.Distance (plyr.transform.position, transform.position);
if (distance <= maxRange) {
GameObject.Find ("Action Text").guiText.text = "Press (E) to interact";
}
But what about multiple objects based on their tags?
you could always use triggers invisible cubes the player could detect
Answer by Briksins · Dec 17, 2013 at 12:54 PM
you can check against each of them in the loop, here is a logic
Use Unity FindGameObjectsWithTag
You will get array of object (obviously if they found)
check the distance from player to those objects
if distance in range of
Then user is close to particular object from all of those in array
But it is not good implementation as it is very compute intensive, as basically each update you will have to find all those objects, calculate distance to them compre if distance is in range of "close range" and blah blah blah
What you should do is to attach to each of your objects collider and trigger an event from exact object once it collide with Player you can set to player - "Player" tag and on collision event check if collided object is player then allow to player to do whatever you want with that object which triggers that is optimal solution
Thanks, I thought that objects with a standard collider can't have another trigger collider, that's why I tried it the hard way.
Your answer
![](https://koobas.hobune.stream/wayback/20220613124332im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Finding Nearest Object Both Positive And Negative? 1 Answer
Hexagon-Grid Distance 2 Answers
iTween MoveTo "Convoy" 0 Answers
grab all game objects with a distance of the object? 2 Answers
OnMouseOver Distance Measuring 1 Answer