Lecture 2: C# Programming for VR application in Unity

50
Lecture 2: C# Programming by Making Gold Miner Game Dr. Kobkrit Viriyayudhakorn iApp Technology Limited [email protected] ITS488 (Digital Content Creation with Unity - Game and VR Programming)

Transcript of Lecture 2: C# Programming for VR application in Unity

Page 1: Lecture 2: C# Programming for VR application in Unity

Lecture 2: C# Programming by Making Gold Miner Game

Dr. Kobkrit ViriyayudhakorniApp Technology Limited

[email protected]

ITS488 (Digital Content Creation with Unity - Game and VR Programming)

Page 2: Lecture 2: C# Programming for VR application in Unity

Troubleshooting• Confuse? Read a manual at https://docs.unity3d.com/2017.1/

Documentation/Manual/UnityManual.html

• Get any problem? Google it: "DOING ANYTHING in unity"

Page 3: Lecture 2: C# Programming for VR application in Unity

Review: Writing Code in Unity

Right click at Project Window Name it as "ConsolePrinter"

Page 4: Lecture 2: C# Programming for VR application in Unity

Start writing a code• Double Click at the ConsolePrinter.cs

• The MonoDevelop-Unity IDE will be show up and you can make the first program.

Page 5: Lecture 2: C# Programming for VR application in Unity

Code structure

Page 6: Lecture 2: C# Programming for VR application in Unity

Printing a Text

Save and Go back to Unity

Page 7: Lecture 2: C# Programming for VR application in Unity

Create a New Game Object

Right click Select

Rename + Enter

Inspect Window on the right.

Page 8: Lecture 2: C# Programming for VR application in Unity

Attaching Script to A Game Object1. Drag the

ConsolePrinter script and drop onto ConsolePrinterGO in Hierarchy Window

2. Or Drag onto Inspector Window

Page 9: Lecture 2: C# Programming for VR application in Unity

Start Running

• Click on Run Button

• Switch to Console Window, you will see "Hello World!" text.

Page 10: Lecture 2: C# Programming for VR application in Unity

Print Statement

Output Console

Page 11: Lecture 2: C# Programming for VR application in Unity

Variable #1: int

Output Console

Page 12: Lecture 2: C# Programming for VR application in Unity

Variable #2: int & float & string

Output Console

Page 13: Lecture 2: C# Programming for VR application in Unity

Variable #3: int & float oper.Output Console

Page 14: Lecture 2: C# Programming for VR application in Unity

Variable #4: string & bool oper.

Output Console

Page 15: Lecture 2: C# Programming for VR application in Unity

Type Represents Range Default Value

bool Boolean value True or False FALSE

byte 8-bit unsigned integer 0 to 255 0

char 16-bit Unicode character U +0000 to U +ffff '\0’

decimal 128-bit precise decimal values with 28-29 significant digits

(-7.9 x 1028 to 7.9 x 1028) / 100 to 28 0.0M

double 64-bit double-precision floating point type

(+/-)5.0 x 10-324 to (+/-)1.7 x 10308 0.0D

float 32-bit single-precision floating point type

-3.4 x 1038 to + 3.4 x 1038 0.0F

int 32-bit signed integer type -2,147,483,648 to 2,147,483,647 0

long 64-bit signed integer type -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

0L

sbyte 8-bit signed integer type -128 to 127 0

short 16-bit signed integer type -32,768 to 32,767 0

uint 32-bit unsigned integer type 0 to 4,294,967,295 0

ulong 64-bit unsigned integer type 0 to 18,446,744,073,709,551,615 0

ushort 16-bit unsigned integer type 0 to 65,535 0

Page 16: Lecture 2: C# Programming for VR application in Unity

if-else oper.

Output Console

Page 17: Lecture 2: C# Programming for VR application in Unity

Game Planning• You are a gold miner and you want to find

a gold pit.

• You can move up, down, left, and right.

• Movement is a fixed distance.

• After each turn, your distance from the gold pit is displayed.

• You win when you get the gold pit.

• Your score is how many turns it took.

Page 18: Lecture 2: C# Programming for VR application in Unity

Rough Pseudo Code• Set Start Location

• Calculate distance from Gold pit.

• Print the distance

• Read player’s move

• Update location from the Gold pit.

• Repeat.

Page 19: Lecture 2: C# Programming for VR application in Unity

Start Writing a Game Logic

Output Console

Page 20: Lecture 2: C# Programming for VR application in Unity

If we change loc = 10.3

Output Console

Page 21: Lecture 2: C# Programming for VR application in Unity

Rough Pseudo Code• Set Start Location

• Calculate distance from Gold pit.

• Print the distance

• Read player’s move

• Update location from the Gold pit.

• Repeat.

Page 22: Lecture 2: C# Programming for VR application in Unity

Input.GetKeyDown

Page 23: Lecture 2: C# Programming for VR application in Unity

Reading User Input

Output Console (After press left arrow couple times)

Make sure you select the Game tabBefore pressing the left key. ->

Page 24: Lecture 2: C# Programming for VR application in Unity

Adding more Keydown

Output Console (After press left+right arrow couple times)

* Make sure you select the Game tabBefore pressing the left key.

Page 25: Lecture 2: C# Programming for VR application in Unity

Update Location

Output Console

Page 26: Lecture 2: C# Programming for VR application in Unity

Variable Scope: loc

Scope of loc

Page 27: Lecture 2: C# Programming for VR application in Unity

Make it class variable

Scope of loc

Move the variable declaration from within"Start()" Method to within "GoldMiner" class.(Outer the "Start()" method)

Output Console

Page 28: Lecture 2: C# Programming for VR application in Unity

Class variable assignment

• Can not be the product from the computation.

• Can be a constant or not assignment at all.

• You can re-assign the class variable in any function body.

Page 29: Lecture 2: C# Programming for VR application in Unity

Do Distance Calculation when a key is pressed.

Output Console

Now it is playable!

Page 30: Lecture 2: C# Programming for VR application in Unity

Too much duplicate code!

• Using a method / function for repeatedly run code.

• Make code much shorter. Easier to manage the code structure.

Function!

Page 31: Lecture 2: C# Programming for VR application in Unity

• Return nothing, just print the message out.

• Use "void" return type.

• Call it when you want to re-calculate the distance.

• Call it when you want to print out the distance result.

Create calculateDistance Function

Page 32: Lecture 2: C# Programming for VR application in Unity

Completed Game for 1D world.

Keep pressing right arrow

Page 33: Lecture 2: C# Programming for VR application in Unity

Programming C# for 2D world

Page 34: Lecture 2: C# Programming for VR application in Unity

Vectors

X(4,3)

y=3

x=4

(0,0)

Page 35: Lecture 2: C# Programming for VR application in Unity

Vectors Addition

ab

a+b(0,0)

Page 36: Lecture 2: C# Programming for VR application in Unity

Vectors Reduction

ab

a+b

-ba-b

(0,0)

Page 37: Lecture 2: C# Programming for VR application in Unity

Finding Distance of Two Vectors

GoldMiner

(0,0)GoldPit

- Gold Miner + Gold Pit

Page 38: Lecture 2: C# Programming for VR application in Unity

Objects and Class

Class Car

Class = Template

Object = Product

new

Page 39: Lecture 2: C# Programming for VR application in Unity

Methods and Attributes

Page 40: Lecture 2: C# Programming for VR application in Unity

Converting from 1d to 2d

• Using the Vector2 instead of float (Don’t forget the new keyword)

• Update distance to be pathToGoldPit Vector2

• Compute the distance by using Vector2 magnitude property.

• Remove all non make-sense code (written for 1d world).

Page 41: Lecture 2: C# Programming for VR application in Unity

Converting from 1d to 2d

the magnitude of vector = the length of the vector

Page 42: Lecture 2: C# Programming for VR application in Unity

Converting from 1d to 2d

Since we could not compare less than and greater than in the Vector2 class,the code in the block need to be removed, otherwise, the complication errors.

Page 43: Lecture 2: C# Programming for VR application in Unity

Update the location in Vector2

X Y

Page 44: Lecture 2: C# Programming for VR application in Unity

Testing

Haha! We forgot Up and Down button. We can not win!

Page 45: Lecture 2: C# Programming for VR application in Unity

Public variables

Inspector Panel All public variable will be debuggable in the Inspector Panel.

You can change the value and see the change in real time!

Page 46: Lecture 2: C# Programming for VR application in Unity

Exercise I• Implement Up and Down button, so the player can completed the game.

• Starting code is located at https://github.com/kobkrit/vr2017class/blob/master/exercise1.cs

Page 47: Lecture 2: C# Programming for VR application in Unity

Glossary 1Name Meaning Example

Value Numbers, text, etc. “Hello world”, 3.14f, 1

Type The “shape” of the value. int, float, string

Variable The correctly typed box for the values. int anInteger;

Statement A command to the computer. print("hello")

Expression A command that evaluates to a value.

homeLocation - location"Distance:" + distance

Page 48: Lecture 2: C# Programming for VR application in Unity

Glossary 2Name Meaning Example

Method A factory which something to input to get output. Input.GetKeyDown(...)

Arguments The inputs to a method KeyCode.RightArrow

Return value The output of a method if(Input.GetKeyDown(...))

OperationLike a method but with an

operator rather than a name. Often ‘infix’.

1 + 2

Page 49: Lecture 2: C# Programming for VR application in Unity

Glossary 3Name Meaning Example

Object A collection of variables with

values and methods that act on those variables.

The actual house.

Class The blueprint of the variables and methods.

An architect's drawing of a house.

Instantiation The process of making an object from a class. new Vector2(2.0f, 3.0f)

Instance Same as an object. Often used to say “an instance of a class X”.

The actual house according to drawings X.

Page 50: Lecture 2: C# Programming for VR application in Unity

Homework

• Can we print out the location into the console?

• If we have multiple gold pits?

• If we have traps?

• When an user fells to the traps, GAME OVER!