- Home /
Is there a way to explicitly check for collision?
I have gameObjects in my scene that need to be aware of whether they are colliding at startup (for inititalisation) with other gameObjects or not. Since they can´t have a rigidbody all the time I wanted to add a rigidbody to each gameObject in Start(), check whether they collide with something, store that information and remove the rigidbody again.
Now I wonder if there is a way to force a collision check for a gameObject at a specific place in code.
Anyone have a clue if that is possible?
Answer by nullstar · Feb 20, 2013 at 09:43 AM
Unfortunately this isn't possible since Unity's wrapper around PhysX is retardedly restrictive. Using Unity functionality you could approximate a collision check using raycasts or spherecasts. If you need accurate collision though you've no option but to implement your own collision detection routines.
Hmmmm, okay. I was afraid this would be the answer. It´s pretty annoying and leads to really crazy workarounds.
Then how can I be certain that all objects have handled all their collisions? Lets say there are 1000 gameobjects intersecting with each other. How can I know that they all have handled their OnCollisionEnter() for all of those intersections? Is there a timeframe I can rely on?
It´s pretty silly that the code is actually there in the Unity system, but developers can just not access it due to a lack of an API for it. :-(
Unity physics runs in sync with $$anonymous$$onobehaviour.FixedUpdate. If you add all your objects during one FixedUpdate call then the next time that FixedUpdate is called all the initial contacts should have been processed.
You could use something like this on each object:
static var collisionCount : int;
private var collided : boolean = false;
function OnCollisionEnter () {
Collide();
}
function OnCollisionStay () {
Collide();
}
function Collide () {
if (collided) return;
collisionCount++;
if (collisionCount>=1000)
Debug.Log(collisionCount+" collisions has occured!");
collided = true;
}
Your answer
Follow this Question
Related Questions
Check if object is applying force to another object 0 Answers
Creating a teleportation gun 1 Answer
Horizontal gravity on One gameobject 2 Answers
Creating a retractable grappling hook 1 Answer
Get force applied on a gameobject 2 Answers