What is the best (fastest, most efficient) way to simply check if there is an object above another object in Unity?
1 Answer
1It 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.
Can you put the relevant code? (Edit your question to add your script, don't forget to format it using the 101010 button)
– Hellium
how can I do that ?
– goddatrif you have visual studio, and the unity module to it, if enter play mode, in VS there should be a "attach to Unity" button..
– Zetya96