- Home /
Get the collider that exits from an OnCollisionExit
I have a robot with several parts. The root object has a Rigidbody and a Robot script, each child has a BoxCollider and some children has a Tread script.
I want to know when the treads are in contact with something. The events fire and everythings seems fine except i dont get any contact points OnCollisionExit and i cant find any other way to get which specific collider that exits. All i have OnCollisionExit is MyRobot and the object i exited from but i need the object with the actual collider such as somePart1 or tread2.
If i place OnCollisionExit in the Tread script it does not fire when it contacts the ground since neither of them have a Rigidbody.
My object hierarchy and what scripts they have:
MyRobot (Rigidbody, Robot)
somePart1 (BoxCollider)
somePart2 (BoxCollider)
somePart3 (BoxCollider)
tread1 (BoxCollider, Tread)
tread2 (BoxCollider, Tread)
And my Tread script looks like this:
public class Tread : MonoBehaviour
{
public bool isInContact = false;
public void Move()
{
if (isInContact)
r.AddForceAtPosition(/* stuff */);
}
}
My Robot script looks like this:
public class Robot : MonoBehaviour
{
private void OnCollisionEnter(Collision collision)
{
setContact(collision, true);
}
private void OnCollisionStay(Collision collision)
{
setContact(collision, true);
}
private void OnCollisionExit(Collision collision)
{
setContact(collision, false);
}
private void setContact(Collision collision, bool value)
{
foreach (var item in collision.contacts)
{
Tread tread = item.thisCollider.GetComponent<Tread>();
if (tread != null)
tread.isInContact = value;
}
}
}
Answer by FortisVenaliter · Sep 19, 2017 at 10:20 PM
Yeah, the treads need their own rigidbodies if you want to track collision on them separately. Otherwise they are treated like they are the same object.
I recommend putting Rigidbodies on the treads with FixedJoints to hold them in place to the parent Rigidbody. Then you can call your collision code from the Tread class.
This also has the added effect that there is a little give between the part making the construction not completely rigid
Answer by Brandon529 · Apr 20, 2018 at 05:57 PM
I have been trying to figure this out for almost 2 months and i finally FIGURED IT OUT. OnCollisionExit is fired on the frame AFTER you exit the collision, so there is no contact information. however you can store your collision by creating a new variable in your script as a Collision.
public Collision LastCollision;
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){ setContact(collision, true); LastCollision = collision; }
After you've stored your last contact point you can use it on OnCollisionExit as your collision 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(){ setContact(lastCollision, false); }
Hope this helps anyone out there having the same problems, and remember, there's always a way. There has to be.