Collision Detection In Unity

Introduction

 
This article demonstrates the basics of Collision detection and how to create physical materials and triggers in Unity.
 
Prerequisites
 
Unity Environment version 5.6.1
 
Create Terrain
 
Step 1
 
First, you have to open the Unity project. Click on the GameObject in the menu bar. Select the 3D objects and pick the Terrain.
 
 
Terrain will be added to your Scene View.
 
 
Create Cube
 
Step 2
 
Click on the GameObject in the menu bar. Select the 3D objects and pick the Cube.
 
 
The cube will be added to your Scene View. Drag and drop the red material into the cube. The color of the cube will now be changed into red. Set the cube-like in the following image.
 
 
Again add another cube in your Scene View.
 
 
Cube1 will be added. Drag and drop the blue material into cube1. The cube1 will change its color into blue.
 
 
Click on the Cube in the left menu. Go to "Add Component" and select the physics option. Then, click on the Rigid body, as shown below.
 
 
Do the same with blue cube also. 
 
 
Go to the Rigid body and uncheck the "Use gravity" option.
 
 
Click on the “Play” button. The cube will fall down.
 
 
Click on the cube1. Go to the Inspector panel. Select the constraints and check all X, Y, Z boxes.
 
 
Click on the “Play” button. The cube will fall down to the position.
 
 
Collision basics
 
Step 3
 
Click on the cube. Go to the "Add component" and select the "Sphere collider".
 
 
Go to the Box collider >> corner icon. Select the "Remove component" option.
 
 
Click on the “Play” button. The cube can be falling down and moving out to cube1.
 
 
Create Physic material
 
Step 4
 
Click on the Assets menu in the menu bar. Select the create and pick the Physical material option.
 
 
The physical material is added to the assets. Rename as bouncy. Go to the sphere collider click on the material option select bouncy material.
 
 
Click on the bouncy material. Set 1 as bounciness. Set the maximum for a bounce.
 
 
Click on the play button. The cube can be bounced into the terrain.
 
 
Triggers in unity
 
Step 5
 
Click on the cube1. Go to the box collider Check the trigger box.
 
 
Click on the “Play” button. The cube will be entered into cube1.
 
 
Go to the mono development in unity and write the coding like the following.
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4. public class NewBehaviourScript: MonoBehaviour {  
  5.     void OnTriggerEnter(Collider other) {  
  6.         print("Another object has entered the trigger");  
  7.     }  
  8.     // Use this for initialization  
  9.     void Start() {}  
  10.     // Update is called once per frame  
  11.     void Update() {}  
  12. }  
Save the program.
 
Come back to the Unity window. Click on the “Play” button. The cube will enter cube1, and “Another object has entered the trigger” message will be displayed into the console panel.
 
 
Go to the mono development in Unity and write the code like the following.
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4. public class trigger: MonoBehaviour {  
  5.         void OnTriggerEnter(Collider cube) {  
  6.             Destroy(cube.gameObject);  
  7.         }  
  8.         // Use this for initialization  
  9.         void Start() {}  
Save the program.
 
Come back to the Unity window. Click on the “Play” button. The cube will be destroyed when it enters the cube1.
 
 

Summary

 
I hope you understood collision detection basics now.


Similar Articles