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. 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. 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
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.
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. 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. 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. 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. 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. 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. 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. 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. 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. Vector Math
Think of vectors as sets of coordinates in our
coordinate system:
đŁ =
đĽ
đŚ
đ§
30 / 12
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. 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. 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. 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. 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. 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. 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. 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. A Nice Space Background
Now, letâs change the
texture import settings of
our background:
Set Wrap Mode to Clamp.
39
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. A Nice Space Background
Finally, we can change
the scene lighting
settings to use our
brand-new skybox!
41
42. Letâs make a side-scroller!
Just change the
position and rotation
of your camera â
easy!
42 / 12
43. Letâs make a side-scroller!
You can also reduce
the field of view to
ensure the skybox
looks nice.
43 / 12
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. 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. Unity Prefabs
46 / 12
Unity allows us to
create prefabs of our
game objects.
Think of prefabs as
âSchablonenâ for new
game objects.
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. 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. Hint
Splitting up your code across
multiple components is always a
better idea than writing huge,
monolithic code files!
49 / 61
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
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
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. 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. 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. 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;
}
}
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. 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. 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. 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
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. 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. 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. 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. Building The Game
68 / 12
Finally, we can build our
game, to show everyone
and share the fun ď
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
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. Thank you for your attention!
Contact
Mail
dev@npruehs.de
Blog
http://www.npruehs.de
Twitter
@npruehs
Github
https://github.com/npruehs
95 / 60