we will learn how to make  Trigger game object


the tutorial running on web, you will see the cube destroy when trig

This Tutorial will show you how to write unity c# code to do  trigger ,
please step by step to follow below steps to create  a new project . new a Unity Project


function OnTriggerEnter (other : Collider) : void

OnTriggerEnter is called when the Collider other enters the trigger.

This message is sent to the trigger collider and the rigidbody (or the collider if there is no rigidbody) that touches the trigger. Note that trigger events are only sent if one of the colliders also has a rigid body attached.


new a Unity Project

Create a Plane

create an new Plane Object, and  change the game object name to "ground"

position to x=0, y=0, z=-5 , and 
Scale x=2, y=2, z=2.

Add a new cube, and change name called “player” object.

create an new Cube Object, and  change the game object name to "player
position to x=2, y=0.5, z=-0

Add a new cube top of our “player” object.

create an new Cube Object, and  change the game object name to "trigger cube"

position to x=2, y=2, z=0 , and 
Scale x=1, y=1, z=1.

“trigger cube” game object Add “Rigidbody”

Add “Menu->Component->Physics->Rigidbody”  to “trigger cube”, make this cube has physics action.

Enable the Trigger function

please click and enable “Trigger Cube\Box\Colider\Is Trigger”, for enable and make this game object has trigger function.

Create a new C# Script “addTriggerEvent”

Add the "addTriggerEvent" script to "Trigger Cube" game object.

drag and drop the  "addTriggerEvent" script to "Trigger Cube",

you will see the game object attribute has this script.

private string define_tag="Player";
for(int y=1;y

	<li>Line 3: <code><code> </code></code>
<div><span style="color: #888888;"><code>GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube );</code></span><span class="Apple-style-span" style="font-family: monospace;">            </span></div>
<pre><code>create a new cube</code></pre>
	<li>Line 5: <code><code> </code></code>
<pre><span style="color: #888888;"><code>  <code>cube.transform.position=</code><code>new</code> <code>Vector3 (x,y, -5); </code></code></span><code>setup cube object position and location to x,y,-5 </code></pre>
	<li>Line 6: <code><code>
 </code></code><span style="color: #888888;"><code>cube.AddComponent(</code><code>"Rigidbody"</code><code>);</code></span>add the cube has physics Rigidbody.</li>
	<li>Line 7: <code><code><code> </code></code></code>
<pre><span style="color: #888888;">cube.tag=define_tag;</span></pre>
setup the cube tag is  "<span class="Apple-style-span" style="font-family: Consolas, Monaco, monospace; font-size: 12px; line-height: 18px; white-space: pre;">Player"</span></li>
	<li>Line 2~3: <code><code>
<div><span style="color: #888888;"><code>for</code><code>(int y=1;y<=5;y++){</code></span></div>
<div><span style="color: #888888;"><code>    </code><code>for</code><code>(int x=-3;x<=3;x++){</code></span></div>
<div><span style="color: #888888;"><code>}}</code></span></div>
doing loop to add cubes become a wall.
<h3><span class="Apple-style-span" style="font-size: 13px; font-weight: normal;">It's time a create a new script, Create a new C# Script  name "mouseMoveScript" we add a new C# Script. click mouse right button, and select “Create\C# Script”, update the C# Script name to “addCubeWallScript”.</span></h3>
<a href="http://www.powenko.com/en/wp-content/uploads/2011/08/Screen-Shot-2011-08-05-at-6.45.56-PM.png"><img class="alignnone size-full wp-image-123" title="Screen Shot 2011-08-05 at 6.45.56 PM" src="http://www.powenko.com/en/wp-content/uploads/2011/08/Screen-Shot-2011-08-05-at-6.45.56-PM.png" alt="" width="302" height="139" /></a>

double-click the "addCubeWallScript", the editor will show out, unity default is "MonoDevelop" app to do edition. please change the code to below.

using UnityEngine;
using System.Collections;

public class addCubeWallScript : MonoBehaviour {

	// Use this for initialization

	private string define_tag="Player";
	void Start () {

		for(int y=1;y		{
			for(int x=-3;x			{
			  GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube );
			  cube.transform.position=new Vector3 (x,y, -5);


	// Update is called once per frame
    void Update() {
        if (Input.GetButtonDown("Fire1")) {
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        	RaycastHit hit;
			if (Physics.Raycast(ray,out hit, 100))  {
     			Vector3 screenPos =Input.mousePosition;
				screenPos.z = 0;
				GameObject otherObj= hit.collider.gameObject;



    1. Line 9:  
         void Start () 
      unity will call this event when start, and add script the cubes become a wall.
    2. Line 17:  
         void Update() 
      unity will call this event when redaw on the game object.
    3. Line 27:  
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition) 
      unity will call this event when mouse cursor move out on the game object.
    4. Line 29:  
         if (Physics.Raycast(ray,out hit, 100)) 
      unity will call this event when mouse press down on the game object.
    5. Line 32:  
         GameObject otherObj= hit.collider.gameObject;
      save this game object material color to originalColor parameter.
    6. Line 33:  
      setup this game object material color is Blue (mouseOverColor = Color.blue;)
    7. Line 34:  
      setup this game object material color is original Color.

add "addCubeWallScript" Script to "ground" object.

press the "Play"  button to start the game. it will look like below picture.


setup different color when move the game object.


renderer.material.color = (1,1,1,1);
R,G,B,A  where the values are from 0 to 1.
Red,Green, Blue, Alpha


Input.GetMouseButton(0) is detect left-click

Input.GetMouseButton(1) is detect mid-click

Input.GetMouseButton(2) is detect right-click

sample code:

please download from here, "Tutorial 006 , mouse move Input control "

By admin-powenko

Dr. Powen Ko is a teacher and CEO on LoopTek LLC, and like to teaching. if you need to class, please let PowenKo know, he will love to service and sharing. LoopTek web site is www.looptek.com

Leave a Reply