Physics

Summary

A Collider is a component that defines the physical boundaries of a GameObject. This way, to detect the collision between two GameObjects, each of them must have a Collider.

In Unity, colliders can function in two different ways:

  • Normal Mode: the object physically interacts with other objects

    • Collider only

      • Property: No movement, static object

      • Useful for: walls, floor

    • Collider + RigidBody

      • Property: Moves and responds to gravity or impulses

      • Useful for: domino Challenge, rolling ball

      • Function: void OnCollisionEnter(Collision collision)

  • Trigger Mode: the object does not block others; no physical interaction => Ghost object

    • Collider + Trigger

      • Property: detects when a GameObject passes through

      • Useful for: the finish line in a car race, a trap in a cave

      • Function: void OnTriggerEnter(Collider other)

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

    ../_images/sphere.png
  • Select the sphere

  • In the Inspector, notice that a Collider is already present

    ../_images/spherecollider.png
  • Uncheck the checkbox of the Mesh Renderer

  • Notice that the collider of the sphere is now visible on the screen

    ../_images/spherecol.png
  • Activate the sphere Mesh Renderer

The Collider of the sphere perfectly matches its shape. Here are the colliders of other objects:

../_images/otherscolliders.png
  • Sphere: sphere collider

  • Capsule: capsule collider

  • Cylinder: capsule collider

  • Cube: box collider

Two remarks:

  • All basic 3D objects are delivered with a built-in collider

  • The cylinder uses a capsule collider (Lower computational cost)

Avertissement

We recommend not using a plane because it is defined by 200 triangles. Testing its collision is equivalent in complexity to testing the collision with 200/6 = 33 cubes. As a consequence, use a cube to create a floor.

RigidBody

Basic setup

We begin our study by adding a RigidBody to the sphere.

  • Create a cube and and assign it a color

    • Scale the cube to create a ground

  • Select the sphere

    • In the Inspector, click on the button Add Component

    • Select RigidBody

    • In the RigidBody section, check: Use Gravity

    ../_images/usegravity.png ../_images/fall.png
  • If you want the camera to shoot the same view as the scene view

    • In the Hierarchy, select the camera

    • Right click and select Align With View

  • Enter Play mode

    ../_images/fall.gif

What happened?

  • The sphere has a Rigidbody, moreover it is affected by gravity so it falls

  • The ground don’t have a Rigidbody, it’s a static object, it cant move

  • Both the sphere and the ground have a Collider, allowing them to come into contact, which stops the sphere’s fall

Simulation

We add some new elements to the scene; try to reproduce the scene below.

Some hints:

  • Analyze which object has a RigidBody: is the object moving or not?

  • Detect which object is affected by gravity: it is falling or not ?

  • Enable all options correctly

../_images/bascule.png ../_images/vidbascule.gif

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

    • Rename the object « BCollider »

    • In the Inspector, press the Add Component button and select Box Collider

    ../_images/scene11.png
    • In the Inspector, activate the Is Trigger option

    ../_images/istri.png
    • In the Inspector, click on the Edit Collider icon:

    ../_images/edit.png
    • Use the face gizmos to

      • Enlarge the box

      • Make it thicker than the sphere

    ../_images/enlarge.gif

Collider script setup

A collider trigger provides multiple functions to handle collisions:

../_images/onmethod.png
  • In the PhysX folder, create a new script named Colliz

  • Select the GameObject BCollider

  • Drag and drop the script into the Inspector

    ../_images/script.png
  • Copy paste the following code into the script

using UnityEngine;

public class Colliz : MonoBehaviour
{
        public Material Mat_in,Mat_out;

        void OnTriggerEnter(Collider other)
        {
                Renderer objectRenderer = other.gameObject.GetComponent<Renderer>();
                objectRenderer.material = Mat_in;
        }

        void OnTriggerExit(Collider other)
        {
                Renderer objectRenderer = other.gameObject.GetComponent<Renderer>();
                objectRenderer.material = Mat_out;
        }
}
  • Drag and drop two Solid Color Materials into the Inspector

    ../_images/plum.gif

Avertissement

By reading the script, you can see that we change the sphere’s color through the Collider using the reference named other. Why not place this script directly on the sphere? We could, but that would require setting its Collider as a trigger, turning it into a ghost object and we don’t want that.

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

../_images/giz.png ../_images/wire.png

Test

  • Enter Play mode:

    ../_images/fallll.gif