- Home /
Ignore individual collisions between two objects
I want to ignore collisions between an object and a special class of objects only when the collision contact point meets a few criteria. In other words:
function OnCollisionEnter(other : Collision) {
if (isSpecial(other.gameObject) && meetsCriteria(other.contacts[0].point)) {
// Ignore this individual collision
}
}
However, I don't want to use Physics.IgnoreCollision(other.collider, collider)
because I want future collisions to still happen.
Any thoughts? I will probably have to default to some kind of hackery where I remember what colliders I've ignored and then reenable collisions later, but I'd like to have a cleaner and less hacky solution.
Thanks!
Answer by whydoidoit · Mar 21, 2013 at 01:09 PM
Well you could flag special game objects by giving them a script (with perhaps even nothing in it!)
if(other.gameObject.GetComponent<IsSpecial>())
{
}
Now as for meeting criteria - if these are fixed then you could obviously just code meetsCriteria - otherwise perhaps an event?
public class ContactPointCriteria
{
public ContactPoint point;
public bool meetsCriteria;
}
public event Action<ContactPointCriteria> CheckContactPoint = delegate {};
Then
var cpc = new ContactPointCriteria { point = other.contacts[0] };
CheckContactPoint(cpc);
if(other.gameObject.GetComponent<IsSpecial>() && cpc.meetsCriteria)
{
}
To wire up the event
someClassInstance.CheckContactPoint += HandleContact;
void HandleContact(ContactPointCriteria criteria)
{
criteria.meetsCriteria = true;//the result of some calculation
}
Or you could keep that same ContactPointCriteria and send or broadcast a message:
var cpc = new ContactPointCriteria { point = other.contacts[0] };
BroadcastMessage("CheckContact", cpc, SendMessageOptions.DontRequireReceiver);
if(other.gameObject.GetComponent<IsSpecial>() && cpc.meetsCriteria)
{
}
Then handling it on a suitable script would be
void CheckContact(ContactPointCriteria criteria)
{
criteria.meetsCriteria = true;//the result of some calculation
}
Um, checking whether the contact meets the criteria and whether the object meets certain criteria are both easy -- already implemented. What I don't know how to do is ignore the collision.
Oh you mean ignore in the physics? I get your point :)
Too late by the time you get an event I believe. But perhaps you could use IgnoreCollision and then reset it in a coroutine that was doing WaitForFixedUpdate perhaps (calling it again and passing false as the last parameter).