Unity 3D – PickUp – Rotator

Unity 3D – PickUp – Rotator

1. Create a Cube with Rotator.js

#pragma strict

function Start () {

}

function Update () {
// Time.deltatime, use this function to make your game frame rate independent
transform.Rotate(new Vector3(15,30,45) * Time.deltaTime);
}
By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D – PickUp – Rotator

Unity 3D – Camera Controller

Unity 3D – Camera Controller

1. Create a ball that rolls over a plane

2. Creare a Camera with:
– CameraController.js

#pragma strict

public var player : GameObject;
private var offset :  Vector3;

function Start () {
offset = transform.position;
}

function LateUpdate () {
//LateUpdate is called after all Update functions have been called. 
// This is useful to order script execution. For example a follow camera should always be implemented in LateUpdate because it tracks objects that might have moved inside Update.
transform.position = player.transform.position + offset;
}

2. Hierarchy DRAG AND DROP ‘ball’ GameObject over Inspector> CameraController.js ‘player’ variable slot.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D – Camera Controller

Unity 3D – Get Axis – rigidbody.AddForce

Unity 3D – Get Axis – rigidbody.AddForce

1. Create a Plane (Ground) with:
– Mesh Collider

2. Create over the plane a Sphere with:
– Mesh Collider
– Rigid Body
– PlayerController.JS:

#pragma strict
 
function Start () {
 
}
// Theese variables are public to make the code easy-reusable
// You can setup theese variables from Inspector
var speed : float;

function FixedUpdate () {
     // Get User Input START
     var moveHorizontal : float= Input.GetAxis ("Horizontal");
     var moveVertical : float= Input.GetAxis ("Vertical");
     // Get User Input END

     var movement : Vector3= new Vector3 (moveHorizontal, 0.0f, moveVertical);
     rigidbody.AddForce (movement * speed * Time.deltaTime); // Time.deltaTime Use this function to make your game frame rate independent
}

Inspector> change ‘speed’ to 500 and Play

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D – Get Axis – rigidbody.AddForce

Unity – JS – Strings Operations

Unity – JS – Strings Operations
Unity uses the .Net System.String class for strings. See the Microsoft MSDN documentation for Strings for more details.

#pragma strict
function Start () {
    var s : String = "hello";
    Debug.Log(s);                    // prints hello
    s = String.Format("{0} {1}", s, "world");
    Debug.Log(s);                    // prints hello world
    s = String.Concat("hello","world");
    Debug.Log(s);                    // prints helloworld
    s = s.ToUpper(); 
    Debug.Log(s);                    // prints HELLOWORLD
    s = s.ToLower();
    Debug.Log(s);                    // prints helloworld
    //Debug.Log(s.CharAt(1));        // CharAt not supported, produces compiler error
                                     // instead use array syntax below
    Debug.Log(s[1]);                 // prints e
    
    var c : char = 'x'[0];           // slight odd JS way of specifying a char
                                     // 'x' is a string and [0] means first character
                                     
    Debug.Log(s.IndexOf(c));         // prints -1 (s does not contain an x)
    
    var i : int = 42;
    s = i.ToString();
    Debug.Log(s);                    // prints 42
    s = "-43";
    i = int.Parse(s);
    Debug.Log(i);                    // prints -43
    
    var f : float = 3.14159265358979f;
    s = f.ToString();
    Debug.Log(s);                    // prints 3.141593 (which is an approximation)
    
    s = "-7.14159265358979";
    f = float.Parse(s);
    Debug.Log(f);                    // prints -7.141593 (which is an approximation)
}
By |Unity3D, Video Games Development|Commenti disabilitati su Unity – JS – Strings Operations

Unity – Spaceship Shooting Game – JS – Game Over

Unity – Spaceship Shooting Game – JS – Game Over

MAIN TOP MENU> GameObject> Create Empty, rename it ‘Display Text’, Inspector> Transform> gear icon> Reset

MAIN TOP MENU> GameObject> Create Other> GUI Text, rename it ‘Restart Text’> Text ‘Restart’

MAIN TOP MENU> GameObject> Create Other> GUI Text, rename it ‘Game Over Text’> Text ‘Game Over’

Hierarchy: Display Text (father)
– Score Text (child)
– Restart Text (child)
– Game Over Text (child)

Game Controller empty object has GameController.js:

#pragma strict

var hazard : GameObject;
var spawnValues : Vector3;
var hazardCount : int;
var spawnWait : float;
var startWait : float;
var waveWait : float;

// GUI Text da assegnare da Inspector
var scoreText : GUIText;
var restartText : GUIText;
var gameOverText : GUIText;

// variabili per il game over, restart, sono booleane VERO O FALSO
private  var gameOver : boolean;
private  var restart : boolean;

private  var score : int;

function Start () {
    // gameover falso all'inizio 
    gameOver = false;
    // restart falso all'inizio
    restart = false;
    // GUIText Restart è vuoto all'inizio
    restartText.text = "";
    // GUI Text Game Over è vuoto all'inizio
    gameOverText.text = "";
    score = 0;
    UpdateScore ();
    StartCoroutine (SpawnWaves ());
}

function Update () {
    // se restart è vero, vedi sopra che è una variabile booleana
    // questo dato è inviato dalla riga - if (gameOver)
    if (restart)
    {
        // resta in ascolto del tasto R da tastiera
        // se viene attivato
        if (Input.GetKeyDown (KeyCode.R))
        {
            // ricarica il livello
            Application.LoadLevel (Application.loadedLevel);
        }
    }
}

function SpawnWaves () {
    yield WaitForSeconds (startWait);
    while (true)
    {
        for ( var i : int= 0; i < hazardCount; i++)
        {
             var spawnPosition : Vector3= new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
             var spawnRotation : Quaternion= Quaternion.identity;
            Instantiate (hazard, spawnPosition, spawnRotation);
            yield WaitForSeconds (spawnWait);
        }
        yield WaitForSeconds (waveWait);

        // se gameOver è vero, vedi sopra che è una variabile booleana
        if (gameOver)
        {
            // scrivi a video 
            restartText.text = "Press 'R' for Restart";
            // setta restart a vero
            restart = true;
            // interrompi il gioco
            break;
        }
    }
}

function AddScore (newScoreValue : int) {
    score += newScoreValue;
    UpdateScore ();
}

function UpdateScore () {
    scoreText.text = "Score: " + score;
}

// la funzione GameOver() viene richiamata da DestroyByContact.js
function GameOver () {
    // scrivi a video Game Over!
    gameOverText.text = "Game Over!";
    // setta la variabile booleana a vero
    gameOver = true;
}

Inspector, you have to assign:
– Score Text (GUIText)
– Restart Text (GUIText)
– Game over Text (GUIText)

Asteroid prefab has DestroyByContact.js:

#pragma strict

var explosion : GameObject;
var playerExplosion : GameObject;
var scoreValue : int;
private var gameController : GameController;

function Start ()
{
    // Richiama l'oggetto Game Controller e il suo script GameController.js INIZIO
    var gameControllerObject : GameObject = GameObject.FindWithTag ("GameController");
    if (gameControllerObject != null)
    {
        gameController = gameControllerObject.GetComponent (GameController);
    }
    if (gameController == null)
    {
        Debug.Log ("Cannot find 'GameController' script");
    }
    // Richiama l'oggetto Game Controller e il suo script GameController.js FINE
}

function OnTriggerEnter(other : Collider) 
{
    if (other.tag == "Boundary")
    {
        return;
    }
    Instantiate(explosion, transform.position, transform.rotation);
    if (other.tag == "Player")
    {
        Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
        // dopo che è istanziato il VFX per l'esplosione del Player
        // richiama Game Controller oggetto> GameObject.js> funzione Gameover()
        gameController.GameOver ();
    }
    gameController.AddScore (scoreValue);
    Destroy(other.gameObject);
    Destroy(gameObject);
}

In conclusione:

1. Asteroide è un Prefab con associato DestroyByContact.js
a) istanzia l’esplosione del Player
b) richiamala funzione di Gameover() di GameController.js
2. Game Controller è un empty object con associato GameController.js
a) riceve da DestroyByContact.js l’attivazione della funzione Gameover()
b) Gameover() scrive ‘Game Over!’ sul display e setta la variabile booleana gameOver a VERO
c) Se gameOver è VERO scrive a video ‘Press R for Restart’ e setta la variabile booleana restart a VERO, interrompe il ciclo while con break
d) Se restart è VERO resta in attesa che venga premuto ‘R’
e) Se viene premuto R viene ricaricato il livello – Application.LoadLevel (Application.loadedLevel) –

By |Unity3D, Video Games Development|Commenti disabilitati su Unity – Spaceship Shooting Game – JS – Game Over