I have an object that has multiple colliders and I’d like to execute code when only 1 of them collides with an object. How would I go about doing this? Colliders don’t seem to have a name, or an array I can access so I’m a little lost on how to do this.
(I’m not looking for an answer to basic collision detection, I’m looking for an answer to individualizing the colliders so I can control how different parts of my character interact with objects).
Each ContactPoint in contacts of the collision gives both colliders that hit. You cannot have two of the same type of collider on the game object, so you can use GetComponent() to get the specific collider you need. Say you want to detect hits on the BoxCollider. Untested example:
#pragma strict
private var boxCollider;
function Start() {
boxCollider = GetComponent(BoxCollider);
}
function OnCollisionEnter(collision : Collision) {
var myCollider = collision.contacts[0].thisCollider;
if (myCollider == boxCollider) {
Debug.Log("Hit the box collider");
}
}
Note I ran some test a long time ago that indicated that separate OnCollisionEnter() calls are made for each pair of colliders (this and other) that contacted during the frame. You may want to check this out yourself, but if true, you only have to check the first ContactPoint as I’ve done in the code above. If not true (or no longer true), then you will have to check all the contacts in each collision.