- Home /
How to do static collision checks?
I am looking for this sort of API
// Does collider A collide with collider B?
bool CheckCollision(Collider a, Collider b)
// Does Collider A collide with anything?
bool CheckCollisionAny(Collider a);
Instead of the typical OnCollisionEnter
, etc. callbacks. Why? Because many times in my AI code I need to "hallucinate" objects at different positions and see if they collide. This is essential for some algorithms. The Unity API already has a similar interface for raycasting. How can I implement this interface for arbitrary colliders?
Answer by Owen-Reynolds · Apr 14, 2015 at 02:36 AM
In general you can't do an instantanious collision check. You can fake it various ways.
OverlapSphere. Or a CapsuleCast straight down (or up,) or rigidbody.sweepTest for cubes (and maybe compound colliders, not sure.) But for *Cast and maybe SweepTest, you have to be careful with the "doesn't count thing you start inside of" rule. So may have to do things like starting from y=+100 and doing SweepTestAll, to be sure nothing is excluded.
Then you have to worry about objects not counting as in place until after a physics step, if they are also moving (the things you are casting against.)
Your answer
Follow this Question
Related Questions
Why there is no Collider.IsTouching(...) ? 2 Answers
Flying Controls Collision Issues 0 Answers
Collision with Plane fails 0 Answers
Player registering collisions where there are none 0 Answers