PowenKo, Tutorial 007 , mouse click game object

August 7, 2011   ·   3 Comments

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 “

shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (3)

  1. metodo

    Hello.This post was really motivating, particularly since I was investigating for thoughts on this issue last Friday.

    »
  2. bielizna damska kinga

    Greetings from Ohio! I’m bored to tears at work so I decided to check out your website on my iphone during lunch break. I love the information you present here and can’t wait to take a look when I get home. I’m surprised at how quick your blog loaded on my cell phone .. I’m not even using WIFI, just 3G .. Anyhow, good site!

    »

You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Unity3D4

Amazon

adsbygoogle

Recent Posts

Categories

Amazon