- Home /
Finding a GameObjects neighbors in a 3D space
I have a bunch of GameObjects in a scene. I need each GameObject to know the location and rotation of its neighbours (within a certain range). What is the best and fastest way to find the location and rotation of a GameObject's neighbours within a certain range? Can someone provide an example? Thanks in advance!
I understand your question, but what exactly are you trying to do? A bit more context would be appreciated
I simply want to be able to get the position and rotation of neighbooring objects and then manipulate them.
Answer by JC_SummitTech · Dec 07, 2016 at 11:54 PM
Physics.SphereCast centered on the core object to get a list of objects in range, then grab these object's transforms
Could you provide a simple example? (I am pretty new to this)
the API is here: https://docs.unity3d.com/ScriptReference/Physics.SphereCast.html
Spherecast basically shoots a sphere along a path and returns whether it hit anything. if you make the distance 0 then it will just return what was in the sphere at distance 0.
Just look at that link, it has a good example on how to use it and you can modify it for yourself.
Answer by lylat · Dec 08, 2016 at 08:12 AM
What you are looking for is :
Physics.OverLapSphere
https://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html
Basically creates a sphere collider for a single frame and checks to see if it collided with anything, you would need to make the objects you are checking against have colliders, but it is also possible to give them triggers if that is not what you want to do. (Detail in the docs on how to do this).
What @JC_SummitTech said is also viable.
You are right, this would be more appropriate than a SphereCast.