unity3d

Unity3D – Scrolling Typewriter effect – JavaScript

Unity3D – Scrolling Typewriter effect – JavaScript

Create a scene with:

– TextMesh
– Empty Object, assign TypeWriter.js

#pragma strict

var displayText : TextMesh;

var textShownOnScreen: String ;
var fullText : String = "The text you want shown on screen with typewriter effect.";
var wordsPerSecond : float = 2; // speed of typewriter, words per second
var timeElapsed : float = 0;   
 
function Update()
{
    timeElapsed += Time.deltaTime;
    textShownOnScreen = GetWords(fullText, timeElapsed * wordsPerSecond);
    displayText.text = textShownOnScreen;
}
 
function GetWords(text : String, wordCount : int): String
{
    var words : int = wordCount;
 
    // loop through each character in text
    for (var i : int = 0; i < text.Length; i++)
    { 
        if (text[i] == ' ')
        {
            words--;
        }
 
        if (words <= 0)
        {
            return text.Substring(0, i);
        }
    }
 
    return text;
}

Inspector, assign a 3DText to var displayText.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Scrolling Typewriter effect – JavaScript

Unity3D – Explosion Force – JavaScript

Unity3D – Explosion Force – JavaScript

Create a scene with:

– Plane (Collider)

– Cube1 (Collider + Rigid Body)
– Cube2 (Collider + Rigid Body)
– Cube3 (Collider + Rigid Body)

– Sphere (SphereExplosion.js)

SphereExplosion.js:

#pragma strict

	var radius = 50.0; // radius of explosion
	var power = 500.0; // power of explosion
	var bombObject : GameObject; // Assign in Inspector the Bomb GameObject
	
	
function Start () {
}
	
function OnMouseDown ()
{
        // When you click over the object
        
                // Force START -----------------------------------------------------------
                // Applies an explosion force to all nearby rigidbodies
		var explosionPos : Vector3 = bombObject.transform.position;
		// Returns an array with all colliders touching or inside the sphere.
		var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
		
		for (var hit : Collider in colliders) {
			if (hit && hit.rigidbody)
				hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0);
		}
		 // Force END -------------------------------------------------------------
		
}

function Update () {
}

Inspector> Assign the Sphere Mesh to var bombObject, setup var radius and var power

Play and click over the Sphere… boom!!!

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Explosion Force – JavaScript

Unity3D – Drivable Vehicle – JavaScript – Super Easy

Unity3D – Drivable Vehicle – JavaScript – Super Easy

Create a scene with:

– (parent) CarBody (Box Object) -> Add Rigid Body -> Mass 200
– (child) CarWheelFR (Empty Object) -> Add WheelCollider
– (child) CarWheelFL (Empty Object) -> Add WheelCollider
– (child) CarWheelRR (Empty Object) -> Add WheelCollider
– (child) CarWheelRL (Empty Object) -> Add WheelCollider

CarBody, add the script:


#pragma strict

// Assign in Inspector
var CarWheelFR : WheelCollider; // Front Right
var CarWheelFL : WheelCollider; // Front Left
var CarWheelRR : WheelCollider; // Rear Right
var CarWheelRL : WheelCollider; // Rear Left

var Speed: float = 10; 
var Breaking: float = 20; 
var Turning: float = 20; 

function Start () {
}// END Start

function Update () {
	// This code makes the car go foward and backward - notare che è a trazione posteriore
	// simulate the whell rotation - spinta
	CarWheelRR.motorTorque = Input.GetAxis("Vertical")*Speed; // arrow up / down
	CarWheelRL.motorTorque = Input.GetAxis("Vertical")*Speed;
	
	// reset the variable, if you miss this code 'CarWheelRR.brakeTorque = Breaking;' for ever after get Space key button
	CarWheelRR.brakeTorque = 0;
	CarWheelRL.brakeTorque = 0;
	
	// This code makes the car turn
	// simulate the steer - sterzata
	CarWheelFR.steerAngle = Input.GetAxis("Horizontal")*Turning; // arrow left / right
	CarWheelFL.steerAngle = Input.GetAxis("Horizontal")*Turning;
	
	if (Input.GetKey(KeyCode.Space))
	{
		// simulate the brake, frenata
		CarWheelRR.brakeTorque = Breaking;
		CarWheelRL.brakeTorque = Breaking;
	}
	
}// END Update

Assign in Inspector var CarWheelFR CarWheelFL CarWheelRR CarWheelRL

Play

For italian people: come funziona?
1. Monto l’auto con il corpo e le ruote, imparentando le ruote a CarBody, come nella realtà
2. Con l’input da tastiera sfrutto le proprietà del Collider WheelCollider: .motorTorque .steerAngle .brakeTorque

Vedi API a: http://docs.unity3d.com/ScriptReference/WheelCollider.html

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Drivable Vehicle – JavaScript – Super Easy

Unity3D – Destroy if transform.position

Unity3D – Destroy if transform.position

Create a Cube and attach Cube.js:


#pragma strict

function Updare(){
	Destroy();
}

function Destroy(){
    // destroy this game object if y < 2 
    var posY : float = gameObject.transform.position.y;
    if (posY <= -2 ) {
    Destroy(gameObject);
    }
} // END Destroy

Play and move the cube under 2 units in y

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Destroy if transform.position

Unity3D Game Engine – Unity3D – Inventory System – Send and Search into Inventory – JavaScript

Unity3D Game Engine – Unity3D – Inventory System – Send and Search into Inventory – JavaScript

Create a Scene with:

– Main Camera

– GameController (Empty game object), attach:
– GameControllerScript.js
– Inventory.js

– Cube, Sphere, Cylinder, attach:
– SendToInventory.js

Inventory.js


#pragma strict

var inventory = new ArrayList(); // declare the inventory ArrayList

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

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


GameControllerScript.js


#pragma strict

var displayInventory: GUIText; // Assign in Inspector

private var inventoryScript : Inventory; // variable to store another script
 
function Awake ()
{
    inventoryScript = GetComponent(Inventory); // get script 'Inventory'  
}// END Awake()

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

function Update(){
	displayInventory.text = "Inventory: " + Array(inventoryScript.inventory).ToString();//display inventory by converting it to an array
}// END Update()


SendToInventory.js


#pragma strict

var myName : String;// to store the object name

private var inventory : Inventory; // store the GameController>Inventory component

function Start () {

	myName = this.gameObject.name; // get the object name
	Debug.Log(myName);// show the name of this object
	
	// inserisco in una variabile l'oggetto con nome GameController, lo slash serve perchè non ha parent
    var gameControllerObject : GameObject = GameObject.Find ("/GameController");
    // se l'oggetto esiste lo inserisco in una variabile
    if (gameControllerObject != null)
    {
        inventory = gameControllerObject.GetComponent (Inventory);
    }
    // se l'oggetto non esiste restituisce un messaggio di errore
    if (gameControllerObject == null)
    {
        Debug.Log ("Cannot find 'Inventory' script");
    }
	
}// END Start()

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

function OnMouseDown ()
{
        inventory.inventory.Add(myName); // GameController>Inventory component.inventoryArrayList().Add(game object name)
        Destroy(gameObject); // destroy the game object because I have already sent it to the inventory
}// END OnMouseDown()

Play, click over Sphere, Cube, Cylinder, the inventory will be updated.

For italian peolple: come funziona?

1. Inventory.js
– dichiaro Array List(), il mio inventario

2. GameControllerScript.js
– ottiene il componente Inventory.js
– visualizza il contenuto dell’inventario

3. SendToInventory.js
– ottiene il componente Inventory.js
– al click del mouse sull’oggetto, invia il nome dell’oggetto corrente all’inventario
– distrugge l’oggetto appena inserito nell’inventario, per non rischiare di inserirlo due volte. Volendo si può anche semplicemente disabilitare il Collider per evitare la registrazione di altri click.

Search inside ArrayList

Change GameControllerScript.js:


#pragma strict

var displayInventory: GUIText; // Assign in Inspector

private var inventoryScript : Inventory; // variable to store another script
 
function Awake ()
{
    inventoryScript = GetComponent(Inventory); // get script 'Inventory'  
}// END Awake()

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

function Update(){
	displayInventory.text = "Inventory: " + Array(inventoryScript.inventory).ToString();//display inventory by converting it to an array

InventorySearch();
}// END Update()

function InventorySearch(){   
    
    // search string START ---------------- 
    var howBig = inventoryScript.inventory.Count;
    var n : int = 0;
	for (n = 0; n < howBig; n++){
	   if ( inventoryScript.inventory[n] == "Cube" )
	       Debug.Log("Find Cube");
	}
    // search string END ------------------
	
}// END InventorySearch()


Conver ArrayList to Array and Search inside Array

Change GameControllerScript.js:


#pragma strict

var displayInventory: GUIText; // Assign in Inspector

private var inventoryScript : Inventory; // variable to store another script
 
function Awake ()
{
    inventoryScript = GetComponent(Inventory); // get script 'Inventory'  
}// END Awake()

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

function Update(){
	displayInventory.text = "Inventory: " + Array(inventoryScript.inventory).ToString();//display inventory by converting it to an array

InventorySearch();
}// END Update()

function InventorySearch(){   
    
    // Convert ArrayList() to Array
    var newarr = new Array (Array(inventoryScript.inventory));
    Debug.Log(newarr);
    
        // search string START ---------------- 
        var n : int = 0;
	for (n = 0; n < newarr.length; n++){
	   if ( newarr[n] == "Cube" )
	       Debug.Log("Find Cube");
	}
	// search string END ------------------
	
}// END InventorySearch()


Notice:


function InventorySearch(){    
    
    // Convert ArrayList() to Array
    var newarr = new Array (Array(inventoryScript.inventory));
    Debug.Log(newarr);
    
        // search string START ---------------- 
        var n : int = 0;
	for (n = 0; n < newarr.length; n++){
	   if ( newarr[n] == "Cube" )
	       Debug.Log("Find Cube");
	}
	// search string END ------------------
	
}// END InventorySearch()

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D Game Engine – Unity3D – Inventory System – Send and Search into Inventory – JavaScript