- Home /
Physics.OverlapSphere returns a strange "centerSphere" collider?
Hello, i want to trace some possible "snaps" in my code, and after some tests i decided to use OverlapSphere instead of OnTriggerEnter, which was killing performance. what is also good, OverlapSphere doesn't need to have any rigid bodies attached to any of the script's gameObjects.
so, as seen in this image, i'm using small sphere colliders that are in the available slots, and then each one has a script that does a Physics.OverlapSphere every one second or so, to avoid running it every frame.
and even though the code runs fine, sometimes it crashes and when i print the name of the "traced collider" i get the name of "centerSphere". And after searching, and knowing that i used such a name nowhere in my code, didn't even find anything on google about such a thing, i came here
anyone have the slightest idea what that might be?
here's also some most critical part of the code that prints this:
function detectAutoConnections(){
var hitColliders = Physics.OverlapSphere(transform.position, coll.radius, 11);
for (var i = 0; i < hitColliders.Length; i++){
Debug.Log(gameObject.name + " - " + hitColliders[i].gameObject.name);
}
}
i invoke this function once i enable these small spheres
InvokeRepeating("detectAutoConnections", 0.0, 1.0); //start detecting auto-connections immediately, every 1 seconds
i get "autoConnector - centerSphere" autoConnector is correct and good, centerSphere-i have no idea... i'm filtering collisions on layer 11, and only these little sphere colliders belong to it. so i don't see why it finds this strange centerSphere object..?
A couple of thoughts. Layers are masks, not integers. An easy way to handle the mask is to declare a variable of type Layer$$anonymous$$ask and then set it in the inspector:
var mask : Layer$$anonymous$$ask = 0;
You you could write a bit of code that finds the game object called 'centerSphere' and makes something happen...moves it to the center of the screen, flashes it, something...or simply search for the name in the hierarchy at runtime.
Also note that OverlapSphere() does not check the spheres. It, "Currently this only checks against the bounding volumes of the colliders not against the actual colliders."
Thank you mate! Your idea for figuring out thhe centerSphere object was good :) it was an object i created for my movement/rotation gizmo, and working with unity 10 hours a day can make your $$anonymous$$d go crazy :D
about the layer masks, i'm using the layer manager to set my layers and assign gameObjects to them. like the image below:
and then in the code i do something like
myGameObject.layer = 11;
is this the wrong way of using the layers in the overlapsphere function?
agree, it should be converted to answer and i'll tick it right to at least get it out of the unanswered list :)
Answer by robertbu · Sep 17, 2013 at 09:25 AM
The problem is in this line here:
Physics.OverlapSphere(transform.position, coll.radius, 11);
The '11' should be '1 << 11'.
As mentioned above, it is easier to specify:
var mask : LayerMask = 0;
Physics.OverlapSphere(transform.position, coll.radius, mask);
Then you set 'mask' to layer 11 in the inspector.
Note, given what you are doing, it might be easier to just maintain a set of points and use distance to do your calculation. You could use a set of empty game objects with tags to find the positions.
Thank you for this very useful information about layer masks, it's something i wasn't aware of :)
so you say there's a faster and even easier way than using overlapsphere to trace distances between these points? the problem is, there would be about 2000 of them (moving, so i can't really maintain sets of points?), and i can't just go around and calculate distances between them all, wouldn't that kill performance? something in my guts tells me overlapsphere is the most efficient way of doing it... but i'd happily try other ideas that could make it more effectient too!
If OverlapSphere() checks against the bounding box as advertised, then checking distance (or squared distance) is likely to be more efficient. Ins$$anonymous$$d of the colliders you have now, place an empty game object at the position the colliders now hold. You can use GameObject.FindGameObjectsWithTag() and then compare the distance between the points. Note that with OverlapSphere() your code may be simpler, but Unity is still having to do the processing under the hood.
This assumes you need more performance. What you have now may be good enough.
i fear the performance will never be enough, there will be a day when someone tries to make a complex build (it's a lego style toy) that would go to up to 10000 traces of distances between the connection points... so you say i remove my colliders and forget the whole overlapSphere thing, and try to manually check the distances between all of them :)