How do I check specific collisions between colliders.

Hi guys,

I know this question gets posted a lot, but it never gets the answer I need. People always go on about using OnCollisionEnter, OnTriggerEnter and the others, but that doesn’t solve my issue.

I need to be able to take two colliders and check whether or not they are colliding arbitrarily. I can’t encapsulate the functionality I need inside the OnCollisionStay function or any of the others. I simply want a way of querying;

if (collider.isCollidingWith(otherCollider)) //perform action

This isn’t something I want to happen EVERY TIME two objects collide - it’s just going to happen every now and then. As such; I don’t want to put it in OnCollisionEnter or any of the others because it may end up being expensive.

Any help would be appreciated, thanks.

Hello,

The thing is, a collision is a single event in time. It might happen every FixedUpdate, but it is still a single event. Unless you write your own collision solver the only way you are able to check for collisions is with OnCollisionEnter, OnCollisionStay and OnCollisionExit. If you do write your own collision solver, note that it will most definately be slower than anything built into Unity :slight_smile:

I don’t know where you got that from but these functions have no abnormally high overhead that I know of? To replicate the behaviour you’re describing (collider.isCollidingWith(otherCollider)), you can simply do:

private var collisions:Dictionary<Collider, boolean>;

function Awake() {
    collisions = new Dictionary<Collider, boolean>();
}

function OnCollisionEnter(collision:Collision) {
    collisions[collision.collider] = true;
}

function OnCollisionExit(collision:Collision) {
    collisions[collision.collider] = false;
}

function isCollidingWith(collider:Collider) {
    return (collider in collisions) && collisions[collider];
}

Note that this code is untested, but the consept remains.

Hope this helps,
Benproductions1