Video Games Development

Unity3D Game Engine – Get Objects with Find.name / FindWithTag / FindGameObjectsWithTag

Unity3D Game Engine – Get Objects with Find.name / FindWithTag / FindGameObjectsWithTag

Get Objects – Find.name

Restituisce un oggetto in base al nome che ha in Hierarchy

1. Create inside Hierarchy:

– parent ->’Arm’ (Cube) braccio
– child of Arm ->’Forearm’ (Cube) avambraccio
– child of Forearm ->’Hand’ (Cube) mano

– GameController(Empty Object) -> attach GameController.JS

2. GameController.JS:


#pragma strict

        // Devo associare i Gameobject a delle variabili
        // E' meglio private perchè non devo modificarla in Inspector
	private var hand : GameObject;
	private var arm : GameObject;

function Start () {
        // Hand non ha lo slash / all'inizio perchè ha un oggetto parent
	hand = GameObject.Find("Hand");
	// Arm ha lo slash / all'inizio perchè non ha un oggetto parent
	arm = GameObject.Find("/Arm");
}

function Update () {
        // Ruoto i singoli elementi
	hand.transform.Rotate(0, 100 * 10* Time.deltaTime, 0);
	arm.transform.Rotate(0, 100 * Time.deltaTime, 0);

}

Official docs:
GameObject.Find: http://docs.unity3d.com/ScriptReference/GameObject.Find.html

Get Objects – GameObject.FindWithTag

Restituisce SOLO L’ULTIMO OGGETTO a cui è stato assegnato un determinato tag.

1. Create inside Hierarchy:

– Hand1′ (Cube) -> Inspector assign Tag ‘MyHand’

– Hand2 (Cube) -> Inspector assign Tag ‘MyHand’

– GameController(Empty Object) -> attach GameController.JS

2. GameController.JS:


#pragma strict

// definisco la variabile per ottenere GameObject
private var hand : GameObject;

function Start () {
    // inserisco in una variabile l'oggetto con tag 'MyHand'
    // ritorna SOLO l'ultimo oggetto a cui è stato assegnato il tag
    hand =  GameObject.FindWithTag ("MyHand");
    
    // se l'oggetto con tag non esiste restituisce un messaggio di errore
    if (hand == null)
    {
        Debug.Log ("Cannot find 'MyHand'");
    }
}

function Update () {
        // Ruota
	hand.transform.Rotate(0, 100 * 10* Time.deltaTime, 0);

}

3. Play, ruota SOLO L’ULTIMO OGGETTO CON IL TAG

Official docs:
http://docs.unity3d.com/ScriptReference/GameObject.FindWithTag.html

Get Objects – GameObject.FindGameObjectsWithTag

Restituisce in un array di oggetti, TUTTI GLI OGGETTI a cui è stato assegnato un determinato tag.

1. Create inside Hierarchy:

– Hand1′ (Cube) -> Inspector assign Tag ‘MyHand’

– Hand2 (Cube) -> Inspector assign Tag ‘MyHand’

– GameController(Empty Object) -> attach GameController.JS

2. GameController.JS:


#pragma strict

// definisco la variabile per ottenere GameObject
// è un array di oggetti perchè potrebbero essere più di uno
var hands : GameObject[];

function Start () {
    // inserisco in una variabile l'oggetto con tag 'MyHand'
    // ritorna TUTTI gli oggetti a cui è stato assegnato il tag
    hands =  GameObject.FindGameObjectsWithTag ("MyHand");
}

function Update () {
    // Ruota gli oggetti contenuti nell'array hands[]
    hands[0].transform.Rotate(Vector3.back, 10 * Time.deltaTime);
    hands[1].transform.Rotate(Vector3.back, 10 * Time.deltaTime);
}

3. Play, ruotano TUTTI GLI OGGETTI CON IL TAG

Official docs:
http://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D Game Engine – Get Objects with Find.name / FindWithTag / FindGameObjectsWithTag

Unity 3D – Hide Mouse Cursor

Unity 3D – Hide Mouse Cursor

1. Load a Scene

2. Select MainCamera, assign HideCursor.JS

#pragma strict
  
function Start () {  
	// Hide the cursor
	Screen.showCursor = false;	
}
   

function Update () {  
}


3. Build it and run

NOTICE: If you try the game during production you will see the mouse cursor! You have to build to see the final result!

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D – Hide Mouse Cursor

Unity 3D Game Engine – Frames per Seconds – JavaScript

Unity 3D Game Engine – Frames per Seconds – JavaScript

1. Create GUIText and attach:


// Attach this to a GUIText to make a frames/second indicator.
//
// It calculates frames/second over each updateInterval,
// so the display does not keep changing wildly.
//
// It is also fairly accurate at very low FPS counts (<10).
// We do this not by simply counting frames per interval, but
// by accumulating FPS for each frame. This way we end up with
// correct overall FPS even if the interval renders something like
// 5.5 frames.
 
var updateInterval = 0.5;
 
private var accum = 0.0; // FPS accumulated over the interval
private var frames = 0; // Frames drawn over the interval
private var timeleft : float; // Left time for current interval
 
function Start()
{
    if( !guiText )
    {
        print ("FramesPerSecond needs a GUIText component!");
        enabled = false;
        return;
    }
    timeleft = updateInterval;  
}
 
function Update()
{
    timeleft -= Time.deltaTime;
    accum += Time.timeScale/Time.deltaTime;
    ++frames;
 
    // Interval ended - update GUI text and start new interval
    if( timeleft <= 0.0 )
    {
        // display two fractional digits (f2 format)
        guiText.text = "" + (accum/frames).ToString("f2");
        timeleft = updateInterval;
        accum = 0.0;
        frames = 0;
    }
}

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Frames per Seconds – JavaScript

Unity 3D – Mouse Drag to Rotate GameObject

Unity 3D – Drag to Rotate GameObject

1. Create a Cube, assign RotateObject.js:


var clickPos 	: Vector2;
var offsetPos	: Vector2;
var divider 	= 80;

function Start()
{
	clickPos = Vector2(0,0);
	offsetPos = Vector2(0,0);
}

function Update () {

	offsetPos = Vector2(0,0);
	
	if(Input.GetKeyDown(leftClick()))
	{
		clickPos = mouseXY();
	}
	
	if(Input.GetKey(leftClick()))
	{
		offsetPos = clickPos - mouseXY();
	}
	
	// Rotate the GameObject
	transform.Rotate(Vector3(-(offsetPos.y/divider),offsetPos.x/divider,0.0), Space.World);
}

// Debug Code: Prints the current mouse position
function OnGUI ()
{
	/*GUI.Label(Rect(10,350,200,100), "mouse X = " + Input.mousePosition.x);
	GUI.Label(Rect(10,370,200,100), "mouse Y = " + Input.mousePosition.y);
	
	GUI.Label(Rect(120,350,200,100), "click X = " + clickPos.x);
	GUI.Label(Rect(120,370,200,100), "click Y = " + clickPos.y);
	
	GUI.Label(Rect(210,350,200,100), "offset X = " + offsetPos.x);
	GUI.Label(Rect(210,370,200,100), "offset Y = " + offsetPos.y);*/
}

// Return true when left mouse is clicked or hold
function leftClick()
{
	return KeyCode.Mouse0;
}

//Immediate location of the mouse
function mouseXY()
{
	return Vector2(Input.mousePosition.x, Input.mousePosition.y);
}

//Immediate location of the mouse's X coordinate
function mouseX()
{
	return Input.mousePosition.x;
}

//Immediate location of the mouse's Y coordinate
function mouseY()
{
	return Input.mousePosition.y;
}

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D – Mouse Drag to Rotate GameObject

Unity 3D Game Engine – Camera Facing Billboard

Unity 3D Game Engine – Camera Facing Billboard

This is useful for billboards which should always face the camera and be the same way up as it is.

1. Hierarchy create the structure:

– Main Camera

– Empty Object (parent)
– Plane (child)

2. Rotate Plane to face the Main Camera

3. Select Empty Object and assign CameraFacingBillboard.JS:


#pragma strict

var m_Camera : Camera ; // Assign in Inspector

function Start () {

}

function Update () {
	transform.LookAt(transform.position + m_Camera.transform.rotation * Vector3.forward,
			                 m_Camera.transform.rotation * Vector3.up);

}

4. Hierarchy> DRAG AND DROP Main Camera over Inspector> CameraFacingBillboard.JS, Camera var

5. Run to see the final result

Original Article: http://wiki.unity3d.com/index.php?title=CameraFacingBillboard

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Camera Facing Billboard