unity3d

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

Unity – Variables and Data Types

Unity – Variables and Data Types

Public and Private

In Unity JavaScript it is not obligatory for every variable to declare – private or public –

Under:
1. without declarations the variable ‘myInt’ is public, you can use it anywhere in the code.
2. variable ‘ret’ is private because it is created inside a function, you can use it outside the function MultiplyByTwo()

#pragma strict

var myInt : int = 5;


function Start ()
{
    myInt = MultiplyByTwo(myInt);
    Debug.Log (myInt);
}


function MultiplyByTwo (number : int) : int
{
    var ret : int;
    ret = number * 2;
    return ret;
}

Under: it is better to declare – private or public – it is more professional and will make fewer mistakes!

#pragma strict

public var apples : int;
public var bananas : int;


private var stapler : int;
private var sellotape : int;


public function FruitMachine (a : int, b : int)
{
    var answer : int;
    answer = a + b;
    Debug.Log("Fruit total: " + answer);
}


private function OfficeSort (a : int, b : int)
{
    var answer : int;
    answer = a + b;
    Debug.Log("Office Supplies total: " + answer);
}

Data Types

Value
Contengono un valore
Cambiano solo se il valore della variabile viene cambiato in fase di scripting con una funzione

– int : interi
– float : numeri con la virgola
– double : it can hold very large (or small) numbers, the maximum and minimum values are 17 followed by 307 zeros.
– String : stringhe
– char : characters (caratteri alfanumerici)
– bool : TRUE Or FALSE
– Structs : strutture
-> Vector3
-> Vector2
-> Quaternion

Examples:

var hazardCount : int;
var spawnWait   : float;
var s           : String = "hello";
var gameOver    : boolean;
var spawnValues : Vector3;
var spawnValues : Vector3;
var cameraTarget : Transform;

Reference
Contengono un indirizzo di memoria con un valore che cambia nel tempo.
Ad esempio se la posizione di un oggetto cambia nel tempo anche la variabile di riferimento cambia, senza bisogno di utilizzare uno script. L’oggetto di riferimento viene assegnato all’interno di Inspector.

– Classes
-> Transform
-> GameObject

Examples:

// Assegnare da Inspector gli oggetti di riferimento
var mytext : GUIText;
var displayTime  : TextMesh; 
var theTextComponent : UI.Text; // store the Text component - Unity 4.6 or over
var theSliderComponent : UI.Slider; // store Slider component - Unity 4.6 or over
var MyInputField : UI.InputField; // store Slider component - Unity 4.6 or over
var myobject : GameObject;
var mycamera : Camera;
var mylight : Light;
var myaudio : AudioClip;

var myobjects : GameObject[]; // array di oggetti
var materials : Material[]; // array di materiali
By |Unity3D|Commenti disabilitati su Unity – Variables and Data Types