Video Games Development

Unity 3D Game Engine – Set Material Transparency

Unity 3D Game Engine – Set Material Transparency

1. MAIN TOP MENU> Gameobject> Create Other> Cube

2. Hierarchy, select the Cube> Inspector> materal slot, select Shader: Transparent/Diffuse

NOTICE; YOu HAVE TO USE a Transparent shader, in Unity3D only Transparent Shaders support alpha channel

3. Select the Cube and assign SetTransparency.JS:


#pragma strict

	function Start () {
	}

	function Update () {
		gameObject.renderer.material.color.a = 0.5; // set transparency to 50%
	}

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Set Material Transparency

Unity 3D Game Engine – Fade Out Transparency

Unity 3D Game Engine – Fade Out Transparency

1. MAIN TOP MENU> Gameobject> Create Other> Cube

2. Hierarchy, select the Cube> Inspector> materal slot, select Shader: Transparent/Diffuse

NOTICE; YOu HAVE TO USE a Transparent shader, in Unity3D only Transparent Shaders support alpha channel

3. Select the Cube and assign FadeOutTransparency.JS:


#pragma strict

	// Fades from minimum to maximum in seconds
	var minimum = 1.0;
	var maximum = 0.0;
	var fadeoutSpeed = 1f; // 0.1 slow, 2 fast

	function Start () {
	}

	function Update () {	
		gameObject.renderer.material.color.a = Mathf.Lerp (minimum, maximum, fadeoutSpeed * Time.time);
	}

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Fade Out Transparency

Unity 3D Game Engine – Materials – Fade Between 2 Base Colors

Unity 3D Game Engine – Materials – Fade Between 2 Base Colors

1. MAIN TOP MENU> Gameobject> Create Other> Cube

2. Hierarchy, select the Cube> Inspector> materal slot, select Shader: Diffuse

NOTICE: the script will fade between ‘Shader> ‘Main Color’ values, you HAVE TO REMOVE all textures if you want to see ‘Main Color’!

3. Add to the Cube Fade.JS


#pragma strict

	// Fade the color from red to green
	// back and forth over the defined duration
	var colorStart : Color = Color.red;
	var colorEnd : Color = Color.green;
	var duration : float = 1.0;

function Start () {

}


	function Update () {
		var lerp : float = Mathf.PingPong (Time.time, duration) / duration;
		renderer.material.color = Color.Lerp (colorStart, colorEnd, lerp);
	}

4. Done!

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Materials – Fade Between 2 Base Colors

Unity 3D Game Engine – Load Next Level

Unity 3D Game Engine – Load Next Level

Syntax:


// Load the level named "level2".
Application.LoadLevel ("level2");

// Load the level index 1, 
// you find this value inside Buil Settings> 'Scenes in Build'
Application.LoadLevel (1);    

To improve performance a big game is broken over many levels.
Inside Unity3D you can use Scenes to create levels.

1. MAIN TOP MENU> File> New Scene, create 2 scenes: level1 and level2
2. Project window> DOUBLE CLICK over level1 to select the scene
3. MAIN TOP MENU> GameObject> Create Empty ‘GameController’> Inspector> ‘Add Component’> GameController.JS


#pragma strict

var scores : int;

function Start () {
scores = 10;

}

// On Click Counter START #########################
//This is the variable we are using to store the number of clicks
var clickCounter: int;
//This creates a button and adds +1 to clickCounter variable every 1 click
function OnGUI () {
    if (GUI.Button (Rect (10,10,150,100), "You clicked:" + clickCounter)) {
        clickCounter ++;        
    }
}
// On Click Counter END ###########################

function Update () {

   if (clickCounter > 10) {
    scores = 20;
    // Load the level named "level2".
	Application.LoadLevel ("level2");       
    }

} // END Update

4. MAIN TOP MENU> File> Buil Settings> DRAG AND DROP level1 and level2 scenes over ‘Scenes in Build’ window
Add the scenes into Build or Application.LoadLevel (“level2”); will not work!

When you click 11 times over the GUI.Button, level2 will be loaded.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Load Next Level

JS Basics – Passing variables between functions attached to different GameObjects

JS Basics – Passing variables between functions attached to different GameObjects

Hierarchy:

– Cube -> attach CubeScript.js

– GameController (Empty GameObject), Inspector> Tag rollout ‘GameController’ -> attach GameController.JS

CubeScript.js


#pragma strict

// definisco la variabile per il punteggio
var scoreValue : int; 

// variabile privata, non visibile in Inspector, mi serve per ottenere il componente GameController
private var gameController : GameController; 

function Start ()
{
    // inserisco in una variabile l'oggetto con tag GameController
    var gameControllerObject : GameObject = GameObject.FindWithTag ("GameController");
    // se l'oggetto con tag GameController esiste lo inserisco in una variabile
    if (gameControllerObject != null)
    {
        gameController = gameControllerObject.GetComponent (GameController);
    }
    // se l'oggetto con tag GameController non esiste restituisce un messaggio di errore
    if (gameController == null)
    {
        Debug.Log ("Cannot find 'GameController' script");
    }
}

function Update () {
SendData();
}

function SendData() {
    yield WaitForSeconds (3); // aspetta 3 secondi
    scoreValue = 10;
    // invia scoreValue allo script taggato GameController -> funzione AddScore()
    gameController.AddScore (scoreValue);
    Destroy(gameObject); // distruggi l'oggetto corrente altrimenti l'invio di dati si ripete ad ogni frame

}

GameController.JS


#pragma strict

// contatore di punteggio, private perchè non vogliamo che sia modificabile da Inspector
private  var score : int;

function Start () {

}

function Update () {

}

function AddScore (newScoreValue : int) {
    // aggiorna score aggiungendo il valore newScoreValue
    // che gli viene inviato da CubeScript.js
    // alla riga:  - gameController.AddScore (scoreValue); -
    score += newScoreValue;
    Debug.Log(score);  
    
}

The final console result is: 10

Spiegazione:

1. CubeScript.js
a. cerca un GameObject con il tag GameController
b. se presente ottiene dall’oggetto GameController il componente GameController (che non è altro che GameController.JS)
c. invia a GameController.JS -> funzione ‘AddScore()’ la variabile ‘scoreValue’;

2. GameController.JS
a. riceve nella funzione ‘AddScore()’ la variabile ‘scoreValue’
b. assegna ‘scoreValue’ alla variabile ‘newScoreValue’
c. effettua i calcoli e visualizza in console il risultato finale.

By |Unity3D, Video Games Development|Commenti disabilitati su JS Basics – Passing variables between functions attached to different GameObjects