I have an object that has a front collider, left collider and right collider. When a collision occurs, I notice that there are situations where the front collider and left collider/right collider has collided with the object. I need to somehow figure out which collider made contact "first". Is there anyway to do this? The reason I need this is because I have different code that needs to run depending on which collider made contact first.
3 Answers
You can always use OnColliderEnter (or OnCollisionEnter2D), and store the first collision. So consider something like this:
Collider FirstCollider;
int CollisionCount;
void OnCollisionEnter(Collider collider)
{
if (FirstCollider == null)
FirstCollider = collider;
CollisionCount++;
}
void OnCollisionExit(Collider collider)
{
CollisionCount--;
if (CollisionCount == 0)
FirstCollider = null;
}
You would want to make sure none of your colliders are colliding before you set FirstCollider back to null.
I haven't tested it, but I figure it should work.
-
\$\begingroup\$ I'm using OnCollisionEnter() but the entries in the contacts array is not in any specific order so I can't really make use of it. \$\endgroup\$ Commented Jan 29, 2016 at 20:19
Actually and hopefully you did attach all three colliders on same gameObject. What you have to do is create 3 children gameObject and attach one collider to one child. In that way you can detect what you want.
Attach a Jon's script to each collider then in my OnCollisionEnter you can from which collider collision is occured through tag, any attribute or even with gameObject's name. Like,
void OnCollisionEnter( Collision col)
{
Debug.Log("Collided with: " + name);
}
Update:
If your only problem is to avoid your own colliders then one of the many solutions is to assign a separate layer
for your character for example layer name is Player
.
- Add layer as
- Assign it to parent and children.
- Goto Edit -> Project Settings -> Physics, in collision matrix, remove check from Player to Player like,
In this way your player colliders will never fire any event on their own collision.
-
\$\begingroup\$ I've have child objects for Front, Top, Bottom, Left and Right colliders. Each object has a box collider on it. The parent object has no colliders but a rigidbody. When OnCollisionEnter() is called, if I loop through the collision.contacts array I will see that sometimes the Front and Right/Left colliders are in contact. This is the problem as I don't know which one is first. \$\endgroup\$ Commented Jan 29, 2016 at 20:18
-
-
\$\begingroup\$ My colliders don't collide with each other. The problem is that I have multiple colliders colliding with a single object and I can't determine which collider made contact "first". \$\endgroup\$ Commented Jan 29, 2016 at 22:10
-
\$\begingroup\$ For example, I have a front collider that extends out from the character and a collider on the right that doesn't extend as far. When the character collides with an object head on the OnCollisionEnter() method fires and says that both the Front collider and the Right collider have entered collision. If the object is directly to my right and I move in to it the OnCollisionEnter() method also fires and says the Front collider and Right collider have entered collision with the object. I'm not sure how to differentiate what situation caused the collision to occur. \$\endgroup\$ Commented Jan 29, 2016 at 22:10
I'd just like to add, for completeness, that you could always try using a single collider for your GameObject, then infer what side it was on from the collision point.
You can access the point like so:
void OnCollisionEnter(Collision collision) {
foreach(var contact in collision.contacts) {
Vector3 pointOfImpact = contact.point;
}
}