Collision detection in Unity 3D

3D Game Design with Unity

🕑 This lesson will take about 24 minutes

In this lesson, you will learn about collision detection in Unity, different types of 3D colliders, different types of collision triggers and physics materials in a 3D game.

Collision

In a 3D game, a collision occurs when one object comes into contact with another object. In this lesson, we will look at collision detection, the Rigidbody component, colliders, triggers and physics materials. Scroll down to watch the video on all of these things or read on.

Rigidbody component

For objects to use Unity’s built-in physics engine, they need to include the Rigidbody component. To add it to a selected object, click Add component > Physics > Rigidbody from the Inspector panel.

The Rigidbody component gives you several properties to work with including:

  • Mass

  • Drag (the amount of air resistance applied to an object when it is moving – an object with higher mass will need more force to move and will stop at a faster rate)

  • Angular drag (air resistance that is applied when turning)

  • Use Gravity

  • Collision Detection, and so on…

Collision detection

In order for objects to detect collisions, they both need to use a component called a collider. A collider is a perimeter around an object that can detect when another object enters it. Colliders are often referred to as “hit boxes”.

Note: Objects don’t always need Rigidbody components to collide – all they need is a collider object. However, Rigidbody components are required when you want an object to be treated as a physical object and work with properties like gravity, mass etc., for example, to allow an object to fall or be moved around. And at least one of the 3D objects colliding will need a Rigidbody to trigger a collision – we’ll talk about that later.

Colliders

Objects like cubes, spheres and capsules already have collider components when they’re created. You can add a collider to an object by selecting the object and clicking Add component > Physics from the Inspector panel, then select the 3D collider shape you want such as box, sphere, capsule, wheel, terrain, or mesh (which takes the exact shape of a 3D model but can reduce game performance).

When a collider is added to a game object the collider will appear in the Inspector panel for that object. Colliders have a number of properties including

  • Is trigger (this will determine if the object will be treated like a physical object that can be hit, or if other objects can pass through it. If Is Trigger is selected, other objects will be able to enter and leave the collider)

  • Material (you can apply physic materials to change the friction and bounciness of an object – an object can behave like rubber, metal, wood, etc. by adjusting these properties)

  • Center (the centre of the collider

  • Size, and

  • Geometric properties eg. radius, if the collider is a sphere

You can try out different colliders with different shapes. For example, you can add a sphere collider to a cube so that it will roll around like a ball, and you can change the size of a collider so that the object either floats above other surfaces or sinks into other surfaces.

Watch the video below to see how you can work with the Rigidbody component, colliders, triggers and physic materials. Then scroll down to see more information on physic materials, triggers and the sample code.

Is YouTube blocked at school? Watch on Google Drive.

Physics materials

To create a physic material, click Assets > Create > Physics material. You can then modify properties to make the object behave like metal, wood, rubber etc. For example, you can increase bounciness to make it behave like rubber. Once you have created a physic material you can add it to a collider attached to an object from the Inspector panel. Watch the video above to see how.

Triggers

Triggers can call three different methods that allow you to program what a collision event will mean. The three trigger methods are:

  • OnTriggerEnter – this is called when another object enters the collider

  • OnTriggerStay – this is called when another object stays inside the collider

  • OnTriggerExit – this is called when another object exits the collider.

Triggers can be used to program what will happen if a player falls off a map, a player enters the water, or an enemy enters a perimeter, for example.

If you added the code below to a script (called CollisionScript) attached to a cube, it would display a message in the console whenever another object enters the cube:

In this code, ‘other’ is referring to “another object”.

You could modify the code to say what specific object has entered the collider, for example:

Or you could even destroy the other object that has entered the collider:

Lastly, you can use tags to distinguish between collisions with different objects in the scene. Tags are used to identify different types of objects (eg. enemy, collectables, spikes, hazards, etc.) In Unity, you can select an object and in the Inspector panel apply a tag to the object from the Tags dropdown menu (create a new tag name, then go back to the object and select the tag from the dropdown menu). Once the tag is applied to the object, you can use an if statement to check when a collision has occurred with the object that has that tag. The same tag can be applied to more than one object or instance of an object.

Next lesson: Raycasting