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>());
2 Answers
In the Unity menu bar, go to Edit > Project Settings, then select the Physics category to open the Physics window. You will find a Matrix and here you can uncheck the collision between 2 layers. If you put the Player in a layer and the object you don't want to collide into in another layer and in the matrix you uncheck the box, everything will maintain his properties and the player won't collide with it.
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.
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 .