I’ve just ported my project from 4.6.2f1 to 5.0.0f4 for testing.
I have a GameObject with BoxCollider and a RigidBody. There’s a simple script on the GameObject which is supposed to detection collisions:
function OnCollisionExit(collisionInfo : Collision) {
Debug.Log(collisionInfo.collider.name);
Debug.Log(collisionInfo.contacts.Length);
}
When a collisoin happens, I can see the name of the collider, but contacts.Length always returns 0. So the collision is obviously happening, but I can’t figure out why no contact data is being generated. The above works flawlessly in Unity 4.6. Has anyone encountered something similar before?
Any help appreciated.
I should add, that I’ve changed absolutely no settings in Unity 5. A cursory look at the Physics inspector didn’t raise any red flags.
i Have been trying to figure this out for almost 2 months and i finally FIGURED IT OUT. you can store your contact point by creating a new variable in your script as a Contact Point.
public ContactPoint LastContactPoint;
And then you can store your last contact point in that variable from your OnCollisionEnter and OnCollisionStay functions like so
public void OnCollisionStay(Collision collision){ foreach(ContactPoint Contact in collision.contacts){ LastContactPoint = Contact; } }
After you’ve stored your last contact point you can use it on OnCollisionExit as your contact point and be able to get the contact information from your last contact(such as normals, impact velocity and all your contact point needs). You can do so even without referencing to your collision on an input variable unless you want to.
public void OnCollisionExit(){ if (Vector3.Angle(LastContactPoint.normal, Vector3.up) < MaxSlope) { Grounded = false; } }
Hope this helps anyone out there having the same problems, and remember, there’s always a way. There has to be.
This is problematic for one specific scenario.
Correct me if I’m wrong (no really, please, this is killing me!) but if I need to know which child object was the one which got hit I need to use the contactpoints because child objects do not fire the collision events so I can’t script anything there I have to do it on the parent… so to detect collisions of my objects children I’m using the following code
How would I achieve this with collisionExit ? to tell that these collisions are no longer active?
P.S: I’m currently achieving this by using OnCollisionStay and recording a timestamp per child, when the timestamp is more than a deltatime away the collision is over for that child object.