Unity, C#, Tutorial 004 , keyboard control

August 5, 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 use Up/Down/Left/Right to Control the Cube.

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 first Unity3D App.

open a Tutorial 003 project, and update the code.

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 Axes from Scripts

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

value = Input.GetAxis ("Horizontal");

let’s create a new C# Script  name “playerControlScript”

we add a new C# Script.
click mouse right button, and select “Create\C# Script”.

update the C# Script name to “playerControlScript”.

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

	// Use this for initialization
	void Start () {


	// Update is called once per frame
	void Update () {
	float t_Hvalue = Input.GetAxis ("Horizontal");
	float t_Vvalue = Input.GetAxis ("Vertical");

		transform.position=new Vector3 (transform.position.x+t_Hvalue  ,
		                                transform.position.y+t_Vvalue, 0);



    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:
      float t_Hvalue = Input.GetAxis ("Horizontal");

      An axis has a value between -1 and 1. The neutral position is 0. This is the case for joystick input and keyboard input.
    3. Line 16:

       transform.position=new Vector3 (transform.position.x+t_Hvalue  ,
                                              transform.position.y+t_Vvalue, 0);

      Move game object to new position.


we change "Cube" name to "player",

and add "playerControlScript" 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.


Is Game object “player” move too fast? please add code to update it and make move smooth,


  • Time.deltaTime
  • * 0.1

sample code:

please download from here, “Tutorial 004 , keyboard 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


Readers Comments (0)

You must be logged in to post a comment.

PowenKo’s Online Store




Recent Posts