Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

School For Games 2015 - Unity Engine Basics

790 views

Published on

Workshop for School For Games 2015 at InnoGames, Hamburg, Germany.

Introduction to game lifecycles and Unity3D.

Published in: Technology
  • Be the first to comment

School For Games 2015 - Unity Engine Basics

  1. 1. Game Development Grundlagen der Unity-Engine Nick Prühs
  2. 2. About Me “Best Bachelor“ Computer Science Kiel University, 2009 Master Games Hamburg University of Applied Sciences, 2011 Lead Programmer Daedalic Entertainment, 2011-2012 Co-Founder slash games, 2013 Microsoft MVP 2015 2 / 12
  3. 3. First Things First • At npruehs.de/teaching you‘ll find all slides • Ask your questions – any time! • Contact me any time at dev@npruehs.de! 3 / 12
  4. 4. Objectives • To understand the fundamentals of game lifecycles • To learn how to build a small game with Unity3D • To get an idea of how to learn from others 4 / 60
  5. 5. New Unity Project 5 / 12
  6. 6. New Unity Project Unity projects contain multiple different files and folders • Assets (3D Models, Images, Code) • Settings (Input, Phyiscs) • Temporary Files (imported assets, see later) 6 / 12
  7. 7. Unity Editor Layout – 2 by 3 7 / 12
  8. 8. Unity Editor Layout – 2 by 3 The scene view shows all objects of the level and allows free movement. 8 / 12
  9. 9. Unity Editor Layout – 2 by 3 The game view shows the level as seen by the camera. 9 / 12
  10. 10. Unity Editor Layout – 2 by 3 The hierarchy provides an overview of all scene objects. 10 / 12
  11. 11. Unity Editor Layout – 2 by 3 The project view contains all assets (3D models, code) of your project. 11 / 12
  12. 12. Unity Editor Layout – 2 by 3 The inspector shows the details of the selected game object. 12 / 12
  13. 13. Unity Camera 13 / 12 The camera draws scene objects as seen from its position.
  14. 14. Asset Import Unity imports all of your assets so it understands how to use them properly. (Thanks, Unity!) 14 / 12
  15. 15. Asset Import Unity imports all of your assets so it understands how to use them properly. (Thanks, Unity!) 15 / 12
  16. 16. Game Loop Update Draw 16 Init Shutdown The game lifecycle is split up into four major steps.
  17. 17. Game Loop Update Draw 17 Init Shutdown During initialization, the game sets up everything to run properly, such as preparing the graphics device, loading all assets, or opening a log file.
  18. 18. Game Loop Update Draw 18 Init Shutdown In each update, all game objects may change their properties, such as position, health, or cooldown timer.
  19. 19. Game Loop Update Draw 19 Init Shutdown Then, the game tells the graphics device what to draw, where, and how.
  20. 20. Game Loop Update Draw 20 Init Shutdown Finally, the game needs to shut down properly, returning the graphics device to the operating system, for example.
  21. 21. Our First Script! Now that we’ve got our little space ship ready, it’s time to add some action! We want to do two things now: 1. Check if the player pressed a button. 2. Move the space ship if he or she did. 21 / 12
  22. 22. Our First Script! 22 / 12 In Unity, we can attach scripts to game objects. Each script will describe the behavior of that particular game object.
  23. 23. Our First Script! C# 23 // Use the magic code Unity provides for us. using UnityEngine; using System.Collections; // Name of the script. public class KeyboardMovement : MonoBehaviour { // Use this for initialization. void Start () { // Nothing to do here, yay! } // Update is called once per frame. void Update () { // Check if player pressed any button. if (Input.GetKey(KeyCode.W)) { // Add "forward" vector to current position. this.transform.localPosition += Vector3.forward; } } }
  24. 24. Our First Script! // Use the magic code Unity provides for us. using UnityEngine; using System.Collections; Whew, that’s a lot of stuff. Let’s take a detailed look at that! First, game engines like Unity and frameworks like .NET save us from writing the same code over and over again (e.g. vector math, lists). 24 / 12
  25. 25. Our First Script! // Name of the script. public class KeyboardMovement:MonoBehaviour { … } Clearly, Unity needs to know the name of our script. The other stuff (public, class, MonoBehaviour, that strange colon, …) won’t be covered here. Go to a dedicated computer science class for that ;) 25 / 12
  26. 26. Our First Script! // Use this for initialization. void Start () { // Nothing to do here, yay! } 26 / 12 Remember the initialization we sometimes need to do before entering the game loop? Unity allows us to that right here, right now!
  27. 27. Our First Script! // Use this for initialization. void Start () { // Nothing to do here, yay! } Remember the initialization we sometimes need to do before entering the game loop? Unity allows us to that right here, right now! (Thanks, Unity!) 27 / 12
  28. 28. Our First Script! // Check if player pressed any button. if (Input.GetKey(KeyCode.W)) { … } Unity even intercepts keyboard input for us. All we need to do is ask ;) 28 / 12
  29. 29. Our First Script! // Add "forward" vector to current position. this.transform.localPosition += Vector3.forward; Unity stores information relevant for drawing the scene with our camera in a transform. This includes the position of an object, its rotation, and its scale. 29 / 12
  30. 30. Vector Math Think of vectors as sets of coordinates in our coordinate system: 𝑣 = 𝑥 𝑦 𝑧 30 / 12
  31. 31. Vector Math Just as numbers, you can add vectors, resulting in a new set of numbers: 𝑣1 + 𝑣2 = 𝑥1 𝑦1 𝑧1 + 𝑥2 𝑦2 𝑧2 = 𝑥1 + 𝑥2 𝑦1 + 𝑦2 𝑧1 + 𝑧2 31 / 12
  32. 32. Vector Math Just as numbers, you can add vectors, resulting in a new set of numbers: 𝑣1 + 𝑣2 = 1 2 3 + 4 5 6 = 5 7 9 32 / 12
  33. 33. Our First Script! // Add "forward" vector to current position. this.transform.localPosition += Vector3.forward; We can access the transform of the game object the script is attached to using the this keyword. Then, we can modify its position by using the += operator. 33 / 12
  34. 34. Our First Script! Whew, that was fast! Maybe we should improve our code here a bit. 1. Our spaceship should have its own speed value. This will also allow us to have different ships with different speed! 2. We should take the frame time of our game into account. Otherwise, our spaceship would be faster if our PC is faster – unfair! 34 / 12
  35. 35. Our First Script! C# 35 // Use the magic code Unity provides for us. using UnityEngine; using System.Collections; // Name of the script. public class KeyboardMovement : MonoBehaviour { // Speed of this game object. public float Speed; // Use this for initialization. void Start () { // Nothing to do here, yay! } // Update is called once per frame. void Update () { // Check if player pressed any button. if (Input.GetKey(KeyCode.W)) { // Add "forward" vector to current position. this.transform.localPosition += Vector3.forward * this.Speed * Time.deltaTime; } } }
  36. 36. Our First Script! // Speed of this game object. public float Speed; Here, speed is a variable (like x in your math class). public means, everybody can change the speed value. float means, speed may be a fraction (like 1.4) 36 / 12
  37. 37. Our First Script! 37 / 12 Unity exposes public variables in the inspector we were talking about earlier. Change the speed value and see what happens!
  38. 38. A Nice Space Background Lets pick up a nice space image from the official NASA website: http://apod.nasa.gov/ap od/ap120828.html 38
  39. 39. A Nice Space Background Now, let’s change the texture import settings of our background: Set Wrap Mode to Clamp. 39
  40. 40. A Nice Space Background Next, we need to create a material for the skybox to use with our camera. • Set the shader to Skybox/6 Sided. • Assign the texture to all six slots. 40
  41. 41. A Nice Space Background Finally, we can change the scene lighting settings to use our brand-new skybox! 41
  42. 42. Let’s make a side-scroller! Just change the position and rotation of your camera – easy! 42 / 12
  43. 43. Let’s make a side-scroller! You can also reduce the field of view to ensure the skybox looks nice. 43 / 12
  44. 44. Adjusted Movement C# 44 // Update is called once per frame. void Update () { // Check if player pressed any button. if (Input.GetKey(KeyCode.W)) { // Add “up" vector to current position. this.transform.localPosition += Vector3.up * this.Speed * Time.deltaTime; } if (Input.GetKey(KeyCode.S)) { // Add “down" vector to current position. this.transform.localPosition += Vector3.down * this.Speed * Time.deltaTime; } if (Input.GetKey(KeyCode.A)) { // Add “back" vector to current position. this.transform.localPosition += Vector3.back * this.Speed * Time.deltaTime; } if (Input.GetKey(KeyCode.D)) { // Add "forward" vector to current position. this.transform.localPosition += Vector3.forward * this.Speed * Time.deltaTime; } }
  45. 45. Now for some real action! We want our spaceship to fire its weapons! For this, we need to: 1. Add a projectile to our project. 2. Create a projectile whenever the player presses a button. 45 / 12
  46. 46. Unity Prefabs 46 / 12 Unity allows us to create prefabs of our game objects. Think of prefabs as “Schablonen” for new game objects.
  47. 47. Our Second Script! C# 47 // Use the magic code Unity provides for us. using UnityEngine; using System.Collections; // Name of the script. public class KeyboardFire : MonoBehaviour { // Prefab to use as projectile. public GameObject ProjectilePrefab; // Use this for initialization void Start () { // Still nothing to do, yay! } // Update is called once per frame void Update () { // Check if player pressed the SPACE button. if (Input.GetKey(KeyCode.Space)) { // Fire projectile! Instantiate(this.ProjectilePrefab, this.transform.position, this.transform.rotation); } } }
  48. 48. Boring projectiles are boring C# 48 // Use the magic code Unity provides for us. using UnityEngine; using System.Collections; // Name of the script. public class AutomaticMovement : MonoBehaviour { // Speed of this game object. public float Speed; // Update is called once per frame void Update () { // Fly "forward" automatically. this.transform.localPosition += Vector3.forward * this.Speed * Time.deltaTime; } }
  49. 49. Hint Splitting up your code across multiple components is always a better idea than writing huge, monolithic code files! 49 / 61
  50. 50. Game Objects • objects in your game world can (or cannot)… • be visible • move around • attack • explode • be targeted • become selected • follow a path • common across all genres 50 / 57
  51. 51. Game Objects 51 / 57
  52. 52. There are probably hundreds of ways… Then one day your designer says that they want a new type of “alien” asteroid that acts just like a heat seeking missile, except it’s still an asteroid.” - Scott Bilas 52 / 57
  53. 53. 53 / 12
  54. 54. Camera Movement 54 / 12 Right now, we can easily leave the screen, if we want to. Let’s change that: 1. Reparent the camera to our player ship. 2. Add the automatic movement script to the player ship.
  55. 55. Unity Hierarchy 55 / 12 If a game object in Unity is a child of another, its transform is relative to the one of its parent: • Position • Rotation • Scale
  56. 56. Adding Enemies Time to add our first enemies! We need to … 1. Create a prefab for our enemy space ship. 2. Rotate enemies correctly. 3. Modify their movement. 56 / 12
  57. 57. Automated Movement With Direction C# 57 // Use the magic code Unity provides for us. using UnityEngine; using System.Collections; // Name of the script. public class AutomaticMovement : MonoBehaviour { // Velocity of this game object. public Vector3 Velocity; // Update is called once per frame void Update () { // Fly "forward" automatically. this.transform.localPosition += this.Velocity * Time.deltaTime; } }
  58. 58. Adding Enemies 58
  59. 59. Taking Damage Next, we want to keep track of the health value of each ship. After all, we want to destroy our enemies, don’t we? We need to: 1. Write a script keeping track of spaceship health. 2. Check for collisions of projectiles with ships. 3. Reduce health when projectiles hit ships. 59 / 12
  60. 60. Spaceship Health C# 60 // Use the magic code Unity provides for us. using UnityEngine; using System.Collections; // Name of the script. public class Health : MonoBehaviour { // Health of this game object. public float CurrentHealth; // Update is called once per frame void Update () { // Destroy when dead. if (this.CurrentHealth <= 0) { Destroy (this.gameObject); } } }
  61. 61. Hint The if operator in C# allows us to add conditions to our code. It’s always a good idea not to compare numbers for exact values (<= instead of ==) 61 / 12
  62. 62. Handling Collisions As you can imagine, finding out whether two objects collide can include some terrible math. In Unity, colliders can be used to approximate the extents of a game object for easier collision detection. 62 / 12
  63. 63. Handling Collisions Let’s add a sphere collider for our space ship and projectiles! 63 / 12
  64. 64. Handling Collisions If your collider moves (just like our ships and projectiles), we need to add a rigidbody as well. Make sure to disable “Use Gravity”, or Unity will make our spaceships fall down. 64 / 12
  65. 65. Handling Collisions Whenever two objects collide, Unity won’t allow them to intersect each other. Check “Is Trigger” to allow these objects to intersect! 65 / 12
  66. 66. Handling Collisions C# 66 // Use the magic code Unity provides for us. using UnityEngine; using System.Collections; // Name of the script. public class DamageOnCollision : MonoBehaviour { // Damage this game object deals on collision. public float Damage; // OnCollisionEnter is called whenever we collide with somebody else. void OnTriggerEnter(Collider other) { // Get the health component of the other object. Health health = other.gameObject.GetComponent<Health> (); if (health != null) { // Reduce health. health.CurrentHealth -= this.Damage; // Destroy this game object. Destroy (this.gameObject); } } }
  67. 67. Finishing Touches 67 / 12 Now, your job as programmer is done – time for the designer in you! Add enemies to the level any play, play, play!
  68. 68. Building The Game 68 / 12 Finally, we can build our game, to show everyone and share the fun 
  69. 69. Where To Go From Here As with every software project, there’s always more to do… • Enemy Fire • Enemy AI • Menus • Explosions • Score • Cheats • More Enemies • More Levels 69
  70. 70. Learning From Others 70 / 60 StarCraft II Galaxy Editor
  71. 71. Learning From Others 71 / 60 StarCraft II Galaxy Editor
  72. 72. Learning From Others 72 / 60 StarCraft II Galaxy Editor
  73. 73. Learning From Others 73 / 60 World of Warcraft LUA UI API
  74. 74. Learning From Others 74 / 60 Hearthstone Log File
  75. 75. Learning From Others 75 / 60 StarCraft II Screenshot
  76. 76. Learning From Others 76 / 60 StarCraft II Screenshot
  77. 77. Learning From Others 77 / 60 StarCraft II Screenshot
  78. 78. Learning From Others 78 / 60 StarCraft II Screenshot
  79. 79. Learning From Others 79 / 60 StarCraft II Screenshot
  80. 80. Learning From Others 80 / 60 StarCraft II Screenshot
  81. 81. Learning From Others 81 / 60 StarCraft II Screenshot
  82. 82. Learning From Others 82 / 60 StarCraft II Screenshot
  83. 83. Learning From Others 83 / 60 Unreal Tournament 3 Splash Screen
  84. 84. Learning From Others 84 / 60 Unreal Tournament 3 Login Screen
  85. 85. Learning From Others 85 / 60 Hostile Worlds Screen Transitions
  86. 86. Learning From Others 86 / 60 Steam Server Browser
  87. 87. Learning From Others 87 / 60 WarCraft III Lobby
  88. 88. Learning From Others 88 / 60 WarCraft III Loading Screen
  89. 89. Learning From Others 89 / 60 WarCraft III Score Screen
  90. 90. Learning From Others 90 / 60 StarCraft II Options Screen
  91. 91. Learning From Others 91 / 60 Doom 3 GPL Release on GitHub
  92. 92. Learning From Others 92 / 60 Post-Mortem of WarCraft
  93. 93. Don’t Reinvent The Wheel 93 / 60 HOTween
  94. 94. There might even be cookies. 94 / 60 Source: UTWeap_LinkGun.uc (August UDK 2010) /** Holds the list of link guns linked to this weapon */ var array<UTWeap_LinkGun> LinkedList; // I made a funny Hahahahah :)
  95. 95. Thank you for your attention! Contact Mail dev@npruehs.de Blog http://www.npruehs.de Twitter @npruehs Github https://github.com/npruehs 95 / 60

×