0% found this document useful (0 votes)
23 views19 pages

Lab 03

Uploaded by

kaisar.kozhay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views19 pages

Lab 03

Uploaded by

kaisar.kozhay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Unity3D Scripting Applications

Prof. Monica Bordegoni


2

Input.GetKey ()
• Transform.up | transform.forward | tranform.right
Returns true while the user holds down
Manipulate the object in axis
Void update()
{ • Vector3.up | Vector3.forward | Vector3.right
PrintToConsole(); Representation of 3D vector (0,1,0)
}

Void PrintToConsole()
{
If(Input.GetKey(“Space”))
{
Debug.Log(“key is pressed down”);
}
}

1) GetKey doc s :
https ://doc s .unity3d.c om/S c riptRefere nc e/Input.GetKey.html
2) KeyC ode doc s :
https ://doc s .unity3d.c om/S c riptRefere nc e/KeyC ode.html
Exercise 3

AddRelativeForce AddRelativeTorque
Methods 4

Exec ute bloc k of c ode that makes the game engine run multiple orders :

1. Dec lare and define method


2. C all method

Parameters
if empty, there is no void loop( ) // C alling func tion
required input
Example 01 {
Return value Type Void GrabObjec t( ) // func tion dec laration if c harac ter ( pres s ed s pac e) // Pres s ed S pac e
‘Void’ s tands for no is T rue
returning value. {
{
Go to the objec t; GrabObjec t( ) ; //C all ‘GrabObjec t’
Pic k the objec t; func tion
Bring bac k the objec t; }
} Declaring function } Calling function
Methods (Functions) 5

Example 2
Float Inc rementS c ore ( int x ) // Dec lare func tion

// func tion rec eives input of type integer return output of type float

{
float y = x+1.5 ; // add 1.5 to x and s tore it in y
return ( y) ; // return output of type float
}

void loop( )
{
int a;
IncrementScore (a) //C all func tion ‘Inc rement S c ore ’

}
If Condition 6

Us e the if s tatement to s pec ify a bloc k c ode to be exec uted if a c ondition is True .

If (1st Condition)
{ If ( Time.time >3 )
//Execute this code block only {
Debug.Log ( “3 seconds has passed ”); //Execute this code block only
}
}

Else if (2 nd Contition ) // Check Second Condition If the first isn’t true Else If ( Time.time >5 )
{ {
//Execute code, if 2 nd condition is true Debug.Log ( “5 seconds has passed ”); //Execute this code block only
}
}
Else (Time.time >10 )
Else {
{ Debug.Log ( “ 10 seconds has passed ”); //Execute this code block only
// if no condition is true, execute code here }

}
C# Operators 7
8

Components
Bas e c las s for everything attac hed to a GameObjec t.

C ommon Properties and methods:

1. Get Component() : us ed to c all the attac hed c omponent to the game objec t
ex.
GetC omponent<Rigidbody>( ) .us eGravity = fals e //dis able gravity for the game objec t

2. Destroy() : Method for des troying attac hed c omponent


Ex.
Des troy( GetComponent <BoxCollider >() ) // Des troy ‘Box c ollider’ s ub- c omponent

3. Tag : get game objec t by tag

Ex. 1 check tag of game object:


if( gameObjec t.tag = = “Player”)

Ex.2 Assign tag to game object


gameObjec t.tag = “hit“;

C omponents doc s : https ://doc s .unity3d.c om/S c riptRefere nc e/C ompone nt.html
Library Math.f 9

Common Methods:

• Ceil ... Return the s malles t integer greater than or equal to f

• Floor ... Return the larges t integer s maller than or equal to f

• Clamp … C ons trains the given values between Max & Min Values .

• Lerp … Linearly c hange the value from a to b within time t.

• Pingpong .. Return a value that inc rement and dec rement between 0 and length.

• Round … return value to the neares t integer.

• Sin …. Return the s ine of the angle.

• Max .. Return max of 2 values

• Exp … Exponential

Mathf: https ://doc s .unity3d.c om/S c riptRefere nc e/Mathf.html


Light 10

Script interface for light components.


Important func tions :

1. .enabled // s et the light on

Ex.
GetC omponent<Light>( ) .enabled = true; //Boolean value

2. Color //c ontrol light c olor

Ex.
GetC omponent<Light>( ) .c olor = c olor.yellow; //c olor type value

3. Range\ //C ontrol light range

Ex.
GetC omponent<Light>( ) .range = 20; // float or integer value

4. Intensity //C ontrol light Intens ity

Ex.
GetC omponent<Light>( ) .intens ity = 5; // float or integer value

Light doc s : https ://doc s .unity3d.c om/20 21.3/Doc ume ntation/S c riptRefere nc e/Light.html
Audio 11

Example
Audio s ys tem C ons is ts bas ic ally of 3 elements : I. Add ‘Audio S ourc e C omponent’.
II. Drag and drop audioclip .
• Audio S ourc e [ Generate the audio] III. Dis able play on awake.
• Audio lis tener [ exis ts in c amera c omponent] IV. Drag and drop the c reated audio script on the game objec t.
V. Make s ure the c amera has the ‘Audio Lis tner’ C omponent.
• Audio file [ Audio as s et File]

**Us e Ogg or Mp3 formats

Common Properties
1. Play Play audio c omponent
2. Pause paus e audio c lip c omponent
3. Stop S tops the audio C lip
4. Loop Repeat/loop audio c omponent
5. Volume C ontrol the audio volume
6. isPlaying .. Is the clip playing right now

Audio ://doc s .unity3d.c om/S c riptReferenc e/AudioS ourc e.html

External tools and assets :


1. Free s ound as s ets : Frees ound.org
2. Free s ound editor & rec order: https ://www.audac ityteam.org/
12

Time.time

This is the time in seconds since the start of the application

Void update()
{
If (Time.time > 10)
{
Debug.Log(“10 Seconds is passed ”) // print in console after 10 seconds
}
}

T ime doc s : https ://doc s .unity3d.c om/S c riptRe fere nc e/T ime - time.html
13

Invoke
Delay the function call in ‘Seconds’ Quit Application
Close the application
void update()
{ void update()
Invoke ( “C hangeS c ene”, 5.0f) ; {
} if ( Input.GetKey( "es c ape") )
{
void C hangeS c ene( ) Application.Quit ();
{ }
S c eneManager.LoadS c ene( “S c ene2”) ; }
}

Invoke doc s : Quit doc s :


https ://doc s .unity3d.c om/S c riptRefere nc e/MonoBe haviour.Invoke.html https ://doc s .unity3d.c om/S c riptRefere nc e/Applic ation.Quit.html
14

Instantiate Random.Range
Create a clone ‘copy’ of the object
Instantiate method can be called in different ways. Return Random value between two values
Float xPos =Random.Range(-10.0f, 10.0f);
1. Inserting Game Object only
public static Object Instantiate(Object original); xPos will retrieve random values between -10 & 10

2. Game Object, Transform vector of the parent gameObject


public static Object Instantiate(Object original, Transform parent);

3. Game Object , Vector Position , Rotation ‘Quaternion’


public static Object Instantiate(Object original, Vector3 position, Quaternion
rotation);

Ins tantiate doc s : Random Range doc s :


https ://doc s .unity3d.c om/S c riptRefere nc e/Objec t.Ins tantiate.html https ://doc s .unity3d.c om/S c riptRefere nc e/Random.Range.html
Install TextMeshPro ‘TMP’ Package 15

1. Window >Pac kage Manager


2. S elec t ‘Unity Res itry’
3. S earc h for ‘text mes h pro’
4. Ins tall Pac kage 2 3

1 5
4
Create ‘TMP’ 16

1. C reate 3d objec t > T extMes hPRo

2
TextMeshPro 17

Text inputs
Offset position of the text in text frame

Size of the text frame in world

Font type

Font color
Scene Management 18

You c an open the s c ene by:


1. S c ene name
2. S c ene Build Index Number

S c ene Management doc s :


https ://doc s .unity3d.c om/S c riptRefere nc e/S c e neManageme nt.S c eneManager.html
Contatti
Prof. Monica Bordegoni: monica.bordegoni@polimi.it
Ahmed Hegazy: ahmedabdelmoneim.hegazy@polimi.it

You might also like