330 likes | 497 Views
SE 320 – Introduction to Game Development. Lecture 5: Programming in Unity & Developing Simple Games Lecturer: Gazihan Alankuş. Please look at the last two slides for assignments (marked with TODO ). Outline. Reminder about weekly reports Quiz Programming in Unity
E N D
SE 320 – Introduction to Game Development Lecture 5: Programming in Unity & Developing Simple Games Lecturer: GazihanAlankuş Please look at the last two slides for assignments (marked with TODO)
Outline • Reminder about weekly reports • Quiz • Programming in Unity • Where? (where to put your code) • What? (what the code can do) • When? (when the code is executed) • Scripting basics
Quiz • Turn off your monitors • Do not speak • Violators’ papers will not be accepted • 5 min • Violators’ papers will be marked as late • http://homes.ieu.edu.tr/~galankus/teaching/12fall/se320/material/day5/
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 • Let’s try to imagine this in example games
The Code • Where do you write it into? • What can you do with it?
The Code • Where do you write it into? • What can you do with it?
Where do you write code into? • Create a new C# script • This becomes a new component that you can add to game objects just like other components. • The code in it is not executed if it is not added to a game object. • Double click on it in the Project pane to open it in MonoDevelop • Changes are reflected instantly inside Unity • We will talk more about IDEs and debugging in the future
The Code • Where do you write it into? • What can you do with it?
What can you do with the code? • You can do everything that you could manually do using hierarchy/inspector. • Move things • Rotate things • Resize things • Check values of things • Change colors of things • Create/destroy things
What can you do with the code? • You can do everything that you could manually do using hierarchy/inspector. • But how??? • Help->Scripting Reference (or online at http://docs.unity3d.com/Documentation/ScriptReference/ ) • Your custom component has everything that the MonoBehavior class has • Most of the standard components are easily reachable as fields (transform, rigidbody, etc.)
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
How does a game work? • Initialization • Code that runs when the game runs • Animations • Code that runs every frame • User input • Code that runs when the user does something • In-game events • Code that runs when something interesting happens in the game
How are these implemented? • You fill in predefined functions that Unity will call when the right time comes. • These functions are also called “event handlers”. • Unity knows these functions by name. If you provide the function in your script, Unity will call it when the right time comes.
Some predefined functions that Unity calls • Awake • Start • Update • OnCollisionEnter • http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.html provides the whole list
Predefined variables of MonoBehavior • Similarly, your script has access to some predefined variables (through the MonoBehavior class) • name • gameObject • enabled • transform • renderer • http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.html
Predefined variables of MonoBehavior • What can we do with them? • These are class or struct instances • transform.x = 0; • transform.y += 0.1; • transform.LookAt(target);
C# may be new for you • Don’t read too much about it and confuse yourself. It’s easy. Learn the following: • Class (a collection of variable and function definitions, just a blueprint) • Object (an actual copy of a class in memory that you can use) • Method (functions defined in a class) • Field (variables defined in a class)
Defining and using Defining a class Creating objects from the class and using them MyClass c = new MyClass(); c.AField = 3; c.AMethod(); MyClassd = new MyClass(); d.AField= 5; d.AMethod(); class MyClass { intAField; void AMethod() { } } c d AField AField 3 5
Some fundamental data types • Vector3 • Points and vectors in space (explained more thoroughly in SE 313) • Vector3 v = transform.position; • Vector3 v1 = new Vector3(0, 0, 0); • Quaternion • Orientations
Operator Overloading • Vector3 v3 = new Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z); • Is equivalent to • Vector3 v3 = v1 + v2; • You can also do • Vector3 v3 = 0.5 * v1; • Also, Vector3 is a struct, not a class. It’s just like an int.
How to go about working with code • Access what you are interested in • Easy, just use the inspector as a guide • Get the value, it will probably be an object of a class or struct • For example: Vector3 • Read about it in the script reference • http://unity3d.com/support/documentation/ScriptReference/ • Also, search for random stuff in the script reference. It’s likely to lead in the right direction.
Learn as you go • Don’t try to learn too much, you may confuse yourself • Apply and practice everything you read so that it makes sense • We’ll talk about C# more in the future • Let’s try to understand the data structures behind game objects and components
MonoBehavior Fields (automatic variablesin MyScript) Game Object Components Transform transform Cube Rigidbody rigidbody MyScript (MonoBehavior) gameObject
MonoBehavior Game Object Components Transform GetComponent<Transform>() transform Cube GetComponent(“Transform”) MyOtherScript (MonoBehavior) GetComponent<MyOtherScript>() GetComponent(“MyOtherScript”) MyScript (MonoBehavior) gameObject
MonoBehavior Game Object Components It’s like doing this in class: Transform transform; void Awake() { transform = GetComponent<Transform>(); } Transform GetComponent<Transform>() transform Cube GetComponent(“Transform”) MyOtherScript (MonoBehavior) GetComponent<MyOtherScript>() GetComponent(“MyOtherScript”) MyScript (MonoBehavior) gameObject
MonoBehavior It’s like doing this in class: Transform transform; void Awake() { transform = GetComponent<Transform>(); } Game Object Components Transform GetComponent<Transform>() transform Cube GetComponent(“Transform”) MyOtherScript (MonoBehavior) GetComponent<MyOtherScript>() GetComponent(“MyOtherScript”) MyScript (MonoBehavior) Similarly, you should do: MyOtherScriptmyOtherScript; void Awake() { myOtherScript = GetComponent<MyOtherScript>(); } Instead of calling GetComponent in Update() gameObject
Using Standard Components in Code • Don’t try to memorize anything. Just look at the inspector and you’ll figure it out.
How to learn C# • Could not find a basic C# tutorial that is independent of other .Net stuff… • Unity and C# tutorials in catlikecoding.com are good • http://channel9.msdn.com/Series/C-Sharp-Fundamentals-Development-for-Absolute-Beginnersis not bad. If you don’t have enough time, watch 9, 14, 15, 21 • It’s fastest to learn from examples!
Great Sample Code in the Asset Store • http://u3d.as/content/m2h/c-game-examples/1sG • Download through Unity • Create new project and open asset store using the link in the above site • Click “import” • Five games with scenes • Also has a PDF file
TODO: Homework • Use the five game examples from here (explained in previous slide): http://u3d.as/content/m2h/c-game-examples/1sG • Read the PDF and understand how each game works • Make meaningful changes in the game mechanics of two of the games in a way that convinces us that you understood how the game works • Write a short document explaining what modifications you have done and where should we look (in the game and in the code) to see them. • Get Özkan to grade your clock • ozkansayin.ieu@gmail.com(NOTE THE ADDRES CHANGE!) • Subject (paste this): SE 320 Homework 5 • What to send: • Assets -> Export package • File -> Build Settings -> Web player -> Build • DUE DATE: Nov 6th(but you should not procrastinate since you may have one more homework next week which may also be due Nov 6th)
TODO: Projects • Keep sending your weekly project meeting reports • Read the policy document here: http://homes.ieu.edu.tr/osayin/gd/report_policy.html ! • Please be careful about • which addresses you send it to • the subject of the e-mail