Presentation is loading. Please wait.

Presentation is loading. Please wait.

USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,

Similar presentations


Presentation on theme: "USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,"— Presentation transcript:

1 USING UNITY JAVASCRIPT

2 CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names, function names and identifiers are case sensitive. The following two variables are separate and independent of each other: var Test = 6; var test=4; Test and test will not be classified as the same variable and will cause a Javascript error Whitespace Like most programming and scripting languages, Javascript requires one space between keywords, names and identifiers. Extra spaces are ignored so you can use tabs and spaces wherever you need to make your code easier to read and debug Semi-colons Every line of Javascript must end in a semi-colon ; Blank lines don't need sem-colons.

3 Comments You can add in comments to help yourself keep track of parts of script. This does not effect the script itself and is not visible // this is a single line comment /* this is a multi-line comment */ Identifiers (Variable names, function names, labels.) The first character in an identifier name must not be a digit. Some key words include: Variables Declare variables using the keyword var. Javascript variables have no explicit data type and can contain data of any type. var mynumber;

4 EXAMPLE 1- HELLO WORLD This is a basic script. Create a new JavaScript script in Unity. Copy and paste the code and click save. #pragma strict function Start () { Debug.Log (“Hi my name is”); Debug.Log (“Welcome to programming”); } Create a new empty GameObject and attach the script to it. If you get stuck get the link to Tom’s demonstration.

5 EXAMPLE 2- MATH #pragma strict var num1 : int = 3; var num2 : int = 4; var num3 : int; var displaytext : String; //empty for now function Start () { Debug.Log("Number 1 = " +num1); Debug.Log("Number 2 = " +num2); num3 = num1 + num2; Debug.Log("Number 1 + Number 2 = " +num3); }

6 EXAMPLE 3- CHANGE COLOUR (IF STATEMENTS) #pragma strict function Start () { } function Update() { if(Input.GetKeyDown(KeyCode.R)) { gameObject.renderer.material.color = Color.red; } if(Input.GetKeyDown(KeyCode.G)) { gameObject.renderer.material.color = Color.green; } if(Input.GetKeyDown(KeyCode.B)) { gameObject.renderer.material.color = Color.blue; }

7 TYPES OF VARIABLES Optionally, you can also explicitly specify the variable type when you declare a variable. Script = JavaScript var foo : int; // foo is explicitly an integer. var bar : float; // bar is explicitly a floating-point value. var message : String; // message is explicitly a character string.

8 IF ELSE STATEMENTS When and why would you use IF ELSE statements? We use IF ELSE to make one-time decisions. That is depending on the users response some code will be executed and some code will be ignored. The if statement checks whether the text expression inside ( ) is true or not. If the test expression is true, statement/s inside the body of the IF statement is executed but if test is false, statement/s inside body of the IF is ignored. The if...else statement is used if the programmer wants to execute some statement/s when the test expression is true and execute some other statement/s if the test expression is false. This is the format for an IF ELSE statement if ( condition ) { code to be executed if condition is true; } else { code to be executed if condition is false; }

9 IF/ELSE COFFEE SCRIPT #pragma strict var coffeeTemperature:float = 85.0f; var hotLimitTemperature:float = 70.0f; var coldLimitTemperature:float = 40.0f; function Update() { if(Input.GetKeyDown(KeyCode.Space)) TemperatureTest(); coffeeTemperature -= Time.deltaTime * 5f; } function TemperatureTest() { // If the coffee's temperature is greater than the hottest drinking temperature... if(coffeeTemperature > hotLimitTemperature) { //... do this. Debug.Log("Coffee is too hot."); } // If it isn't, but the coffee temperature is less than the coldest drinking temperature... else if(coffeeTemperature < coldLimitTemperature) { //... do this. Debug.Log("Coffee is too cold."); } // If it is neither of those then... else { //... do this. Debug.Log("Coffee is just right."); }

10 IF ELSE STATEMENTS http://unity3d.com/learn/tutorials/modules/beginner/ scripting/if-statements

11 LETS TRY ONE OURSELVES Using Unity JavaScript create an IF ELSE statement regarding age. Pseudocode (coding design tool, helps me visualize what I want in my script) IF AGE >= 18 THEN PRINT “You Qualify” ELSE PRINT “You do not qualify” END IF

12 MY ANSWER #pragma strict var age= 22; function Start() { if (age >= 18) { Debug.Log ("You Qualify"); } else { Debug.Log("You do not qualify"); } } function Update() { }

13 YOUR TURN Create script using if else statements so a teacher can calculate whether their student has passed or failed their SAC. Begin with: #pragma strict function start () { } function update () { }

14 PSEUDOCODE FOR YOUR IF ELSE STATEMENT IF SCORE >= 50 THEN PRINT “You Pass” ELSE PRINT “You failed” END IF

15 FOR LOOPS FOR LOOPS through a block of code a number of times. Format: (set initial variable; condition; action on the variable) (rules, action)

16 EXAMPLE 4- LOOPING (FOR) #pragma strict var numEnemies : int = 3; function Start () { for(var i : int = 0 ; i < numEnemies ; i++) { Debug.Log("Creating enemy number: " + i); } (set initial variable; condition; action on the variable)

17 EXAMPLE 5- ROTATE OBJECT #pragma strict var variableRotationRate = 0.0f; var fixedRotationRate = 0.0f; function Update () { transform.Rotate( Vector3.up * Time.deltaTime * ( variableRotationRate * 360.0f ) ); } function FixedUpdate() { transform.Rotate( Vector3.up * Time.deltaTime * ( fixedRotationRate * 360.0f ) ); }


Download ppt "USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,"

Similar presentations


Ads by Google