Search code examples
c#unity-game-enginecollision

Physics ignore to avoid collision


I am aware of using Physics.Collision to avoid collision between two certain objects. But I want their box colliders to be active so that it detects that they are in contact. Is there a way to achieve this?

Also after I have disabled collision using Physics.Collision(), can I reactivate the collision between the objects?

Physics.IgnoreCollision(obj2.GetComponent<Collider>(), this.GetComponent<Collider>());

Solution

  • See Physics.IgnoreCollision

    ignore: Whether or not the collisions between the two colliders should be ignored or not.

    Per default it is true.

    To re-enable collisions you previously disabled simply pass in false.

    Physics.IgnoreCollision(obj2.GetComponent<Collider>(), this.GetComponent<Collider>(), false);
    

    If you don't want the objects to collide but register if they touch each other then make on of them a Trigger.

    If the object is a trigger then it doesn't react to the collision but it fires an OnTriggerEnter.

    See Colliders -> Triggers

    The scripting system can detect when collisions occur and initiate actions using the OnCollisionEnter function. However, you can also use the physics engine simply to detect when one collider enters the space of another without creating a collision. A collider configured as a Trigger (using the Is Trigger property) does not behave as a solid object and will simply allow other colliders to pass through. When a collider enters its space, a trigger will call the OnTriggerEnter function on the trigger object’s scripts .