- Home /
Best way to detect if an object is near?
What is the best (fastest, most efficient) way to simply check if there is an object above another object in Unity?
Answer by Joshua · Jun 25, 2011 at 01:55 AM
It depends. Let's call the first object, with the script attached to it, gameObject a. If you know beforehand what the other object is you could simply use Vector3.Distance( transform.position, otherObject.transform.position). This only works if you know what specific object you're checking against, for instance in the case where an enemy has to find the distance to the player.
If you want to check if 'any' object is near you could do two things. Either use a trigger to see when it becomes near and save a reference to this object or use Physics.OverlapSphere to find all objects within a certain range.
I think that in your case an overlapsphere is the way to go, but it's hard to know without you being more specific.
Your answer
Follow this Question
Related Questions
[CODE]Keep Gameobject position exactly at mouse cursor when cast float position to int 1 Answer
postion scale and rotation greyed out and not working 1 Answer
Simultaneously moving GameObjects using C# script. 3 Answers
Slide GUITexture/GameObject into view? 1 Answer
transform.position error 1 Answer