School For Games 2015 - Unity Engine Basics

95
Game Development Grundlagen der Unity-Engine Nick Prühs

Transcript of School For Games 2015 - Unity Engine Basics

Game DevelopmentGrundlagen der Unity-Engine

Nick Prühs

About Me

“Best Bachelor“ Computer ScienceKiel University, 2009

Master GamesHamburg University of Applied Sciences, 2011

Lead ProgrammerDaedalic Entertainment, 2011-2012

Co-Founderslash games, 2013

Microsoft MVP2015

2 / 12

First Things First

• At npruehs.de/teaching you‘ll find all slides

• Ask your questions – any time!

• Contact me any time at [email protected]!

3 / 12

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

New Unity Project

5 / 12

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

Unity Editor Layout – 2 by 3

7 / 12

Unity Editor Layout – 2 by 3

The scene view shows all objects of the level and allows free movement.

8 / 12

Unity Editor Layout – 2 by 3

The game view shows the level as seen by the camera.

9 / 12

Unity Editor Layout – 2 by 3

The hierarchy provides an overview of all scene objects.

10 / 12

Unity Editor Layout – 2 by 3

The project view contains all assets (3D models, code) of your project.

11 / 12

Unity Editor Layout – 2 by 3

The inspector shows the details of the selected game object.

12 / 12

Unity Camera

13 / 12

The camera draws scene objects as seen from its position.

Asset Import

Unity imports all of your assets so it understands how to use them properly.

(Thanks, Unity!)

14 / 12

Asset Import

Unity imports all of your assets so it understands how to use them properly.

(Thanks, Unity!)

15 / 12

Game Loop

Update

Draw

16

Init Shutdown

The game lifecycle is split up into four major steps.

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.

Game Loop

Update

Draw

18

Init Shutdown

In each update, all game objects may change their properties, such as position, health, or cooldown timer.

Game Loop

Update

Draw

19

Init Shutdown

Then, the game tells the graphics device what to draw, where, and how.

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.

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

Our First Script!

22 / 12

In Unity, we can attach scripts to game objects.

Each script will describe the behaviorof that particular game object.

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;

}}

}

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 frameworkslike .NET save us from writing the same code over and over again (e.g. vector math, lists).

24 / 12

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

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!

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

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

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

Vector Math

Think of vectors as sets of coordinates in our coordinate system:

𝑣 =𝑥𝑦𝑧

30 / 12

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

Vector Math

Just as numbers, you can add vectors, resulting in a new set of numbers:

𝑣1 + 𝑣2 =123

+456

=579

32 / 12

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

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

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;

}}

}

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

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!

A Nice Space Background

Lets pick up a nice space image from the official NASA website:

http://apod.nasa.gov/apod/ap120828.html

38

A Nice Space Background

Now, let’s change the texture import settings of our background:

Set Wrap Mode to Clamp.

39

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

A Nice Space Background

Finally, we can change the scene lighting settings to use our brand-new skybox!

41

Let’s make a side-scroller!

Just change the position and rotationof your camera –easy!

42 / 12

Let’s make a side-scroller!

You can also reduce the field of view to ensure the skybox looks nice.

43 / 12

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;

}}

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

Unity Prefabs

46 / 12

Unity allows us to create prefabs of our game objects.

Think of prefabs as “Schablonen” for new game objects.

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 initializationvoid Start (){// Still nothing to do, yay!

}

// Update is called once per framevoid Update () {// Check if player pressed the SPACE button.if (Input.GetKey(KeyCode.Space)){// Fire projectile!Instantiate(this.ProjectilePrefab, this.transform.position, this.transform.rotation);

}}

}

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;

}

}

Hint

Splitting up your code across multiple components is always a

better idea than writing huge, monolithic code files!

49 / 61

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

Game Objects

51 / 57

There are probably hundreds of ways…

Then one day your designer says that they want anew type of “alien” asteroid that acts just like a heatseeking missile, except it’s still an asteroid.”

- Scott Bilas

52 / 57

53 / 12

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.

Unity Hierarchy

55 / 12

If a game object in Unity is a child of another, its transformis relative to the one of its parent:

• Position

• Rotation

• Scale

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

Automated MovementWith DirectionC#

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;

}

}

Adding Enemies

58

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

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 framevoid Update (){// Destroy when dead.if (this.CurrentHealth <= 0){Destroy (this.gameObject);

}}

}

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

Handling Collisions

As you can imagine, finding out whether two objects collide can include some terrible math.

In Unity, colliders can be used to approximatethe extents of a game object for easier collision detection.

62 / 12

Handling Collisions

Let’s add a sphere collider for our space ship and projectiles!

63 / 12

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

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

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);

}}

}

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!

Building The Game

68 / 12

Finally, we can build our game, to show everyone and share the fun

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

Learning From Others

70 / 60

StarCraft II Galaxy Editor

Learning From Others

71 / 60

StarCraft II Galaxy Editor

Learning From Others

72 / 60

StarCraft II Galaxy Editor

Learning From Others

73 / 60

World of Warcraft LUA UI API

Learning From Others

74 / 60

Hearthstone Log File

Learning From Others

75 / 60

StarCraft II Screenshot

Learning From Others

76 / 60

StarCraft II Screenshot

Learning From Others

77 / 60

StarCraft II Screenshot

Learning From Others

78 / 60

StarCraft II Screenshot

Learning From Others

79 / 60

StarCraft II Screenshot

Learning From Others

80 / 60

StarCraft II Screenshot

Learning From Others

81 / 60

StarCraft II Screenshot

Learning From Others

82 / 60

StarCraft II Screenshot

Learning From Others

83 / 60

Unreal Tournament 3 Splash Screen

Learning From Others

84 / 60

Unreal Tournament 3 Login Screen

Learning From Others

85 / 60

Hostile Worlds Screen Transitions

Learning From Others

86 / 60

Steam Server Browser

Learning From Others

87 / 60

WarCraft III Lobby

Learning From Others

88 / 60

WarCraft III Loading Screen

Learning From Others

89 / 60

WarCraft III Score Screen

Learning From Others

90 / 60

StarCraft II Options Screen

Learning From Others

91 / 60

Doom 3 GPL Release on GitHub

Learning From Others

92 / 60

Post-Mortem of WarCraft

Don’t Reinvent The Wheel

93 / 60

HOTween

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 :)

Thank you for your attention!

Contact

Mail

[email protected]

Blog

http://www.npruehs.de

Twitter

@npruehs

Github

https://github.com/npruehs

95 / 60