- Home /
Manually check collision ?
Hi,
So simply I want to check if my object collides with anything in one run from instantiation to potential destruction if it does. Is it even possible? I need to do this with meshcollider in mind, so sphere check is not an option, same for delayed "OnCollisionEnter/Stay" and RigidBody.SweepTest(Vector3.zero, out hit) does nothing (maybe I'm doing something wrong there with SweepTest).
Is getting all the bounds and spamming intersections all the way the only solution?
According to SweepTest docs it won't work for $$anonymous$$esh Collider. As for the OnCollisionEnter should work as long the colliders are not triggers. Post some code for more help.
Well, yes, OnCollisionEnter works, but I wanted to make it check the collision just after instantiation so the code can decide what to do with object if it collides or not. $$anonymous$$y current solution is a little imperfect, as object decides what to do with itself some of new placement actions may be interrupted.
Code that I would like to have:
bool CreatePart (ConnectionPoint c, Vector3 pos, Vector3 dir) {
bool b = false;
if (c.exists) { //Ignore this
GameObject g = (GameObject) Instantiate(parts[id], pos, Quaternion.LookRotation(dir)); //Just instantiating the object
}
//And here is the problem, how do I check this collision? This bool is important later in the code and this is why OnCollision stuff doesn't work as solution
if (g && !g.collides) b = true;
return b;
}
bump, can't believe you can't force collision check somehow in the same scope you instantiate object...
Answer by PixelSpartan · Feb 14, 2017 at 04:30 PM
You can just raycast to distance 0 and see if it tells you if you are inside something but make sure to go to physics settings and enable raycast hits triggers so it will also work for triggers
Not gonna work, It's not as simple as e.g. player falling through the floor, Im placing objects which have less or more advanced shapes and I need to know If new object will collide with existing ones considering high accuracy. Delayed OnCollisionEnter works, but makes a lot of mess and may disturb actions that are being taken in the same time that object got instantiated.