Unity – Scripting – OnGUI – Buttons

Unity – Scripting – OnGUI – Buttons

Videogames Development – Unity – Code GUI Objects – Buttons

UnityGUI allows you to create a wide variety of highly functional GUIs very quickly and easily. Rather than creating a GUI object, manually positioning it, and then writing a script that handles its functionality, you can do everything at once with just a few lines of code. The code produces GUI controls that are instantiated, positioned and handled with a single function call.

Simple Text Button

1. Assign the script to the Camera:

#pragma strict

function Start () {

}

function OnGUI () {
	if (GUI.Button (Rect (10,10,150,100), "I am a button")) {
		print ("You clicked the button!");
	}
}

function Update () {

}

Statement:
GUI.Button (Rect (10,10,150,100), “I am a button”)
GUI.Button (Rectangular (x topleft,y topleft,x bottomright,y bottomright), “Button Text”)

Image Button

1. Assign the script to the Camera:

#pragma strict

	var btnTexture : Texture;
	function OnGUI() {
                // Texture Controller
		if (!btnTexture) {
			Debug.LogError("Please assign a texture on the inspector");
			return;
		}
		if (GUI.Button(Rect(10,10,150,100),btnTexture))
			Debug.Log("Clicked the button with an image");
	}

2. Select the Camera> ‘Inspector’> Script> DRAG AND DROP Texture from ‘Assets’ to Script> ‘Btn Texture’ variable.

3. Play and enjoy!

Multiple Buttons

1. Assign the script to the Camera:

Left Side Bar:

#pragma strict

	function OnGUI() {

        // Multiple Buttons - Left Side Bar
		if (GUI.Button(Rect(10,10,50,50),"BTN1"))
			Debug.Log("Clicked BTN1");
		if (GUI.Button(Rect(10,70,50,50),"BTN2"))
			Debug.Log("Clicked BTN2");
		if (GUI.Button(Rect(10,130,50,50),"BTN3"))
			Debug.Log("Clicked BTN3");
		if (GUI.Button(Rect(10,190,50,50),"BTN4"))
			Debug.Log("Clicked BTN4");
	}

Top Left Bar:

#pragma strict

	function OnGUI() {

        // Multiple Buttons - Top Left Bar
		if (GUI.Button(Rect(10,10,50,50),"BTN1"))
			Debug.Log("Clicked BTN1");
		if (GUI.Button(Rect(70,10,50,50),"BTN2"))
			Debug.Log("Clicked BTN2");
		if (GUI.Button(Rect(130,10,50,50),"BTN3"))
			Debug.Log("Clicked BTN3");
		if (GUI.Button(Rect(190,10,50,50),"BTN4"))
			Debug.Log("Clicked BTN4");
	}
By |Unity3D|Commenti disabilitati su Unity – Scripting – OnGUI – Buttons

UNITY – 3D Physic – Joints

UNITY – 3D Physic – Joints

Using Joints

Fixed Joint – Spring Joints

1. MAIN TOP MENU> GameObject> Capsule1
– Capsule Collider (Inspector> ‘Add Component’> Physics> Capsule Collider)
– Rigid Body (Inspector> ‘Add Component’> Physics> Rigid Body)
– Fixed Joint (Inspector> ‘Add Component’> Physics> Fixed Joint)

Inspector ‘Add Component’>

2. In the viewport under Capsule 1 create
MAIN TOP MENU> GameObject> Capsule2
– Capsule Collider (Inspector> ‘Add Component’> Physics> Capsule Collider)
– Rigid Body (Inspector> ‘Add Component’> Physics> Rigid Body)
– Spring Joint> Connected Body> Capsule1 (Inspector> ‘Add Component’> Physics> Spring Joint)

3. In the viewport under Capsule 2 create
MAIN TOP MENU> GameObject> Capsule3
– Capsule Collider
– Rigid Body
– Spring Joint> Connected Body> Capsule2

The final structure is:

Capsule1 -> Collider + Rigid Body + Fixed Joint
|
Capsule2 -> Collider + Rigid Body + Spring Joint
|
Capsule3 -> Collider + Rigid Body + Spring Joint

Sping Joint Parameters

Anchor: setup the pivot point of the joint (the orange dot in the scene view)

Spring: (molla) 1= soft spring (molla debole), 10= tight spring (molla più rigida), infinity= no spring (molla infinitamente rigida)

Damper: (ammortizzatore)

Min Distance: minimum lenghth of the spring

Max Distance: maximun lenghth of the spring

Break Force: forza da applicare per rompere il vincolo

Break Torque: momento torcente da applicare per rompere il vincolo

Hinge Joints

Hinge Joints are useful to make doors, windows etc..

Create a Box -> apply: Collider + Rigid Body + Hinge Joint

Hinge Joint Parameters

Anchor: setup the pivot point of the joint (the orange dot in the scene view)

Axis: the joint axis, example: to open a door X=0 Y=1 Z=0

Motor: example-> move a automatic gate-> check ‘Use Motor’, Target Velocity:5 Force:4

Limits: example-> limit the door opening-> check ‘Use Limits’, Limits Min:0 Max:90

Break Force: forza da applicare per rompere il vincolo

Break Torque: momento torcente da applicare per rompere il vincolo

By |Unity3D|Commenti disabilitati su UNITY – 3D Physic – Joints

Unity – Physics 3D – Basics

Unity – Physics 3D – Basics

Videogames Development – Unity – How to add Physics 3D

ADD PHYSICS

Setup the Physics 3D engine

MAIN TOP MENU> Edit> Project Settings> Physics> Inspector> Gravity (default: T=-9.81 ms2)

Create a Sphere over a Box, the Sphere will fall and will collide overe the box

1. Hierarchy> select Sphere
2. MAIN TOP MENU> Component> Physics> Rigidbody
3. Hierarchy> select Sphere> Inspector> Rigidbody> setup parameters
4. Hierarchy> select Sphere> Inspector> ‘Add Component Button’> Physics> Mesh Collider
5. Hierarchy> select Sphere> Inspector> Mesh Collider> setup parameters

6. TOP CENTER> click over ‘Play’ button, the Sphere will fall
7. TOP CENTER> click over ‘Play’ button, to stop simulation

8. Hierarchy> select Box
9. MAIN TOP MENU> Component> Physics> Box Collider
10. Hierarchy> select Box> Inspector> Box Collider> setup parameters

11. MAIN TOP MENu> Assets> Create> Physics Material
12. ‘Assets’ window> select New Physics Material> Inspector> setup Friction / Bounciness

13. Hierarchy> select Sphere> Mesh Collider> Material> New Physics Material, assign material
14. Hierarchy> select Box> Box Collider> Material> New Physics Material, assign material

15. TOP CENTER> click over ‘Play’ button, now there is collision
16. TOP CENTER> click over ‘Play’ button, to stop simulation

Colliders

Inspector> ‘Add Component’> Physics

– Box Collider
– Sphere Collider
– Capsule Collider

– Mesh Collider

– Whell Collider

– Terrain Collider

COMPLEX MESH

To simulate Physic with complex mesh you can:

– use a group of simple colliders to simulate the shape (Create a group using parent-child features inside Hierarchy)
– use a low resolution mesh + Mesh Collider

MESH COLLIDER DOES NOT WORK!

The collide engine uses the polygon face normals to simulate collisions. To revert normal try:

Hierarchy> Select mesh> Mesh Collider> check ‘Convex’, re-simulate

Rigid Bodies Properties

Inspector> Rigidbody

Mass: maggiore massa, minore reazione alla collisione

Drag: resistenza al movimento, maggiore resistenza, più velocemente arriva allo stato di quiete

Angular Drag: resistenza angolare al movimento, maggiore resistenza, più velocemente arriva allo stato di quiete

Use Gravity: you can setup gravity MAIN TOP MENU> Edit> Project Settings> Physics> Gravity
– default: Y= -9.81 ms2
– platform: low gravity it is great!
– special effects: add agravity in X or Z

Is Kinematic: se è selezionato, l’oggetto può essere manipolato con muovi-ruota-scala, influenza la fisica degli altri oggetti ma la fisica non lo influenza. Un esempio di oggetto ‘Is Kinematic’ è la racchetta del gioco PONG o di BREAKOUT. Questo oggetto NON forza il ricalcolo della fisica ad ogni frame.

Interpolate:
– None (default)
– Interpolate: smussa il movimento in base al frame precedente
– Extrapolate: smussa il movimento in base al frame previsto successivo

Collision Detection:
– Discrete (default)
– Continuos: it is for fast moving objects that are interacting with static geometry
– Continuos Dynamic: it is for fast moving objects that are interacting with other dynamic objects

Constraints:
Blocca la rotazione o la posizione di un oggetto durante una reazione alla fisica. Ad esempio per un TETRIS i tasselli possono cadere, impilarsi ma non ruotare.
– Position XYZ
– Rotation XYZ

By |Unity3D|Commenti disabilitati su Unity – Physics 3D – Basics

UNITY – Cameras

UNITY – Cameras

Create a Camera

1. MAIN TOP MENU> GameObjects> Create Other> Camera

2. Hierarchy> Main Camera> Inspector

– Projection: Perspective (3D Games) / Orthographic (2D Games and Isometric)

– Clipping Planes: Near / Far (to optimize rendering performance)

– Field of View (zoom)

– Background: BG color / Skybox (MAIN TOP MENU> Edit> Rendering> Skybox)

– Culling Mask: (to render only some layers)

Multiple Camera – Picture in Picture

1. Create the second Camera with different point of view

2. Select the Main Game Camera> Inspector> Camera> Clear Flags> Depth only

3. Select the secondary Camera> Inspector> Camera> Clear Flags> Depth only

By |Unity3D|Commenti disabilitati su UNITY – Cameras

UNITY – Add 3D Lights

UNITY – Add 3D Lights

Ambient Light

MAIN tOP MENU> Edit> Render Settings> Ambient Light

Dynamic Light – Realtime

MAIN TOp MENU> GameObject> Create Others>

– Directional light -> sun

– Point Light -> bulb lamp

– Spot Light -> spot light

Hierarchy> Select the light> Inspector:

– Color

– Intensity

– Cookie: you can create a pattern of shadow with the alpha channel of a Texture

– Shadows: No / Hard / Soft

– Halo
-> può essere sostituito da ‘Add Component’> Effects> Halo, poi deselezionare il vecchio “Draw Halo” assegnato di default

– Flare: click over the small icon on the right> Assets> select a Flare
-> può essere sostituito da ‘Add Component’> Effects> Flare> click over the small icon on the right> Assets> select a Flare, poi svuotare lo slot del vecchio “Flare” assegnato di default
To render Flare Component you need Hierarchy> select Camera> ‘Add Component’> Rendering> Flare Layer

Backed Light – Pre Render and Baked over textures

ONLY BACKED LIGHT:

– MAIN TOP MENU> GameObject> Create Others> Area Light

– Shaders> Self Illumination

Render Mode

– Vertex Lighting
-> fastest
-> it interpolare lighting over the surface of the mesh
-> it NOT allow Normal mapping / Light Cookies / Real Time Shadows

– Per pixel Lighting
-> best quality
-> it calculate lighting in every screen pixel
-> it allow Normal mapping / Light Cookies / Real Time Shadows

Setup:

MAIN TOP MENU> Edit> Project Settings> Quality> Pixel Light Count, maggiore valore, maggiore qualità, minore velocità di rendering

Hierarchy> select Light> Inspector> Render Mode> Important (Per pixel Lighting) / Not important (Vertex Lighting)

By |Unity3D|Commenti disabilitati su UNITY – Add 3D Lights