Tutorial 006 , mouse move Input control

August 7, 2011   ·   0 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 drag and drop the Cube to moving this 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

let's add an new Cube Object, and  change the game object name to "player",


we change "Cube" name to "player",

Drag and Drop Script

t’s 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 “mouseMoveScript”.

double-click the “mouseMoveScript”, 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 mouseMoveScript : MonoBehaviour {
	public Color mouseOverColor = Color.blue;
	private Color originalColor;

	// Use this for initialization
	void Start () {
		originalColor = renderer.sharedMaterial.color;
	}

	// Update is called once per frame
	void Update () {

	}
	void OnMouseEnter () {
		renderer.material.color = mouseOverColor;
	}
	void OnMouseExit () {
		renderer.material.color = originalColor;
	}

	void OnMouseDown () {
		StartCoroutine(OnMouseDown_kernal());
	}

	IEnumerator OnMouseDown_kernal() {
	Vector3 screenSpace = Camera.main.WorldToScreenPoint(transform.position);
	Vector3 t_mousePosition;
	t_mousePosition.x=Input.mousePosition.x;
	t_mousePosition.y=Input.mousePosition.y;
	t_mousePosition.z=screenSpace.z;

    Vector3 offset = transform.position-Camera.main.ScreenToWorldPoint(t_mousePosition);
		while (Input.GetMouseButton(0))
		{
			Vector3 curScreenSpace;
			curScreenSpace.x=Input.mousePosition.x;
			curScreenSpace.y=Input.mousePosition.y;
			curScreenSpace.z=screenSpace.z;

			Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
			transform.position = curPosition;
			yield return new WaitForSeconds(0.05f);
		}
	}
}

description:

    1. Line 9:  
         void Start () 
      unity will call this event when start.
    2. Line 17:  
         void OnMouseEnter () 
      unity will call this event when mouse cursor on the game object.
    3. Line 20:  
         void OnMouseExit () 
      unity will call this event when mouse cursor move out on the game object.
    4. Line 24:  
         void OnMouseDown () 
      unity will call this event when mouse press down on the game object.
    5. Line 10:  
         originalColor = renderer.sharedMaterial.color;; 
      save this game object material color to originalColor parameter.
    6. Line 18:  
         renderer.material.color = mouseOverColor; 
      setup this game object material color is Blue (mouseOverColor = Color.blue;)
    7. Line 21:  
         renderer.material.color = originalColor; 
      setup this game object material color is original Color.
    8. Line 28 and Line 45 :  
          IEnumerator OnMouseDown_kernal()
      we create this function,
      yield return new WaitForSeconds(0.05f);
      is wait 0.05 sec, and continue the code,
      IEnumerator 
      is for yield function.
    9. Line 25:  
         StartCoroutine(OnMouseDown_kernal());
      StartCoroutine is for calling IEnumerator function.
    10. Line 29~46:  
         
        Vector3 screenSpace = Camera.main.WorldToScreenPoint(transform.position);
          Vector3 t_mousePosition;
          t_mousePosition.x=Input.mousePosition.x;
          t_mousePosition.y=Input.mousePosition.y;
          t_mousePosition.z=screenSpace.z; 
          Vector3 offset = transform.position-Camera.main.ScreenToWorldPoint(t_mousePosition);
      
      
              while (Input.GetMouseButton(0))
              {
                  Vector3 curScreenSpace;
                  curScreenSpace.x=Input.mousePosition.x;
                  curScreenSpace.y=Input.mousePosition.y;
                  curScreenSpace.z=screenSpace.z;
                  Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
                  transform.position = curPosition;
                  yield return new WaitForSeconds(0.05f);
              }
      Get Mouse Down game object position,
      and doing while loop, to detect mouse left-click unity press up.
 

and add "
mouseMoveScript
" Script to "player" object, if you forget how to add script to Game Object, remind you see tutorial 001 or just drag and drop the script to Game 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 (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Unity3D4

Amazon

adsbygoogle

Recent Posts

Categories

Amazon