Search Unity

Is it possible to disable a particular collision?

Discussion in 'Scripting' started by BobFinery, Nov 25, 2014.

  1. BobFinery

    BobFinery

    Joined:
    Oct 14, 2014
    Posts:
    46
    Hi,

    I have objects that use rigidbody2d and they have colliders on them, say these are in layer "Foreground".

    What I am after is for the colliders to collide with objects in layer "Background" but if they collide with another "Foreground" object I want to apply a repelling force rather than have the physics handle the collision.

    Using layers the collision between two foreground objects is ignored therefore I get no OnCollisionEnter2D calls etc

    I guess I could duplicate the colliders, having one setup as a trigger but this seems messy.

    Cheers,
    Bob
     
  2. Stoven

    Stoven

    Joined:
    Jul 28, 2014
    Posts:
    171
    If you don't want to let the Physics engine handle the collision, disable the Collision matrix assignment for the Colliders and consider using Raycasts instead for those Collider types to get the behavior you want.

    In terms of answering the question of the topic title, you can disable Collisions during gameplay by using Physics.IgnoreCollision and Physics.IgnoreLayerCollision. I have no idea if setting the ignore to false will cause a new Matrix assignment for the Physics engine. I haven't tried it out yet, but it would be pretty interesting if you could as it would open up the potential of tons more collision assignments.
     
  3. BobFinery

    BobFinery

    Joined:
    Oct 14, 2014
    Posts:
    46
    I managed to do what I wanted by seperating the foreground and background colliders and attaching them as two child objects. One is a trigger that uses a foreground layer filter with the other as a collider with a background filter.
    Seems a bit messy though, it would be much simpler if we could just say "on collision enter - if (whatever reason) remove this collision from physics"