SE 320 Introduction to Game Development Lecture 7

  • Slides: 33
Download presentation
SE 320 – Introduction to Game Development Lecture 7: Programming Lecturer: Gazihan Alankuş Please

SE 320 – Introduction to Game Development Lecture 7: Programming Lecturer: Gazihan Alankuş Please look at the last two slides for assignments (marked with TODO) 1

Sample Games in Homework • URL was: http: //u 3 d. as/content/m 2 h/c-gameexamples/1

Sample Games in Homework • URL was: http: //u 3 d. as/content/m 2 h/c-gameexamples/1 s. G • What were things that you found interesting? • What have you learned? • What were your issues? – Let’s quickly go over the games – Jump in when we are close to your question! 2

How does a game work? • You write code – that is able to

How does a game work? • You write code – that is able to manipulate the game world • That code is ran – at certain points in time • You manipulate game state 3

Game State • Examples – HP (life) – Ammo – Inventory items – Cool-down

Game State • Examples – HP (life) – Ammo – Inventory items – Cool-down time for a skill – Experience • More examples from actual games 4

Implementing State • Variables in your custom components public class Life. Manager : Mono.

Implementing State • Variables in your custom components public class Life. Manager : Mono. Behaviour { public float HP; } • The instance of the component will reflect state of the game object that it is attached to. – Different instances of your prefab will have different values for the state variables 5

Implementing State • Locality of state – which component should keep which state? –

Implementing State • Locality of state – which component should keep which state? – One component that keeps HPs of all characters in the game? – Every character keeps his own HP? 6

Accessing Objects in the Scene • Getting another game object – Game. Object enemy

Accessing Objects in the Scene • Getting another game object – Game. Object enemy = Game. Object. Find(“enemy”); //or the one below – public Game. Object enemy; //drag to this • Getting a component – Attached to the same game object • My. Component c = Get. Component<My. Component>(); – Attached to some other game object • My. Component c = enemy. Get. Component<My. Component>(); • Using parent/child relationships – Transform parents. Transform = transform. parent; – Game. Object parent = transform. parent. game. Object; 2/10/2012 7

Accessing Objects in the Scene • It’s slow to keep calling Game. Object. Find(“.

Accessing Objects in the Scene • It’s slow to keep calling Game. Object. Find(“. . ”) • Make connections in the Awake function public class Player: Mono. Behaviour { Game. Object enemy; void Awake() { enemy = Game. Object. Find("enemy"); } } 8

Data Types in Unity • Some standard data types – Vector 3 (position, direction,

Data Types in Unity • Some standard data types – Vector 3 (position, direction, displacement) – Quaternion (orientation, rotation) • Details of standard components – Transform, Collider, Renderer, Rigid. Body • Basic data structures in C# – List, Dictionary, etc. 2/10/2012 9

Vector 3 • Vector 3 means “three dimensional vector” • Just three floats: x,

Vector 3 • Vector 3 means “three dimensional vector” • Just three floats: x, y and z • Often used for talking about a 3 D position or direction – Contains methods/properties that help in this domain • e. g. , transform. position is a Vector 3 2/10/2012 10

Vector 3: related to magnitude • v. normalized • v. magnitude • v. Normalize

Vector 3: related to magnitude • v. normalized • v. magnitude • v. Normalize 2/10/2012 11

Vector 3: some constants • • Vector 3. zero == new Vector 3(0, 0,

Vector 3: some constants • • Vector 3. zero == new Vector 3(0, 0, 0) Vector 3. one == new Vector 3(1, 1, 1) (LOL) Vector 3. forward == new Vector 3(0, 0, 1) Vector 3. back == new Vector 3(0, 0, -1) Vector 3. up == new Vector 3(0, 1, 0) Vector 3. down == new Vector 3(0, -1, 0) Vector 3. right == new Vector 3(1, 0, 0) Vector 3. left == new Vector 3(-1, 0, 0) 2/10/2012 12

Vector 3: Averaging (interpolating) • Vector 3. Lerp(v 1, v 2, fraction) v 1

Vector 3: Averaging (interpolating) • Vector 3. Lerp(v 1, v 2, fraction) v 1 v 2 0 fraction 1 • Vector 3. Slerp(v 1, v 2, fraction) v 1 0 v 2 fraction 1 origin (0, 0, 0) 2/10/2012 13

Vector 3: Geometric operations • • • Vector 3. Scale(v, s) Vector 3. Project(v,

Vector 3: Geometric operations • • • Vector 3. Scale(v, s) Vector 3. Project(v, vbase) Vector 3. Angle(v 1, v 2) Vector 3. Distance(v 1, v 2) Vector 3. Dot(v 1, v 2) Vector 3. Cross(v 1, v 2) 2/10/2012 14

Vector 3: operators • v 1 + v 2 – Adds x, y, z

Vector 3: operators • v 1 + v 2 – Adds x, y, z values separately and returns the sum vector • 3. 5 * v – Multiplies x, y, z with 3. 5 2/10/2012 15

Quaternion • Has four values (x, y, z, w). What are they? – Don’t

Quaternion • Has four values (x, y, z, w). What are they? – Don’t worry about its mathematical definition (has four values, etc. don’t care because they won’t make sense in our context) • Is simply (both of these below are true) – A vector and an angle • Rotate around that vector with that angle – Three consecutive rotations around z, x and y axes • Represents – Orientation (rotate to here from default orientation) – Rotation (change in orientation) 2/10/2012 16

Quaternion • q. x, q. y, q. z, q. w – DON”T CARE!!!1!!! –

Quaternion • q. x, q. y, q. z, q. w – DON”T CARE!!!1!!! – NEVER USE THEM!!!!! >: @ 2/10/2012 17

Quaternion: its value that makes sense • q. euler. Angles – Vector 3 that

Quaternion: its value that makes sense • q. euler. Angles – Vector 3 that contains rotations around axes – Rotation order is z, x, y (but you don’t have to care) • q. To. Angle. Axis(out angle, out axis) – sets the angle and axis variables that you give to it – rotation around that axis with that angle amount 2/10/2012 18

Quaternion: operations • q 1 * q 2 – the result is the rotation

Quaternion: operations • q 1 * q 2 – the result is the rotation that is equal to: rotate with q 2 first, and then with q 1 – or, if q 2 is orientation of something, rotates it with q 1 (same thing, slightly different interpretation) • q*v – rotate v with q • • • Quaternion. Dot(q 1, q 2) Quaternion. Angle. Axis(angle, axis) Quaternion. Euler(x, y, z) Quaternion. Inverse(q). . . 2/10/2012 19

Quaternions: Averaging (interpolating) • Quaternion. Slerp(q 1, q 2, fraction) – The correct way

Quaternions: Averaging (interpolating) • Quaternion. Slerp(q 1, q 2, fraction) – The correct way to interpolate two quaternions • Quaternion. Lerp(q 1, q 2, fraction) – Slightly faster, bad interpolation (moves faster in the middle) 2/10/2012 20

There are more • Ray, Rect, Vector 2, Vector 4, etc. • Use the

There are more • Ray, Rect, Vector 2, Vector 4, etc. • Use the scripting reference 2/10/2012 21

Standard components • Have – Properties (variables) • Can’t modify • Can set for

Standard components • Have – Properties (variables) • Can’t modify • Can set for an instance – Functions • Can call for an instance – Class functions (static) • Can call with the class name, without an instance – Messages • Functions are called on all components that are attached to the Game. Object when events happen (via Send. Message) 2/10/2012 22

Transform • transform. position • transform. rotation – With respect to the world. Changes

Transform • transform. position • transform. rotation – With respect to the world. Changes when parent changes. – Not what you see in the inspector. • transform. local. Position • transform. local. Rotation • transform. local. Scale – With respect to the parent. Does not change when parent changes. – What you see in the inspector • transform. right, transform. up, transform. forward – x, y, z axes of the orientation (the arrows you see) 2/10/2012 23

Global vs. Local c. transform. parent == p. transform ion c. local. Posit c

Global vs. Local c. transform. parent == p. transform ion c. local. Posit c c. n tio i s po p. po sit ion == p. loc al. P os iti on p 2/10/2012 24

Global vs. Local Red ones changed as a result of moving the parent All

Global vs. Local Red ones changed as a result of moving the parent All of this is the same for rotation and local. Rotation c. local. Posit on c iti os l. P = n= a loc. p p io sit o p p. ion c. posit 2/10/2012 25

Renderer • r. material • r. is. Visible 2/10/2012 26

Renderer • r. material • r. is. Visible 2/10/2012 26

Colliders • c. is. Trigger – True • Not used in physics calculations, lets

Colliders • c. is. Trigger – True • Not used in physics calculations, lets things through • On. Trigger. Enter is sent to the Game. Object (and its components) – False • Used in physics calculations, won’t let things through • On. Collision. Enter is sent to the Game. Object (and its components) 2/10/2012 27

Colliders • c. material – The physics material (Physic. Material) • friction constants •

Colliders • c. material – The physics material (Physic. Material) • friction constants • bounciness 2/10/2012 28

Standard components • Have – Properties (variables) • Can’t modify • Can set for

Standard components • Have – Properties (variables) • Can’t modify • Can set for instance • Examples – t. position = t. position + Vector 3. up; – Functions – t. Translate(1, 1, 1) – Class functions/class variables (static) – Vector 3. Lerp(v 1, v 2, f) – Vector 3. up • Can call for instance • Can use with class name, without an instance. Like global variables but in class. – Messages • Functions are called on all components that are attached to the Game. Object when events happen 2/10/2012 – Update() – On. Collision. Enter(Collision collision) 29

There are many other components • Use the scripting reference! 2/10/2012 30

There are many other components • Use the scripting reference! 2/10/2012 30

Collection Data Structures in C# • using System. Collections; using System. Collections. Generic; •

Collection Data Structures in C# • using System. Collections; using System. Collections. Generic; • List<Game. Object> enemies = new List<Game. Object>(); – loop with foreach, access enemies[5] • Dictionary<string, Game. Object> enemies. By. Name = new Dictionary<string, Game. Object>(); – access by enemy name. enemies. By. Name[“wolf”] – loop with foreach • Amazing resource: http: //www. dotnetperls. com/ • Also should watch: http: //channel 9. msdn. com/Series/C-Sharp. Fundamentals-Development-for-Absolute-Beginners/Working-with. Collections-21 31

Game Structure • You will have Game. Objects for various elements in your game

Game Structure • You will have Game. Objects for various elements in your game (character, enemy, robot, etc). Create their own scripts and put code specific for them. • You also need to have code that is common for the game (scoring is an example). Attach such scripts on the camera or on a similarly unique object. • Wire them up in Awake() • For common singletons, create – static My. Script instance; – public static My. Script get. Instance() { return instance; } – public My. Script() { instance = this; } • And in other scripts, – My. Script my. Script = My. Script. get. Instance(); – Easier than Game. Object. Find() 2/10/2012 32

TODO: Homework • • • Start with your own Homework 3 (ask the assistant

TODO: Homework • • • Start with your own Homework 3 (ask the assistant if you haven’t done it) Add a bullet prefab that you will use to make your character throw bullets You already should have a monster prefab. Add a 3 D text over its head that you will use to display the monster’s remaining life percentage. Create a Monster. Life. Manager component that will handle changing and displaying life of the monster that it’s attached to. Make it so that the space key will throw a bullet towards which your character is facing (transform. forward). The bullet needs to remember this initial direction and should move proportional to time at each frame (update) towards this direction. When a bullet hits a monster, it should reduce its life by a random value between 5 and 10. Initially life is 100. When life gets to zero, the monster disappears. You should have at least three monsters in the scene Bonus: reduce monster’s life depending on the body part that is hit (headshot, etc. ) Get Özkan to grade it – – – ozkansayin. ieu@gmail. com Subject (paste this): SE 320 Homework 7 What to send: • • Assets -> Export package File -> Build Settings -> Web player -> Build 33