we will learn how to make  cube object wall  at the run time, and remove the cube when click the cube.

 

the tutorial running on web.please click the cube to remove the game object.

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


Create a Plane

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


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

Create a cube wall.

private string define_tag="Player";
for(int y=1;y<=5;y++){
	for(int x=-3;x<=3;x++){
			  GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube );
			  cube.transform.position=new Vector3 (x,y, -5);
			  cube.AddComponent("Rigidbody");
			  cube.tag=define_tag;
	}
}

description:

  1. Line 3:  
    GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube );            
    create a new cube
  2. Line 5:  
      cube.transform.position=new Vector3 (x,y, -5); setup cube object position and location to x,y,-5 
  3. Line 6: 
    cube.AddComponent("Rigidbody");add the cube has physics Rigidbody.
  4. Line 7:  
    cube.tag=define_tag;

    setup the cube tag is  “Player”

  5. Line 2~3: 

    for(int y=1;y<=5;y++){
        for(int x=-3;x<=3;x++){
    }}

    doing loop to add cubes become a wall.

     

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”.

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<=5;y++)
		{
			for(int x=-3;x<=3;x++)
			{
			  GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube );
			  cube.transform.position=new Vector3 (x,y, -5);
			  cube.AddComponent("Rigidbody");
			  cube.tag=define_tag;

			}
		}
	}

	// 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;
				if(otherObj.tag==define_tag){
					otherObj.transform.position=screenPos;
				}
			}
        }
    }

}

description:

    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:  
          if(otherObj.tag==define_tag){
      setup this game object material color is Blue (mouseOverColor = Color.blue;)
    7. Line 34:  
          otherObj.transform.position=screenPos
      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.

Practice:

setup different color when move the game object.

hit:

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

extension:

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