Search code examples
c#unity-game-enginegame-physicsmesh-collider

Collisions only on specific objects, or alternate solution


Working in unity, trying to find a good way to make a collider only work for a specific object. Layer-matrix collision wont work here and Im also hesitant to manipulate the physics engine to ignore all other colliders.

The layer matrix wont work unless i can create layers on the fly, as this needs to work on an individual basis for an abstract number of game objects.

The use case here is a "floor" for a ragdoll system. I'm working on a 2d game from a top-down perspective, so generally there is no gravity. However, an actor may be "downed". In this case a series of rigidbodies are activated and will be affected by gravity on the Y axis. They need a floor, or they will fall forever. Obviously the issue here is that the floor will affect everything else in the world too.

Here are some images for reference Actor and floor: Editor view of actor and floor

Actor, downed enter image description here

Because there will be an indeterminate number of actors and corresponding floors, I need something that applies on a per-actor basis. I have not found any other forum post with this specific limitation.

I am at a total loss for how to accomplish this, given the limitations I've found elsewhere.

... I suppose my only other thought is to turn this into a trigger and home bake some rigidbody stuff to handle this behavior. But I don't want to go that far unless its the last resort.

Any and all suggestions welcome. Thanks.


Solution

  • While the other comments and answers led me to this solution, they didn't answer the question. But, in the interest of others facing this problem, here is my solution. I ended up utilizing

    Physics.IgnoreCollisions(Collider,Collider,bool)
    

    I set up an initial removal of all collisions in start up

    Collider2D[] allColliders = GameObject.FindObjectsOfType<Collider2D>();
    
            foreach (Collider2D collider in allColliders)
            {
                Physics2D.IgnoreCollision(groundCollider, collider, true);
            }
    

    And I collect the colliders i want to interface with later. I then call a functon to "un-ignore" those colliders

    void ChangeCollisionBehavior(bool on)
        {
            foreach (Collider2D bone in actorRagdollColliders)
            {
                Physics2D.IgnoreCollision(groundCollider, bone, !on);
            }
        }
    

    In this manner, you can create a situation in which a collider only collides with specific objects.