1 / 36

SE 320 – Introduction to Game Development

SE 320 – Introduction to Game Development. Lecture 6: Programming in Unity & Developing Simple Games (continued) Lecturer: Gazihan Alankuş. Please look at the last two slides for assignments (marked with TODO ). Outline. Reminder about weekly reports Quiz Homeworks Programming in Unity.

Télécharger la présentation

SE 320 – Introduction to Game Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SE 320 – Introduction to Game Development Lecture 6: Programming in Unity & Developing Simple Games (continued) Lecturer: GazihanAlankuş Please look at the last two slides for assignments (marked with TODO)

  2. Outline • Reminder about weekly reports • Quiz • Homeworks • Programming in Unity

  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/12fall/se320/material/day5/

  4. Sample Games in Homework • URL was: http://u3d.as/content/m2h/c-game-examples/1sG • Did you look at them, yet? • What were the 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!

  5. 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 take a practical approach

  6. Create a script Name it appropriately It already has some code Double-click on it to open it in MonoDevelop

  7. MonoDevelop is the default IDE where we edit our code Our script already has some code This is a C# class, which is a collection of variables and functions. It is only a blueprint, a definition. When it is instantiated, it is called an object. Only then its variables and functions come to life.

  8. This is the name of our class. It is the name of our component. It is what we see in the Unity window. This is the name of the base class. Our class is “derived” from this class. It is the base class of all user components. Unity provides it for us. We can read about it in the Unity Scripting Reference (Help -> Scripting Reference). Our component has everything that this base class has.

  9. These two functions are provided as examples. Unity is supposed to call the Start function when the game starts and the Update function at everyframe.

  10. Now let’s see whether Unity really calls these functions. I want to print out messages when these functions are called. That way I will know when they are called. This is done with Debug.Log(“message”). I started typing “Deb” inside the Start function. MonoDevelop understood what I was typing and provided a suggestion. This is called “Code Completion” or “IntelliSense”. It appears automatically. You can also make it appear anytime using this key combination: Ctrl + Space.

  11. Now I filled these functions in. However, Unity will ignore this script because it’s justa component that is not added to any game object. I need to add it to a game object. I save this script and Alt + Tab to the Unity window. It is very important to save the script. Otherwise Unity will not see your changes.

  12. I can add my component to a game objectby dragging it under the game object’s components in the inspector.

  13. Or by dragging it on the game object’s name in the hierarchy pane.

  14. Now my component is attached to the game object. An object instance of the component class is created and attached to this game object. There can be other instances of this component that are attached to other game objects.

  15. Now when I run this game with the play button, I see that my functions are really called and the messages are printed. Start was called just once in the beginning.Update is being called all the time.

  16. There are many other functions that Unity can call. In the Unity scripting reference, search for MonoBehavior, and look under the heading “Overridable Functions”. For example, I want a function to be called when my game character touches an object. This I can do with the trigger mechanism in Unity. I mark colliders as “Is Trigger” and when theytouch each other, the components that are attached to the game objects are notified. Let’s learn this from the scripting reference.

  17. This is a part of the MonoBehavior documentation in the scripting reference, under the heading “Overridable Functions”. Here is the function that I was looking for. Ok, how will I create this function in my script? It’s always best to look at the documentation of the function to see how it should be writtenin code. Some functions expect specific parameters and you should write your function to accept them.

  18. This is the page that appeared after I clicked OnTriggerEnter in the last page. This is the documentation of this function that we will write and that Unity knows about. Don’t forget to make this C#! Copy-paste this line into your classto have this function in your component.

  19. Now I pasted the function signature (the linewith the function name and parameters). Then I added another message that will be displayed when the object hits. In Unity, I will place another cube on top of this one. I will make that cube fall down on this one and observe this output message.

  20. I created another cube, marked its collider “Is Trigger”, added a RigidBody to it and placed it over this first cube. Now I see that our cube’s OnTriggerEnter function is called when the second cube hits it.

  21. This taught us how to write functions that Unity will call at appropriate times. Getting user input from the keyboard is a little different, there is no special function thatUnity calls when you hit a key. Instead, in the Update function, at every frame, you need to ask Unity if the user pressed a key, is still pressing a key, or released a key. This is how you do it:

  22. I ran this code and pressed the up button just once, quickly. I see that for one frame, GetKeyDown(Keycode.UpArrow) was true. This was the frame when I first pressed the button. It was false for other frames. Even though I was quick, for three frames,GetKey(Keycode.UpArrow) was true. This was the short durationduring which I held the button down. It was false for other frames. And for one frame, GetKeyUp(Keycode.UpArrow) was true. Thiswas the frame when I released the button. It was false for other frames. Using this technique, we can act according to keyboard input.

  23. Now, I would like to apply a upward vertical force to my object when the up key is down. I need to find out how to apply forces. I need to add a RigidBody component and look at its documentation. Then, I need to find out how much force to apply. Later, I will write code to make this happen.

  24. I added a RigidBody, now the cube falls down. So far so good.

  25. I added the code to apply a force. This will be done only when the user is holding down the up arrow key. MonoBehavior provides a variable “rigidbody” that refers to the RigidBody component that is added to the same game object. Check last week’s slides about these relationships between components and game objects, and about how to access them in code. The force I applied is 10 units in the y direction. I made this number up.

  26. I run my game, press up but can’t move the cube upwards. It seems to slow down, though. If I press up right after running, it stops and moves up. I need a force that will balance gravity better. Gravity is -9.81 towards down, as you can see in Edit->Project Settings->Physics. Since my mass is 1, this means a force of -9.81, which is downwards. If I apply 2 * 9.81 upwards, it should behave as if gravity has switched upwards, since2 * 9.81 – 9.81 = +9.81

  27. I change it accordingly. (9.81f because it accepts floats, not doubles)

  28. Now my cube moves in a way that is balanced.

  29. Learning C# • You have to show extra effort. Our time here won’t be sufficient. • I will talk about some important details from time to time.

  30. Objects and References • Extremely important to understand well • Object references in C# are always in heap. Your class-typed variables are actually pointers in C++. • Material m = new Material();

  31. Pointer Fun with Binky • No class talking about references is complete without Binky! • This is not a joke. If you don’t get why you watched it, please ask. • http://en.wikipedia.org/wiki/File:Pointer_Fun_with_Binky_(Java).ogg

  32. Objects and References • Extremely important to understand well • Object references in C# are always in heap. Your class-typed variables are actually pointers in C++. • Material m = new Material(); • However, structs are in the stack. Your struct-typed variables are like C++ structs. • Vector v = new Vector(); • Looks the same as class instantiation, but acts differently.

  33. Variables with Class Types are References • Transform transform; • GameObjectmyObject; • Structs do not work that way • Vector3, Quaternion, etc. • Vector3 v = new Vector3(0, 0, 1); • Vector3 v1 = v; • v1.x = 1; • //v.x == 0 still

  34. 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

  35. 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 other of the games in a way that convinces us that you understood how the game works. In the end, the total will be four games that you modified. • 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 work, according to http://homes.ieu.edu.tr/~osayin/gd/homework_policy.html • ozkansayin.ieu@gmail.com(NOTE THE ADDRES CHANGE!) • Subject (paste this): SE 320 Homework 6 • What to send: • Assets -> Export package • File -> Build Settings -> Web player -> Build (for both games) • DUE DATE: Nov 6th(you will submit four modified games by this date, including the two that was assigned last week)

  36. 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 • Reports that were not sent according to these policies will be ignored!

More Related