Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing colliders on the same rig from colliding with each other. But allowing them to collide with other rigs.

I have a prefab NPC which has a physics rig attached to it (to do some specific rag-doll stuff with). I need to avoid the various colliders on the same rig (arms, legs etc) from colliding with each other, but they have to be able to collide with the rigs of other instantiated NPCs.

Is there a way to do this? I know I can avoid all the colliders from colliding by putting them on a separate layer, but I cant create a new layer for every NPC.

Thanks

like image 870
Chris Headleand Avatar asked Dec 01 '25 00:12

Chris Headleand


2 Answers

You can do by setting up IgnoreCollision on your NPC class if you have any

http://docs.unity3d.com/ScriptReference/Physics.IgnoreCollision.html

So simple loop through all colliders in the rig and set up to ignore each other

void Start() {

    colliders = GetComponentsInChildren<Collider>();
    foreach(Collider collider in colliders) {

        otherColliders = GetComponentsInChildren<Collider>();
        foreach(Collider otherColider in otherColliders) {

            if (collider != otherColider) {
                Physics.IgnoreCollision(collider, otherColider);
            }
        }
    }
}
like image 73
Greg Lukosek Avatar answered Dec 04 '25 20:12

Greg Lukosek


It looks like the only way to ignore collisions without using layers is to use Physics.IgnoreCollision() between a pair of colliders, for each pair.

You can write some code that would automatically register a newly instantiated game object and create these pairs between the new object and the other ones that were registered before, so you wouldn't need to call this method for each pair yourself.

Or, you can use this code that does that for you :) It has its own representation of layer to control how the objects should ignore each other.

like image 30
Roberto Avatar answered Dec 04 '25 20:12

Roberto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!