videogames development

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

Unity 3D Game Engine – JS – Basics – GameObjects – Get

Unity 3D Game Engine – JS – Basics – GameObjects – Get

A good practice is to check the properties of external GameObjects using JS.
Now, using a script inside an Empty Object we will drive other GameObjects.

Follow this steps:

1. store the GameObject inside a variable: var myobject : GameObject;
2. DRAG AND DROP from Hierarchy to Inspector var slot the object
3. Code it:
varname.property = ….
varname.component.property = …

See the JavaScript:

var myobject : GameObject; // ASSIGN IN INSPECTOR!!!!!!!

function Update () {

// NOTICE: varname.property = ....
myobject.transform.Rotate(Vector3.back, 30f * Time.deltaTime);

// NOTICE: varname.component.property = ....
myobject.renderer.material.color.a = 0.5; // set transparency to 50%


}

See API documentation at: http://docs.unity3d.com/ScriptReference/index.html

Let’s go on!

0. Project window> RMB> Import New Asset:

– Audio Clip

1. MAIN TOP MENU> GameObject> Create:

– Main Camera

– Point Light

– GUIText

– Cube
-> Transform
-> Mesh Filter
-> Box Collider
-> Mesh Renderer> add ‘material1’ (Transparent/Diffuse Shader)
-> Particle System

2. Create Empty Object, name it ‘Game Controller’
-> Audio Source without audio clip
->’GameController.JS’


#pragma strict

// create var to get some external GameObjects START #################
// ASSIGN IN INSPECTOR
var mytext : GUIText;
var myobject : GameObject;
var mycamera : Camera;
var mylight : Light;
var myaudio : AudioClip;
// create var to get some external GameObjects END ###################

function Start () {

	// instantiate objects at x=0 y=0 z=0, Quaternion.identity-> it means no rotation
	// You can use a prefab
	// Start() will be executed only once -> it will create only one clone
	Instantiate (myobject, new Vector3(0f,0f,0f), Quaternion.identity);

	// play audio clip at volume 0.7
        audio.PlayOneShot(myaudio, 0.7);
}

function Update () {

// change the property .text of GUIText
// NOTICE: varname.property = ....
mytext.text = "Change this properties using scripts!";

// rotate game object
// NOTICE: varname.property = ....
myobject.transform.Rotate(Vector3.back, 30f * Time.deltaTime);
// translate game object
myobject.transform.Translate(new Vector2(1,0)* 1f *Time.deltaTime);
// set transparency
// NOTICE: varname.component.property = ....
myobject.renderer.material.color.a = 0.5; // set transparency to 50%
// set particle system start color 
myobject.particleSystem.startColor = Color.cyan;
// disable collider
myobject.collider.enabled = false;

// change light color
mylight.light.color = Color.red;

// change camera skybox color
mycamera.camera.backgroundColor = Color.green;

} // end Update() #######################################################


3. Hierarchy DRAG AND DROP:

– Main Camera over Inspector mycamera variable
– Point Light over Inspector mylight variable
– Gui Text over Inspector mytext variable
– Cube over Inspector myobject variable
– Audio Clip over Inspector myaudio variable

4. Play

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – JS – Basics – GameObjects – Get