Unity – Rotate GameObjects

Unity – Rotate GameObjects

Rotate x frame

DRAG AND DROP this script over a object in the Hierarchy:

#pragma strict

function Update ()
{
         transform.Rotate(Vector3.back);
}

Rotate x frame

Rotate x seconds

#pragma strict

public var turnSpeed : float = 5f;

function Update ()
{
         transform.Rotate(Vector3.back, turnSpeed * Time.deltaTime);
}

Time.deltaTime -> it converts to rotate x seconds
turnSpeed -> speed of the rotation (it is a public variable, you can try to change his value inside Inspector!)

Rotate X Y Z

#pragma strict

public var turnSpeed : float = 20f;

function Update ()
{
         // -----------------------------
         // 2D Rotation XY plane
         // Rotate clockwise Z
         transform.Rotate(Vector3.back, turnSpeed * Time.deltaTime);         
         // Rotate anticlockwise Z
         transform.Rotate(-1*Vector3.back, turnSpeed * Time.deltaTime);
         
         // -----------------------------
         // 3D Rotation XZ and YZ
         // Rotate Y
         transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
         transform.Rotate(-1*Vector3.up, turnSpeed * Time.deltaTime);
         // Rotate X
         transform.Rotate(Vector3.right, turnSpeed * Time.deltaTime);
         transform.Rotate(-1*Vector3.right, turnSpeed * Time.deltaTime);
}
By |Unity3D|Commenti disabilitati su Unity – Rotate GameObjects

Unity – Game Objects – 2D Position

Unity – Game Objects – 2D Position

2D Position

The position of the object in world space.
NOTICE: x,y,z -> 0,0,0 it is the center of the screen

#pragma strict

function Update ()
{     
        // Move the object to (0, 0, 0)
	transform.position = Vector3(0, 0, 0);
		
	// Print the x component of the position to the Console
	print(transform.position.x);
	print(transform.position.y);
	print(transform.position.z);
}

Statement: transform.position = Vector3(x, y, z);

NOTICE: in 2D Games z=0

By |Unity3D|Commenti disabilitati su Unity – Game Objects – 2D Position

Unity – Loops – FOR – WHILE – DO WHILE – FOR EACH

Unity – Loops – FOR – WHILE – DO WHILE – FOR EACH

In Unity loops can execute a block of code a number of times.

FOR

#pragma strict

var numEnemies : int = 3;


function Start ()
{
    for(var i : int = 0; i < numEnemies; i++)
    {
        Debug.Log("Creating enemy number: " + i);
    }
}

The result is:
Creating enemy number: 0
Creating enemy number: 1
Creating enemy number: 2

WHILE

#pragma strict

var cupsInTheSink : int = 4;


function Start ()
{
    while(cupsInTheSink > 0)
    {
        Debug.Log ("I've washed a cup!");
        cupsInTheSink--;
    }
}

The result is:
I’ve washed a cup! (number 4)
I’ve washed a cup! (number 3)
I’ve washed a cup! (number 2)
I’ve washed a cup! (number 1)

DO WHILE

#pragma strict

function Start()
{
    var shouldContinue : Boolean = false;
        
    do
    {
        print ("Hello World");
            
    }while(shouldContinue == true);
}
[

The result is:
Hello World if shouldContinue == true

<h2>FOR EACH and ARRAYS</h2>

#pragma strict

function Start () 
{
    var strings = ["First string", "Second string", "Third string"];
    
    for(var item : String in strings)
    {
        print (item);
    }
}

The result is:

First string
Second string
Third string

By |Unity3D|Commenti disabilitati su Unity – Loops – FOR – WHILE – DO WHILE – FOR EACH

Unity – Time.deltaTime

Unity – Time.deltaTime

The time in seconds it took to complete the last frame (Read Only).
Use this function to make your game frame rate independent, and for smoot movements.

#pragma strict

function Update () {
		// Move the object 10 meters per second!
		var translation : float = Time.deltaTime * 10;
		transform.Translate (translation, 0, 0);
	}
By |Unity3D|Commenti disabilitati su Unity – Time.deltaTime

Unity – Activating GameObjects

Unity – Activating GameObjects

Activate Object

DRAG AND DROP this script over a object in the Hierarchy:

#pragma strict

function Start ()
{
    gameObject.SetActive(false); // Deactivate an object
}

Check State

Create an object in the scene and give it the name ‘GreatObject’

Create this Script and give it the name ‘MyScript’

#pragma strict

public var myObject :GameObject;


function Start ()
{
    Debug.Log("Active Self: " + myObject.activeSelf);
    Debug.Log("Active in Hierarchy" + myObject.activeInHierarchy);
}

1. DRAG AND DROP this script over ‘GreatObject’ in the Hierarchy
2.Select the ‘GreatObject’> Inspector> MyScript> myObject> the slot is ‘None’, on the right ‘Select Game object’ small icon> Scene> select ‘GreatObject’ from the scene, NOW ‘public var myObject = GreatObject’, you will Debug.Log it

NOTICE:

– if you disable a PARENT object also CHILD will be disabled.
– if you disable a CHILD object the PARENT does not change his original status.

By |Unity3D|Commenti disabilitati su Unity – Activating GameObjects