Presentation is loading. Please wait.

Presentation is loading. Please wait.

SE 320 – Introduction to Game Development Lecture 5: Programming in Unity & Developing Simple Games Lecturer: Gazihan Alankuş Please look at the last two.

Similar presentations


Presentation on theme: "SE 320 – Introduction to Game Development Lecture 5: Programming in Unity & Developing Simple Games Lecturer: Gazihan Alankuş Please look at the last two."— Presentation transcript:

1 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) 2/10/20121

2 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 2/10/20122

3 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/1 2fall/se320/material/day5/ http://homes.ieu.edu.tr/~galankus/teaching/1 2fall/se320/material/day5/ 2/10/20123

4 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 2/10/20124

5 The Code Where do you write it into? What can you do with it? 2/10/20125

6 The Code Where do you write it into? What can you do with it? 2/10/20126

7 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 2/10/20127

8 The Code Where do you write it into? What can you do with it? 2/10/20128

9 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 2/10/20129

10 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/ScriptRe ference/ ) http://docs.unity3d.com/Documentation/ScriptRe ference/ – Your custom component has everything that the MonoBehavior class has Most of the standard components are easily reachable as fields (transform, rigidbody, etc.) 2/10/201210

11 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 2/10/201211

12 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 2/10/201212

13 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. 2/10/201213

14 Some predefined functions that Unity calls Awake Start Update OnCollisionEnter http://docs.unity3d.com/Documentation/Scri ptReference/MonoBehaviour.html provides the whole list http://docs.unity3d.com/Documentation/Scri ptReference/MonoBehaviour.html 2/10/201214

15 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/ScriptR eference/MonoBehaviour.html http://docs.unity3d.com/Documentation/ScriptR eference/MonoBehaviour.html 2/10/201215

16 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); 2/10/201216

17 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) 2/10/201217

18 Defining and using Defining a class class MyClass { int AField; void AMethod() { } Creating objects from the class and using them MyClass c = new MyClass(); c.AField = 3; c.AMethod(); MyClass d = new MyClass(); d.AField = 5; d.AMethod(); 2/10/201218 c AField 3 d 5

19 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 2/10/201219

20 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. 2/10/201220

21 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/ScriptRef erence/ http://unity3d.com/support/documentation/ScriptRef erence/ Also, search for random stuff in the script reference. It’s likely to lead in the right direction. 2/10/201221

22 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 2/10/201222

23 MonoBehavior 2/10/201223 Cube Transform Rigidbody MyScript (MonoBe havior) Game ObjectComponents Fields (automatic variables in MyScript) gameObject= Cube transform= Transform rigidbody= Rigidbody rigidbody transform gameObject

24 MonoBehavior 2/10/201224 Cube Transform MyOtherScript (MonoBehavio r) MyScript (MonoBe havior) Game ObjectComponents GetComponent () transform gameObject GetComponent(“MyOtherScript”) GetComponent () GetComponent(“Transform”)

25 MonoBehavior 2/10/201225 Cube Transform MyOtherScript (MonoBehavio r) MyScript (MonoBe havior) Game ObjectComponents GetComponent () transform gameObject GetComponent(“MyOtherScript”) GetComponent () GetComponent(“Transform”) It’s like doing this in class: Transform transform; void Awake() { transform = GetComponent (); }

26 MonoBehavior 2/10/201226 Cube Transform MyOtherScript (MonoBehavio r) MyScript (MonoBe havior) Game ObjectComponents GetComponent () transform gameObject GetComponent(“MyOtherScript”) GetComponent () GetComponent(“Transform”) It’s like doing this in class: Transform transform; void Awake() { transform = GetComponent (); } Similarly, you should do: MyOtherScript myOtherScript; void Awake() { myOtherScript = GetComponent (); } Instead of calling GetComponent in Update()

27 Using Standard Components in Code Don’t try to memorize anything. Just look at the inspector and you’ll figure it out. 2/10/201227

28 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-Beginners is not bad. If you don’t have enough time, watch 9, 14, 15, 21 http://channel9.msdn.com/Series/C-Sharp-Fundamentals- Development-for-Absolute-Beginners It’s fastest to learn from examples! 2/10/201228

29 Great Sample Code in the Asset Store http://u3d.as/content/m2h/c-game- examples/1sG 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 2/10/201229

30 TODO: Homework Use the five game examples from here (explained in previous slide): http://u3d.as/content/m2h/c-game-examples/1sG 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!) ozkansayin.ieu@gmail.com – Subject (paste this): SE 320 Homework 5 – What to send: Assets -> Export package File -> Build Settings -> Web player -> Build – DUE DATE: Nov 6 th (but you should not procrastinate since you may have one more homework next week which may also be due Nov 6 th ) 2/10/201230

31 TODO: Projects Keep sending your weekly project meeting reports Read the policy document here: http://homes.ieu.edu.tr/osayin/gd/report_pol icy.html ! http://homes.ieu.edu.tr/osayin/gd/report_pol icy.html – Please be careful about which addresses you send it to the subject of the e-mail 2/10/201231


Download ppt "SE 320 – Introduction to Game Development Lecture 5: Programming in Unity & Developing Simple Games Lecturer: Gazihan Alankuş Please look at the last two."

Similar presentations


Ads by Google