- Home /
Checking for Collisions Before GameObject Creation
Suppose that I want to create an object (or set of objects) only if the space they would occupy does not contain any other objects. How can I check for this?
(I'm aware of Physics.OverlapSphere(), but that's only any use if the GameObjects are all spheres, or at least have spherical colliders, which is not in general the case.)
Where does it say that OverlapSphere() requires the game objects to have spherical colliders? (According to the docs at least, the colliders aren't considered at all; rather, the bounding volume for the collider is used.)
The object I'm creating has to be spherical, otherwise the check won't match what I'm trying to test!
Answer by _Petroz · Dec 17, 2010 at 10:09 PM
I think the best way to do it would be to have an invisible instance which you position at the location, and check if that invisible instance is colliding. If that instance isn't colliding then create a real instance. That allows you to create any shape/size collider you need.
You'd have to wait until the next physics frame to get the results, though, and it would potentially affect the other objects in the scene, which may or may not be acceptable depending on what you're doing.
Unfortunately the "affecting other objects" part isn't acceptable. (A one physics cycle delay is fine, though.)
Couldn't you make it a trigger so it doesn't affect other objects?
Answer by netatti · May 12, 2011 at 01:50 PM
I had a similar problem and resolved it using this
Physics.OverlapSphere(position, radius);
That'll return an array of Colliders that are intersecting or touching a Sphere at position with the given radius.
Other functions (like Physics.CapsuleCastAll) also sound very promising, but didn't work out for me. For some reason they didn't return collisions properely.
hth
As mentioned in the original post, that only works for checking a sphere, so isn't very useful to me.
oh, sorry for not reading carefully. In my case I'm respawning a human character, which is also not spherical. But I set the radius large enough to avoid collisions most likely and I'm fine with that.
Answer by alior · Nov 18, 2016 at 07:44 AM
Instantiate prefab but make all collider triggers. Check for trigger state. .... Profit!
Your answer
Follow this Question
Related Questions
Prevent rigidbody being affected by character controller 2 Answers
Rigidbodies won't collide if mass difference is too high 0 Answers
Detailed terrain mesh collision 1 Answer
Problem with custom raycast2D collision detection system. 1 Answer
Creates not colliding with anything besides character controller when made rigid (in AngryBots) 1 Answer