SE 320 – Introduction to Game Development

46
SE 320 – Introduction to Game Development Lecture 11: Animations and GoKit Lecturer: Gazihan Alankuş Please look at the last slides for assignments (marked with TODO) 1

description

SE 320 – Introduction to Game Development. Lecture 11: Animations and GoKit Lecturer: Gazihan Alankuş. Please look at the last slides for assignments (marked with TODO ). Topics Today. Revisiting animations Using Animations Using GoKit. Moving Things in Unity. Manual. Animations. - PowerPoint PPT Presentation

Transcript of SE 320 – Introduction to Game Development

Page 1: SE 320 – Introduction to Game Development

1

SE 320 – Introduction to Game Development

Lecture 11: Animations and GoKitLecturer: Gazihan Alankuş

Please look at the last slides for assignments (marked with TODO)

Page 2: SE 320 – Introduction to Game Development

2

Topics Today• Revisiting animations• Using Animations• Using GoKit

Page 3: SE 320 – Introduction to Game Development

Moving Things in Unity

3

Setting position/rotation in the Update()

function

Manual Animations

Applying forces/setting

velocities in the update function

Position-based Physics-based

Creating animations in

Unity’s Animation pane

In Unity

Creating animations in the

modeling software

In Blender, etc.

Tedious, have to keep state and do something every

frame

Things can go out of control, most motions are not physically viable

Static motions, cannot adjust

easily in runtime

Page 4: SE 320 – Introduction to Game Development

4

Deciding how to move things• All methods have their pros and cons, you

have to decide for your situation– Animations are great for most cases

• In week 8, we have learned how to create animations in the Unity editor

• Let’s see how we can use animations coming from a model created in Blender – Models from 3DS Max and Maya are similar

Page 5: SE 320 – Introduction to Game Development

5

Using animations created in Blender

• We have a model that was created in Blender.

• It has a number of animations in asingle timeline, one after the other

Page 6: SE 320 – Introduction to Game Development

6

Using animations created in Blender

• Now we would like to use this in Unity• Select the .blend file and the textures, drag

them together to the Assets folder or Project pane– You can also drag

the folder that contains them

Page 7: SE 320 – Introduction to Game Development

7

Using animations created in Blender

• Now you see a prefab-like entry in the Project pane. You should see the textured robot in the inspector when you select it.

Page 8: SE 320 – Introduction to Game Development

8

Using animations created in Blender

• You can drag it into the scene and run the game. You’ll see it doing some motions one after the other

• These are the animations that are in the timeline of the Blender file. We need to separate them and name them.

Page 9: SE 320 – Introduction to Game Development

9

Using animations created in Blender

• The artist that created the animations (yours truly) told us which frames contain which animations:– 1-20: Walk– 30-50: Damage– 60-100: Rejoice

• Now we want Unity to know them as well. Select the model in the project pane and look at the inspector.

Page 10: SE 320 – Introduction to Game Development

10

Using animations created in Blender

• There you will see import options for this Blender file. Under Animations, we want to add all the animation segments

Page 11: SE 320 – Introduction to Game Development

11

Using animations created in Blender

• We add them and name them accordingly. We can set them to loop if it makes sense.

• Don’t forget to press the Apply button on the bottom

Page 12: SE 320 – Introduction to Game Development

12

Using animations created in Blender

• Now select the robot in the hierarchy. In the inspector, under the Animation component, we see that Play Automatically is marked. This is why it was moving when we ran the game earlier.

• When we run it now, it will only play the Walk animation. This is because the Animation property is set to Walk.

Page 13: SE 320 – Introduction to Game Development

13

Using animations created in Blender

• Under Animations property, we should see all three of the animations. We can set Animation to be one of the others and view them by running the game

Page 14: SE 320 – Introduction to Game Development

14

Using animations created in Blender

• Now disable Play Automatically and let’s start to initiate animations through the code

Page 15: SE 320 – Introduction to Game Development

15

Starting animations in code• These are just like the animations that we

created in Unity’s Animation pane in week #8• Create a new C# script and add it to the robot. • Add the code below to create buttons that

initiate the animations

Page 16: SE 320 – Introduction to Game Development

16

Starting animations in code• Notice how clicking only once is sufficient for

starting the animation. Similarly, your code should call the .Play() function only when it’s needed. – We have talked about this extensively in Week #8

Page 17: SE 320 – Introduction to Game Development

17

Preparation for this week’s homework

• At the end of the slide deck you will see more information about this homework.

• Here we will go over the necessary steps for you to start working on the homework

• Download this package: http://homes.ieu.edu.tr/~galankus/teaching/12fall/se320/material/day11/animhw.unitypackage

• Create an empty Unity project and import the package

Page 18: SE 320 – Introduction to Game Development

18

(Create an empty Unity project and import the package)

Page 19: SE 320 – Introduction to Game Development

19

Preparation for this week’s homework

• Play the game and observe that you lose health when you hit the arrows, the axe, spikes, etc. Jump to the other side and get rich.

Page 20: SE 320 – Introduction to Game Development

20

Preparation for this week’s homework

• Now let’s prepare to add our animation. • In Hierarchy, under 3rd Person Controller,

remove Bip001 and construction_worker. Let the DamageReceiver stay.

Page 21: SE 320 – Introduction to Game Development

21

Preparation for this week’s homework

• Download the robot model from here:• http://homes.ieu.edu.tr/~galankus/teaching/12fall/se320/material/day11/robotmodel.zip

• Extract it, then import the extracted folder into your project by dragging it into the project pane

Page 22: SE 320 – Introduction to Game Development

22

Preparation for this week’s homework

• When you select the robot model in the project pane, you should see the robot textured in the preview part of the inspector pane

Page 23: SE 320 – Introduction to Game Development

23

Preparation for this week’s homework

• Drag the robot model into the scene at the beginning of the level

Page 24: SE 320 – Introduction to Game Development

24

Preparation for this week’s homework

• Now we will make it work with the 3rd person controller. – Create an empty game object and drag it under

3rd Person Controller. Rename it “Offsetter”– Reset its transform component– We will use this to locate the robot without

touching the robot’s transform, which is driven by animations coming from Blender.

Page 25: SE 320 – Introduction to Game Development

25

Preparation for this week’s homework

• Drag the robot under Offsetter• Revert the transform of the robot to

prefab by clicking “Revert to Prefab”. This will ensure that we don’t compete with the transform values coming from Blender’s animations.– Finally, these

transform valuesshould not be inbold

Page 26: SE 320 – Introduction to Game Development

26

Preparation for this week’s homework

• Now, when we run the game, we see that the robot is huge and sideways.– We will fix this using the Offsetter

Page 27: SE 320 – Introduction to Game Development

27

Preparation for this week’s homework

• Rotate and scale the offsetter so that the robot is smaller and looks at the correct direction.

Page 28: SE 320 – Introduction to Game Development

28

Preparation for this week’s homework

• From here on, it’s up to you to complete the rest of the homework. Take a look at the earlier parts of this slide deck for more information.

Page 29: SE 320 – Introduction to Game Development

29

Limitations of Animations• Animating displacement is static. In code, I

should be able to give a destination that I calculated, and an object should move there in a specific duration.

• Tweening libraries– GoKit• https://github.com/prime31/GoKit • https://github.com/prime31/GoKit/wiki • http://prime31.com/unity/docs/#goKitDoc

Page 30: SE 320 – Introduction to Game Development

30

Getting GoKit• Go to https://github.com/prime31/GoKit and

download a zip file• Extract it to a folder.

This is a Unity project.Open it with Unity.

Page 31: SE 320 – Introduction to Game Development

31

Getting GoKit• Open the scenes in the Demo folder and play

with them

Page 32: SE 320 – Introduction to Game Development

32

Using GoKit• To use GoKit in your own projects, you just

need to copy the Editor and Plugins folders into your own project

• Let’s create a new empty Unity project and copy these folders from the GoKit-master project into our project.

Page 33: SE 320 – Introduction to Game Development

33

Using GoKit• Let’s create a cube, a c# script "CubeScript"

and attach the script to the cube. • Create a directional light

and align the camera to view• When you run the game,

nothing happens, yet.

Page 34: SE 320 – Introduction to Game Development

34

Using GoKit• Add the code below to the script. When you run the game

and press the button, you will see the cube move to (5, 0, 0) in 3 seconds.

• Notice how GoKitadded new functions to the existing Transformclass

• Run it and see the cube move

Page 35: SE 320 – Introduction to Game Development

35

Using GoKit• Take a look at the GoKit Code Basics page in

the GoKit wiki: https://github.com/prime31/GoKit/wiki/1.-GoKit-Code-Basics

• Instead of calling extension methods such as transform.positionTo(), you can also do this– Go.to(someTransform, duration,

someTweenConfig)– This way you have more control over the

configuration of your tween.

Page 36: SE 320 – Introduction to Game Development

36

Creating Tween Configurations• Try this code, it will move the cube, make it

smaller and make it red, simultaneously in 3 seconds.

• The TweenConfig class encapsulates these operations.

Page 37: SE 320 – Introduction to Game Development

37

Using GoKit• You can tween any property you see in the components. For

example, lets move it by changing the position property by name:

• Instead of transform, you can use any other component and any property that they may have. It behaves as if you are changing the property using the inspector.

Page 38: SE 320 – Introduction to Game Development

38

Using GoKit• You can shorthand the config creation if you

want. The two code pieces below are equivalent.

Page 39: SE 320 – Introduction to Game Development

39

Using GoKit• You can also chain tweens one after the other, or place them

in different points in time, using TweenChain and TweenFlow classes

Page 40: SE 320 – Introduction to Game Development

40

Visual Path Editor• GoKit includes a visual path editor that you

can create paths composed of spline curves– You can create these paths visually, or you can

supply the points in runtime– Let’s see how we can use the visual path editor

Page 41: SE 320 – Introduction to Game Development

41

Visual Path Editor• Create a folder named

StreamingAssets in your project. This is where GoKit will save pathfiles.

• Create an empty game object and drag the GoDummyPath script onto it

Page 42: SE 320 – Introduction to Game Development

42

Visual Path Editor• When you select this GameObject, you will

see some new markers in the scene.• You can drag these to set the begin and end of

the path.• You can double click on them to create new

points for the path

Page 43: SE 320 – Introduction to Game Development

43

Visual Path Editor• Once you’re done, name your

path and save it using the inspector.

• The file in StreamingAssets folder contains information about this path.

• If you don’t want to create any new paths, you can remove this GameObject.

Page 44: SE 320 – Introduction to Game Development

44

Visual Path Editor• Here is how we can play this path in our code

Page 45: SE 320 – Introduction to Game Development

45

Visual Path Editor• When we run the game, we see that the cube

follows the path nicely

Page 46: SE 320 – Introduction to Game Development

46

TODO: Homework• Start with slides 17-28 here.• Using Blender animations

– Make the robot play the Walk animation while it is moving. Stop it when it is not moving (handle input independently in your script)

– Make the robot play the Damage animation when it receives a damage– Make the robot play the Rejoice animation when it reaches the gold. Play it only once.

• Using GoKit– Change the red balls so that they hit the character like a homing missile– Add another ball cannon that shoots green balls that go in a predefined path that looks like a

wave

• Get Özkan to grade your work, according to http://homes.ieu.edu.tr/~osayin/gd/homework_policy.html– [email protected] (NOTE THE ADDRES CHANGE!)– Subject (paste this): SE 320 Homework 11– What to send:

• Assets -> Export package • File -> Build Settings -> Web player -> Build (for both games)

– DUE DATE: Dec 11