SE 350 – Programming Games
description
Transcript of SE 350 – Programming Games
![Page 1: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/1.jpg)
1
SE 350 – Programming Games
Lecture 7: Programming with UnityLecturer: Gazihan Alankuş
Please look at the last slide for assignments (marked with TODO)
2/10/2012
![Page 2: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/2.jpg)
2
Outline
• Exam talk• Rest of the semester• Continuing with Unity programming
2/10/2012
![Page 3: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/3.jpg)
3
Midterm
• How was it?• Suggestions? Concerns?
2/10/2012
![Page 4: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/4.jpg)
4
Rest of the Semester
• Some homeworks, maybe quizzes?• Major focus is on developing and finishing
your games– Some weeks will be development workshops• Probably starting next week. Bring laptops!• Bring designer friends if you like.
2/10/2012
![Page 5: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/5.jpg)
5
Recap what we learned
• Game objects, components• Prefabs, assets• Classes and structs, – sharing references vs. copying
• Accessing other objects– gameObject, GetComponent<Component>(), – Standard components (transform, renderer, etc.)– GameObject.Find(“name”)– transform.parent
2/10/2012
![Page 6: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/6.jpg)
6
Recap what we learned
• Functions that you write and that Unity calls– Awake, Start, Update, OnCollisionEnter, …
• Making connections (getting references to other objects) in Awake()
• Visually decide what to do on the interface, use it to write code
• Instantiate()/Destroy() objects during runtime
2/10/2012
![Page 7: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/7.jpg)
7
Things that we will learn about
• Basic data types– Vector3 (position, direction, displacement)– Quaternion (orientation, rotation)
• Details of standard components– Transform, Collider, Renderer, RigidBody
• Basic data structures in C#– List, Dictionary, etc.
2/10/2012
![Page 8: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/8.jpg)
8
Vector3
• Vector3 means “three dimensional vector”• Just three floats: x, y and z• Often used for talking about a 3D position or
direction– Contains methods/properties that help in this
domain• e.g., transform.position is a Vector3
2/10/2012
![Page 9: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/9.jpg)
9
Vector3: related to magnitude
• v.normalized• v.magnitude• v.Normalize
2/10/2012
![Page 10: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/10.jpg)
10
Vector3: some constants
• Vector3.zero == new Vector3(0, 0, 0)• Vector3.one == new Vector3(1, 1, 1) (LOL)• Vector3.forward == new Vector3(0, 0, 1)• Vector3.back == new Vector3(0, 0, -1)• Vector3.up == new Vector3(0, 1, 0)• Vector3.down == new Vector3(0, -1, 0)• Vector3.right == new Vector3(1, 0, 0)• Vector3.left == new Vector3(-1, 0, 0) 2/10/2012
![Page 11: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/11.jpg)
11
Vector3: Averaging (interpolating)
• Vector3.Lerp(v1, v2, fraction)
• Vector3.Slerp(v1, v2, fraction)
2/10/2012
v1 v2
0 fraction 1
v1 v2
0fraction
1
origin (0, 0, 0)
![Page 12: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/12.jpg)
12
Vector3: Geometric operations
• Vector3.Scale(v, s)• Vector3.Project(v, vbase)• Vector3.Angle(v1, v2)• Vector3.Distance(v1, v2)• Vector3.Dot(v1, v2)• Vector3.Cross(v1, v2)
2/10/2012
![Page 13: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/13.jpg)
13
Vector3: operators
• v1 + v2 – Adds x, y, z values separately and returns the sum
vector• 3.5 * v– Multiplies x, y, z with 3.5
2/10/2012
![Page 14: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/14.jpg)
14
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
![Page 15: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/15.jpg)
15
Quaternion
• q.x, q.y, q.z, q.w– DON”T CARE!!!1!!!– NEVER USE THEM!!!!! >:@
2/10/2012
![Page 16: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/16.jpg)
16
Quaternion: its value that makes sense
• q.eulerAngles– Vector3 that contains rotations around axes– Rotation order is z, x, y (but you don’t have to
care)• q.ToAngleAxis(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
![Page 17: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/17.jpg)
17
Quaternion: operations• q1 * q2
– the result is the rotation that is equal to: rotate with q2 first, and then with q1
– or, if q2 is orientation of something, rotates it with q1 (same thing, slightly different interpretation)
• q * v– rotate v with q
• Quaternion.Dot(q1, q2)• Quaternion.AngleAxis(angle, axis)• Quaternion.Euler(x, y, z)• Quaternion.Inverse(q)• ...
2/10/2012
![Page 18: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/18.jpg)
18
Quaternions: Averaging (interpolating)
• Quaternion.Slerp(q1, q2, fraction)– The correct way to interpolate two quaternions
• Quaternion.Lerp(q1, q2, fraction)– Slightly faster, bad interpolation (moves faster in
the middle)
2/10/2012
![Page 19: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/19.jpg)
19
There are more
• Ray, Rect, Vector2, Vector4, etc.• Use the scripting reference– Help > Scripting Reference (in Unity window main
menu)
2/10/2012
![Page 20: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/20.jpg)
20
Standard components
• Have – Properties (variables)
• Can’t modify• Can set for instance
– Functions• Can call for instance
– Class functions (static)• Can call with class name, without an instance
– Messages• Functions are called on all components that are attached to
the GameObject when events happen
2/10/2012
![Page 21: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/21.jpg)
21
Transform• transform.position• transform.rotation
– With respect to the world. Changes when parent changes.– Not what you see in the inspector.
• transform.localPosition• transform.localRotation• transform.localScale
– 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
![Page 22: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/22.jpg)
22
Global vs. Local
2/10/2012
p
c
p.position ==
p.loca
lPositi
on
c.localPosition
c.position
c.transform.parent == p.transform
![Page 23: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/23.jpg)
23
Global vs. Local
2/10/2012
p
c
p.position == p.lo
calPosition
c.localPosition
c.position
Red ones changed as a result of moving the parent
All of this is the same for rotation and localRotation
![Page 24: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/24.jpg)
24
Renderer
• r.material• r.isVisible
2/10/2012
![Page 25: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/25.jpg)
25
Colliders
• c.isTrigger– True• Not used in physics calculations, lets things through• OnTriggerEnter is sent to the GameObject (and its
components)– False• Used in physics calculations, won’t let things through• OnCollisionEnter is sent to the GameObject (and its
components)
2/10/2012
![Page 26: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/26.jpg)
26
Colliders
• c.material– The physics material (PhysicMaterial)• friction constants• bounciness
2/10/2012
![Page 27: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/27.jpg)
27
Standard components• Have
– Properties (variables)• Can’t modify• Can set for instance
– Functions• Can call for instance
– Class functions/class variables (static)• 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 GameObject when events happen
• Examples– t.position = t.position + Vector3.up;
– t.Translate(1, 1, 1)
– Vector3.Lerp(v1, v2, f)– Vector3.up
– Update()– OnCollisionEnter() – OnCollisionEnter(Collision collision)
2/10/2012
![Page 28: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/28.jpg)
28
There are many other components
• Use the scripting reference!
2/10/2012
![Page 29: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/29.jpg)
29
Keeping Game State: Data Structures
• You will have to handle information about your game
• Examples– List of enemies– Accessing enemies by their names– Enemies sorted by their distance to the character– The friendly NPC that the enemy is chasing
2/10/2012
![Page 30: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/30.jpg)
30
Data Structures
• Hold references to other objects– GameObject enemy = GameObject.Find(“enemy”);• Remember pointer fun with binky. This is just a pointer.
2/10/2012
![Page 31: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/31.jpg)
31
Data Structures in C#• using System.Collections;
using System.Collections.Generic;
• List<GameObject> enemies = new List<GameObject>();– loop with foreach, access enemies[5]
• Dictionary<string, GameObject> enemiesByName = new Dictionary<string, GameObject>();– access by enemy name. enemiesByName[“wolf”]– loop with foreach
• Amazing resource: http://www.dotnetperls.com/• Also should watch:
http://channel9.msdn.com/Series/C-Sharp-Fundamentals-Development-for-Absolute-Beginners/Working-with-Collections-21
2/10/2012
![Page 32: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/32.jpg)
32
Game Structure• You will have GameObjects 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 MyScript instance;– public static MyScript getInstance() { return instance; }– public MyScript() { instance = this; }
• And in other scripts, – MyScript myScript = MyScript.getInstance();– Easier than GameObject.Find()
2/10/2012
![Page 33: SE 350 – Programming Games](https://reader036.fdocuments.us/reader036/viewer/2022062316/5681673d550346895ddbedb6/html5/thumbnails/33.jpg)
33
TODO: Projects
• If you are not sending reports, I think that you are not working on the projects.
• Every member of the group will send me a separate weekly report about what you did that week about the project– This is private. Do not share with others. – Just write it as an e-mail. No attaching word
documents or pdfs please!– Send them every Thursday night!
2/10/2012