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:
- Line 3:
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube );
create a new cube
- Line 5:
cube.transform.position=
new
Vector3 (x,y, -5);
setup cube object position and location to x,y,-5
- Line 6:
cube.AddComponent(
"Rigidbody"
);
add the cube has physics Rigidbody. - Line 7:
cube.tag=define_tag;
setup the cube tag is “Player”
- 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:
- Line 9:
void Start ()
unity will call this event
when start, and add script the cubes become a wall. - Line 17:
void
Update()unity will call this event
when redaw on the game object. - Line 27:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition)
unity will call this event
when mouse cursor move out on the game object. - Line 29:
if
(Physics.Raycast(ray,out hit, 100))
unity will call this event
when mouse press down on the game object. - Line 32:
GameObject otherObj= hit.collider.gameObject;
save this game object material color to originalColor parameter. - Line 33:
if
(otherObj.tag==define_tag){
mouseOverColor = Color.blue;
) - Line 34:
otherObj.transform.position=screenPos
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 “