Unity – Scripting – OnGUI – Text Area

Unity – Scripting – OnGUI – Text Area

Videogames Development – Unity – Code GUI Objects – Input Text Area

Input Text Area

1. Assign the script to the Camera:

#pragma strict

	var stringToEdit : String = "Hello World\nI've got 2 lines...";
	function OnGUI () {
		// Make a multiline text area that modifies stringToEdit.
		stringToEdit = GUI.TextArea (Rect (10, 10, 200, 100), stringToEdit, 200);
	}

2, Play and type text inside the Text Area

Statement:
GUI.TextArea (Rect (10, 10, 200, 100), stringToEdit, 200);
GUI.TextArea (Rect (x, y, x, y), text content, max lenght);

By |Unity3D|Commenti disabilitati su Unity – Scripting – OnGUI – Text Area

Unity – Scripting – OnGUI – Box

Unity – Scripting – OnGUI – Box

Videogames Development – Unity – Code GUI Objects – Box

Input Text Area

1. Assign the script to the Camera:

#pragma strict
     
    // Draws a box of the size of the screen.
    var stringBox : String = "Hello World\nI've got 2 lines...";
	function OnGUI() {
		 GUI.Box(Rect(0,0,Screen.width,Screen.height),"This is a title");
	}

Statement:
GUI.Box(Rect(0,0,Screen.width,Screen.height),”This is a title”);
GUI.Box(Rectangle(x,y,x,y),text content);

By |Unity3D|Commenti disabilitati su Unity – Scripting – OnGUI – Box

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