Video Games Development

Input Control – Arrow Key – Advanced

Input Control – Arrow Key – Advanced

#pragma strict

//--------------------------------------------------------------------
// USER INPUT CONTROL START
//--------------------------------------------------------------------
public var inputMoveSpeed : float = 10f; // increase -> faster
public var inputTurnSpeed : float = 50f; // increase -> faster

function UserInputControl()
{
    // Arrow key to move object
    if(Input.GetKey(KeyCode.UpArrow))
        transform.Translate(Vector2.up * inputMoveSpeed * Time.deltaTime);
    
    if(Input.GetKey(KeyCode.DownArrow))
        transform.Translate(-1*Vector2.up * inputMoveSpeed * Time.deltaTime);
    
    if(Input.GetKey(KeyCode.RightArrow))
        transform.Translate(Vector2.right * inputMoveSpeed * Time.deltaTime);
        
    if(Input.GetKey(KeyCode.LeftArrow))
        transform.Translate(-1*Vector2.right * inputMoveSpeed * Time.deltaTime);
        
    // Z X keys to rotate object
    if(Input.GetKey(KeyCode.X))
         transform.Rotate(Vector3.back,inputTurnSpeed * Time.deltaTime);
         
    if(Input.GetKey(KeyCode.Z))
         transform.Rotate(-1*Vector3.back,inputTurnSpeed * Time.deltaTime); 
}
//--------------------------------------------------------------------
// USER INPUT CONTROL END
//--------------------------------------------------------------------

function Update ()
{
UserInputControl(); // call User Input control inside Update()   
}

Installation:
1. Copy paste the code
2. Call the function – UserInputControl(); – inside Update()

By |Unity3D|Commenti disabilitati su Input Control – Arrow Key – Advanced

Unity – Horizontal and Vertical – Get Axis

Unity – Horizontal and Vertical – Get Axis

Horizontal and Vertical Control – Complete Example

#pragma strict

function Start () {

}

var speed : float = 10.0; // to change speed

function Update () {

 var horiz : float = Input.GetAxis("Horizontal");
 var vert : float = Input.GetAxis("Vertical");

 // Make it move 10 meters per second instead of 10 meters per frame...
 horiz *= Time.deltaTime;
 vert *= Time.deltaTime;
 
 transform.Translate(Vector3(horiz*speed,vert*speed,0));
}
By |Unity3D|Commenti disabilitati su Unity – Horizontal and Vertical – Get Axis

Unity – OnMouse

Unity – OnMouse

OnMouse is called when the user has used the mouse over the GUIElement or Collider.

Basic

1. Create an object
2. Assign a Collider OR ONMOUSE WILL NOT WORK!
3. Attach this script and click over the object with the mouse button:

#pragma strict

function Start ()
{
   
}


function OnMouseDown ()
{
    Debug.Log('Activaction of OnMouseDown!');
}

NOTICE: function OnMouseDown is outside other functions, you do not need put it inside Update() function.

Complete list of OnMouse functions

#pragma strict

function Start ()
{
   
}
// On Mouse Click events ###############################################

function OnMouseDown ()
{
        // When you click over the object
        Debug.Log('Activaction of OnMouseDown!');
}

function OnMouseDrag ()
{
        // When you click over the object and drag
        Debug.Log('Activaction of OnMouseDrag!');
}

function OnMouseUp () {
        // When you release the drag over or outside the object
	Debug.Log('Activaction of OnMouseUp!');
}

function OnMouseUpAsButton () {
        // When you release over the object
	Debug.Log('OnMouseUpAsButton');
}

// On Mouse position cursor events ######################################

function OnMouseEnter () {
	// When cursor enter in the object area
	Debug.Log('Activaction of OnMouseEnter!');
}

function OnMouseOver () {
	// When cursor is over the object area
	Debug.Log('Activaction of OnMouseOver!');
}

function OnMouseExit () {
     // When cursor leave object area
	Debug.Log('Activaction of OnMouseExit!');
}
By |Unity3D|Commenti disabilitati su Unity – OnMouse

Unity – Switch

Unity – Switch

Switch Statement:

#pragma strict

var intelligence : int = 5;
    
    
function Greet()
{
    switch (intelligence)
    {
    case 5:
        print ("Why hello there good sir! Let me teach you about Trigonometry!");
        break;
    case 4:
        print ("Hello and good day!");
        break;
    case 3:
        print ("Whadya want?");
        break;
    case 2:
        print ("Grog SMASH!");
        break;
    case 1:
        print ("Ulg, glib, Pblblblblb");
        break;
    default:
        print ("Incorrect intelligence level.");
        break;
    }
}

NOTICE:
‘switch (intelligence)’ -> get the valuie from ‘var intelligence : int = 5’

By |Unity3D|Commenti disabilitati su Unity – Switch

Unity – Car Driving – Basic

Unity – Car Driving – Basic

A very simplistic car driving on the X-Y plane


#pragma strict 

        // A very simplistic car driving on the x-y plane
	var speed : float = 10.0;          // to change car speed
	var rotationSpeed : float = 100.0; // to change rotation speed
	
	function Update () {
		// Get the horizontal and vertical axis.
		// Keys: A D to rotate left right - W: go!  
		// The value is in the range -1 to 1
		var translation : float = Input.GetAxis ("Vertical") * speed;
		var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
		
		// Make it move 10 meters per second instead of 10 meters per frame...
		translation *= Time.deltaTime;
		rotation *= Time.deltaTime;
		
		// Move translation along the object's x-axis
		transform.Translate (0, translation, 0);
		// Rotate around our z-axis
		transform.Rotate (0, 0, -1*rotation);
	}

By |Unity3D|Commenti disabilitati su Unity – Car Driving – Basic