In most Unity projects, there will be a need to create physics interactions. Situations in which physics are needed would include any use of gravity, or where objects collide and react to one and other. In this tutorial, you will learn to work with Colliders and Triggers to control physical interactions.
In most Unity projects, there will be a need to create physics interactions. Situations in which physics are needed would include any use of gravity, or where objects collide and react to one another. Colliders are applied to GameObjects to represent their physical form in the physics simulation. By default, GameObjects with a Rigidbody component applied will be blocked (collided) by the Collider. While Colliders can block Rigidbodies, they can also be used as Triggers. When a collider is set as a Trigger, it detects external interactions from other game objects and executes code that is within a OnTriggerEnter, OnTriggerExit, or OnTriggerStay function within a script.
Select any 3D GameObject within the Scene
In the Inspector, click the Add Component button
Select the Physics category and choose the Collider that best represents the physical shape of the 3D GameObject (Fig. 01)
It’s important to remember that while Colliders are meant to represent the GameObjects physical properties, the preciseness of that representation can vary depending on the needs of the project. For example, avatars or characters may use a Capsule Collider. It is only meant to keep the avatar from falling through the floor or walking through a wall.
Triggers will not block Rigidbodies. They are meant to allow Rigidbodies to pass through them and execute code within OnTrigger events.
In the Hierarchy, click on Create > Create Empty to create an Empty GameObject
With Empty GameObject selected, click Add Component in the Inspector
Select Physics > Box Collider
Enable Is Trigger in the Box Collider component (Fig. 02)
Enable Edit Collider to modify the shape of the Trigger (Fig. 03)
Drag the various handles on each side of the Trigger to interactively modify its shape
Remember, Colliders will block other rigidbodies in the scene, but they can also be used to trigger events using scripts. When creating your levels, use a trigger to open doors, create environmental hazards, or whatever you can think of.