Tutorial 005 , mouse Mobile Input control

August 6, 2011   ·   0 Comments

we will learn how to add a  cube object at the run time, and doing animation.


the tutorial running on web.please left-click  to add cubes.

This Tutorial will show you how to write unity c# code to do  animation,

please step by step to follow below steps to create  your Mobile Input or mouse input  Unity3D App.

Unity iOS/Android offers you access to the iPhone, iPad and Android input systems via Input and iOS Input scripting interfaces.

Touch Screen

create a new project , and create a new C# script "mouseControlScript",

Adding new Input Axes

If you want to add new virtual axes and keyboard control go to the
Edit->Project Settings->Input menu. Here you can also change the settings of each axis and input.

Input Manager will display on Inspector

Name The name of the string used to check this axis from a script.
Descriptive Name Positive value name displayed in the input tab of the configuration dialog for standalone builds.
Alt Negative Button Alternative button used to push the axis in the negative direction.
Sensitivity Speed in units per second that the the axis will move toward the target value. This is for digital devices only.
Type The type of inputs that will control this axis.
Axis The axis of a connected device that will control this axis.
Joy Num The connected Joystick that will control this axis.

Using Input GetButtonDown from Scripts

You can query the current  button state from a script like this:

Input.GetButtonDown("Fire1"))

double-click the “mouseControlScript“, 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 mouseControlScript : MonoBehaviour {

	// Use this for initialization
	void Start () {

	}

    public GameObject particle;
    void Update() {
        if (Input.GetButtonDown("Fire1")) {
            particle = GameObject.CreatePrimitive(PrimitiveType.Cube);
			Vector3 screenPos =Input.mousePosition;
			screenPos.z = 20;
			Vector3 worldPos = Camera.main.ScreenToWorldPoint(screenPos);
		    particle.transform.position=worldPos;

        }
    }
}

description:

      1. Line 12:  
        void Update() is a event function, unity will call this event function every frame when the screen redraw,
        most time, this function will be call 25 time per second or more,
        unity developers use it do to data update or display,
        like us, we write the move action in this function,
        when it was be call every time, users will see the cube move.
      2. Line 13:
        Input.GetButtonDown("Fire1")

        An function return  a value  is true or false when user mouse down.
      3. Line 15~16:
        Vector3 screenPos =Input.mousePosition;
        Vector3 worldPos = Camera.main.ScreenToWorldPoint(screenPos);
        Convert the mouse click position to window screen position..
      4. Line 15~16:  
        particle.transform.position=worldPos;
        set up the new cube position.

Create the Cube and add our script.

GameObject->Create other->Cube to create a new cube, and drag and drop the  “mouseControlScript” 

we change “Cube” name to “player”,

press the “Play” button to start the game, it will add cube when user click window,  it will look like below picture.

sample code:

please download from here, “Tutorial 005 , mouse Mobile 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