unity3d

Unity3D – Character Controller – isGrounded – JavaScript

Unity3D – Character Controller – isGrounded – JavaScript

Was the CharacterController touching the ground during the last move?


function Update () {
		var controller : CharacterController = GetComponent(CharacterController);
		if (controller.isGrounded)
			print("We are grounded");
	}

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Character Controller – isGrounded – JavaScript

Unity3D – Character Controller – Velocity – JavaScript

Unity3D – Character Controller – Velocity – JavaScript

This allows you to track how fast the character is actually walking for example when he is stuck at a wall this value will be the zero vector, or if vertical speed is <0 the character is falling down. The current relative velocity of the Character. The velocity is relative because it won't track movements to the transform that happen outside of the CharacterController (e.g. character parented under another moving Transform, such as a moving vehicle). [js] function Update () { // Get relative velocity of Character Controller component GetVelocity(); }//END UPDATE function GetVelocity() { // Get the component var controller : CharacterController = GetComponent(CharacterController); var horizontalVelocity : Vector3 = controller.velocity; horizontalVelocity = Vector3(controller.velocity.x, 0, controller.velocity.z); // The speed on the x-z plane ignoring any speed var horizontalSpeed : float = horizontalVelocity.magnitude; // The speed from gravity or jumping var verticalSpeed : float = controller.velocity.y; // The overall speed var overallSpeed : float = controller.velocity.magnitude; Debug.Log(horizontalSpeed + " " + verticalSpeed + " " + overallSpeed ); }// END GetVelocity() [/js]

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Character Controller – Velocity – JavaScript

Unity3D – Character Controller – Detect Collision

Unity3D – Character Controller – Detect Collision

Determines whether other rigidbodies or character controllers collide with this character controller (by default this is always enabled).
This property is useful to disable the character controller temporarily. For example, you might want to mount a character into a car and disable collision detection until it exits the car again.

        var c : CharacterController;
	c = GetComponent(CharacterController);
	c.detectCollisions = false;
By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Character Controller – Detect Collision

Unity3D – Game Engine – Stop and Resume Timer – JavaScript

Unity3D – Game Engine – Stop and Resume Timer – JavaScript

A Stop and Resume Timer is very useful in video games!

Create an Empty Object and assign Timer.js:


#pragma strict

var myTimer : float; // start value for the timer
var defTime : int; // the reset value
var isCounting : boolean = false; // true while time running
var countUp : boolean = false; // false to count 10...9...8...7...6 true to count 0...1...2...3...4
var GuiText : GUIText; // display timer values

function Start () {
}// END Start()

function Update () {
	Timer();
}// END Update()

function Timer() {
	if(myTimer <= 0){
			myTimer = 0.0; // we do not need negative time!
		}

		if(isCounting && !countUp){
			myTimer -= Time.deltaTime; // count 10...9...8...7...6
		}
			
		if(isCounting && countUp){
			myTimer += Time.deltaTime; // count 0...1...2...3...4
		}

		if(Input.GetKeyDown(KeyCode.R)){ // Reset at defTime
			myTimer = defTime;
		}

		if(Input.GetKeyDown(KeyCode.S)){ // start or stop timer
			isCounting = !isCounting; // invert value of isCounting boolean variable
		}

		GuiText.text = myTimer.ToString("F0"); // display timer values
	}//END Timer()

Inspector> DRAG AND DROP a ‘GUI Text’ over the var GuiText

Inspector> setup var myTimer= 10, var countUp= uncheck, it will count 10…9…8…7…6
OR
Inspector> setup var myTimer= 0, var countUp= check, it will count 0…1…2…3…4

Play, S to start and stop, R to reset.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Game Engine – Stop and Resume Timer – JavaScript

Unity 3D – Loading and Downloading Screen – Basic

Unity 3D – Loading and Downloading Screen – Basic

Loading Screen

How to create a simple Loading Screen and using it with the Web Player

Create e scenes:

– level2 (it is the second scene)
– Main Camera
– Plane
-> attach a 4 MPix Texture
-> select the Texture> Inspector> Override for Web>
– Max Size 4096
– Format True Color
(it will be 20 MB into final build)

– level1 (it is the first scene)
– Main Camera
-> attach ‘GameController.js’
– Plane
-> attach a small Texture with a loading message
-> attach ‘LoadingScreen.js’
– GUI Text
-> attach ‘DownloadingText.js’

DownloadingText.js

#pragma strict

	// Attach this script to a GuiText object
	// Print on a guiText how much has been streamed level at index 1
	// When finished streaming, print "Level 1 has been fully streamed!"
	var percentageLoaded : float = 0;
	
	function Start () {

	}// ENd Start()
	function Update() {
		if(Application.GetStreamProgressForLevel(1) == 1) {
			guiText.text = "Level at index 1 has been fully streamed!";
		} else {
			percentageLoaded = Application.GetStreamProgressForLevel(1) * 100;
			guiText.text = percentageLoaded.ToString();// it displays: 54.569856... 100, you can use it to drive a progress bar!
		}
	}// END Update

GameController.js:

#pragma strict

function Start () {
}// END Start

function Update () {
}// END Update()

function OnGUI () {
    if (GUI.Button (Rect (10,10,150,100), "Load Next Level")) {
        Application.LoadLevel ("level2");          
    }
}// END OnGUI()

LoadingScreen.js

#pragma strict

function Start () {
	// Hide the object
	renderer.enabled = false;
}// END START

function Update () {
     //  It checks if the application is loading a level. If yes, then show the splash screen.
 	 if(Application.isLoadingLevel)
      Show();
}// END Update

function Show () {
	// Show the object
	renderer.enabled = true;
}// END Show()

MAIN TOP MENU> File> Build Settings>
– Scenes In Build> Add first level1.unity (0), then level2.unity(1)
– Platform Web Player
– Web Player> Streamed

Build, upload, play.

How does it work?
Really easy!
0. Watch the GUIText, with the percentage of download, you will see that web player is downloading level2.unity in background.
1. When the game starts the web player downloads level1.unity, after level2.unity and so on…
2. Press “Load Next Level”
3. NOTHING HAPPENS if the download of level2.unity is not complete.
4. ONLY when the download of level2.unity is completed, web player loading level2.unity scene
5. LoadingScreen.js checks if the application is loading a level. If yes, then show the splash screen.

Downloading Screen

For italian people:
‘Application.isLoadingLevel’ sarà TRUE solo nel momento in cui il livello sarà completamente scaricato e sarà iniziato il suo effettivo caricamento.
‘Application.isLoadingLevel’ NON ‘sente’ la fase di download della scena.

Per creare un loader durante lo streaming, prima dell’avvio dell’istruzione ‘Application.LoadLevel’ visualizzare una schermata di loading nella scena corrente. La procedura sarà:
1. Il livello1 viene terminato
2. Visualizzare una schermata per il download level2…
3. Avviare ‘Application.LoadLevel’ per iniziare il caricamento del livello2
4. Una volta scaricato completamente il livello2, inizierà il processo di caricamento del livello2
5. ‘Application.isLoadingLevel’ visualizzerà una schermata per il loading level2…
6. Il livello2 sarà avviato

Best streaming practice

50 KB display the logo and menu (4 seconds)
320 KB let the user play a tiny tutorial level or let him do some fun interaction in the menu (20 seconds)
800 KB let the user play the first small level (50 seconds)
Finish downloading the entire game within 1-5 MB (1-5 minutes)

Externalize the music and load it via the WWW class

Loads the level asynchronously in the background

Unity will completely load all assets and all objects in the scene in a background loading thread. You can create a completely streaming world where you constantly load and unload different parts of the world based on the player position.

Sample Code:

function Start () {
		// Load the level named "MyBigLevel".
		var async : AsyncOperation = Application.LoadLevelAsync ("MyBigLevel");
		yield async;
		Debug.Log ("Loading complete");
	}

Reference: http://docs.unity3d.com/ScriptReference/Application.LoadLevelAsync.html

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D – Loading and Downloading Screen – Basic