Collider ******** Summary ======= A **Collider** is a component that defines the physical boundaries of a GameObject in the scene. Thus, Colliders define the shape of an object for the physics engine, so without one, there's no collision detection or response, the object is **non-blocking**. This means that other objects — including the player — can pass through it as if it doesn't exist physically. Here are some typical configurations using a collider: .. hint:: * Non-Blocking * Other objects can pass through * Useful for: decoration or props that are not in the player's way * Component: **No collider** * Function: ⌀ * Non-Blocking with Trigger * Other objects can pass through, - but the collider detects when a GameObject passes through * Useful for: the finish line in a car race, a trap in a cave * Set: **Collider + Trigger** * Function: void OnTriggerEnter(Collider other) * Blocking * Other objects can bump into it - remains immobile * Useful for: walls, floor * Set: **Collider** * Function: None Colliders Shape =============== Setup ----- * Navigate to Assets > Scenes * Create a new folder named "PhysX" * Create a new scene called "Colliders" * Double click to open it * Create a Sphere and assign it a color .. image:: sphere.png :scale: 40% * Select the sphere * In the Inspector, notice that a Collider is already present .. image:: spherecollider.png :scale: 40% Sphere collider --------------- * Uncheck the checkbox of the *Mesh Renderer* * Notice that the collider of the sphere is now visible on the screen .. image:: spherecol.png :scale: 50% * Activate the sphere *Mesh Renderer* Other colliders --------------- We introduce the colliders of the other standard 3D objects: .. image:: otherscolliders.png :scale: 30% +-----------+-------------------+ | Shape | Collider Type | +===========+===================+ | Sphere | Sphere Collider | +-----------+-------------------+ | Capsule | Capsule Collider | +-----------+-------------------+ | Cylinder | Capsule Collider | +-----------+-------------------+ | Cube | Box Collider | +-----------+-------------------+ Two remarks: * All basic 3D objects are delivered with a collider * The cylinder uses a capsule collider (Lower computational cost) Collider ======== Scene Setup ----------- * Navigate to Assets > Scenes > PhysX * Create a new Scene named *Detection* * Create a sphere, assign it a color and a RigidBody with gravity * Move the sphere upwards Collider Setup -------------- * In the Hierarchy, right click and select **Create Empty** gameobject * Rename the object "BCollider" * At the bottom of the Inspector, press the *Add Component* button .. image:: add.png :scale: 50% * Select **Box Collider** .. image:: box.png :scale: 50% .. image:: scene1.png :scale: 35% * In the Inspector, activate the **Is Trigger** option .. image:: istri.png :scale: 50% * In the Inspector, click on the **Edit Collider** icon: .. image:: edit.png :scale: 40% * Use the face gizmos to * Enlarge the box * Make it larger and thicker than the sphere .. image:: enlarge.gif :scale: 70% Collider script setup --------------------- A collider trigger provides multiple functions to handle collisions: .. image:: onmethod.png :scale: 40% * In the PhysX folder, create a new script named "Colliz" * Select the GameObject *BCollider* * Drag and drop the script into the Inspector .. image:: script.png :scale: 50% * Copy paste the following code into the script .. code-block:: csharp using UnityEngine; public class Colliz : MonoBehaviour { public Material Mat_in,Mat_out; void OnTriggerEnter(Collider other) { Renderer objectRenderer = other.gameObject.GetComponent(); objectRenderer.material = Mat_in; } void OnTriggerExit(Collider other) { Renderer objectRenderer = other.gameObject.GetComponent(); objectRenderer.material = Mat_out; } } * Drag and drop two Solid Color Materials into the Inspector .. image:: plum.gif .. warning:: By reading the script, you can see that we change the sphere’s color via its Collider, using the reference named *other*. But why not place this script directly on the sphere? Because any object that activates the trigger option of its collider becomes a non-blocking object and that’s not the behavior we want for a 3D sphere. Display collider ---------------- It is possible to display the collider in Play mode: * Select the Game window (by clicking on the tab, not by activating Play mode) * On the right of the Game tab, click on *Gizmos* to show the collider during Play mode * In the Hierarchy, select the BCollider .. image:: giz.png :scale: 60% .. image:: wire.png :scale: 60% Test ---- * Enter Play mode: .. image:: fallll.gif :scale: 60%