Derived from Kirill Muzykov’s Rocket Mouse Tutorial @shahedC WakeUpAndCode.com

Preview:

Citation preview

By Shahed ChowdhuriSr. Technical Evangelist

Unity 2D: Step by Step, Part 3Derived from Kirill Muzykov’s Rocket Mouse Tutorialhttp://www.raywenderlich.com/69392/make-game-like-jetpack-joyride-unity-2d-part-1

@shahedC

WakeUpAndCode.com

Slicing Sprite Sheets

Select “Run” Sprite in Project panel

Select “Run”Sprite

Set “Sprite Mode” to “Multiple”

Click “Sprite Editor” Button

(Sprite Editor pops up!)

Slice It!1. Click Slice dropdown

2. Set Type to “Grid”

3. Set Pixel Size• X = 156• Y = 156

4. Click Slice button

Apply the Slice Click “Apply”

then, close popup

Verify Run Sprites in Project Panel

Separate spritesfor each frame

Creating Animations

Click Window Animation

Click on Animation tab

Create “Animations” Folder

Right-click Assets, click Create Folder Name it “Animations”

Select “Cat” in Hierarchy

Select “Cat”

Create New Animation

In Animation tab,

Click dropdown…

Create New Clip (twice)• run• fly

Create Clips for “run” and “fly”

Save “run.anim“ Save “fly.anim“

Verify Animations

Verify:• Cat animation• fly clip• run clip

Verify Animator Component

Select “Cat”

Verify “Animator”

Adding Run Frames

Select “run” Animation

Click “run” animationin dropdown

Expand “Run” Spritesheet

Expand “Run” animation in “Sprites” folder

Select “Run” Frames

Click first frame, press Shift on your keyboard, click last frame

Drag “Run” Frames into “run” Animation

Drag frames into “run” animation

Adding Fly Frames

Select “fly” Animation

Click “fly” animationin dropdown

Expand “Fly” Spritesheet

Expand “Fly” animation in “Sprites” folder(notice that there’s only 1 frame)

Select “Fly” Frames

Click Fly frame

Drag “Fly” Frame into “fly” Animation

Drag frame into “fly” animation

Stop Recording Animations

Click “red dot”recording iconto stop recordinganimations

(or just Save)

Run the Game!

The cat runs like crazy!!!

Run

Fixing the Animation

Update “run” Animation Sample Value

Update “Sample” to 8

Update Cat’s Animator Component

Uncheck Apply Root Motion

Change Update Mode to“Animate Physics”

Run the Game!

The cat runs slower now, but is still running in the air!

Run

Creating Transitions

Click Window Animator

Click on Animator tab

Create Transition…

Right-click “run” animation, then click “Make Transition”

Complete Transition… From “run” to “fly”

Click “fly” animation to finish creating the transition

Create Another Transition

Now, right-click “fly” animation, then click “Make Transition”

Complete Transition… From “fly” to “run”

Click “run” animation to finish creating the transition

Add Parameter

Click + next to Parameters (lower left of Animator tab)

Choose “Bool” (which is of type Boolean)Name it “grounded”

Set Parameter Condition For “run” “fly”

Select run fly animation

Select “grounded” under Conditions

Set to “false”

Set Parameter Condition For “fly” “run”

Select fly run animation

Select “grounded” under Conditions

Set to “true”

Run the Game!

Optional: Check/uncheck “grounded”, then play the game to test the animations

Run

Working with the Ground

Create Empty Game Object

Verify new Game Object

Drag GameObject into Cat, Rename It

Drag GameObject into “Cat”, verify that it is a child of “Cat”, rename to “GroundCheck”

Set “groundCheck” Position

Set Transform Position for “groundCheck”• X = 0• Y = -0.7• Z = 0

Select Icon and Color

Click icon symbol to select a color, e.g. green

Verify “groundCheck” in Scene

Verify “groundCheck” game object

Select Floor Inside Prefab

Expand “mount1” prefab, select “Floor”

Add Layer…

In Inspector panel, click Layer dropdown to Add Layer…

Enter “Ground” Layer

Enter “Ground” for User Layer 8

Assign Floor to “Ground” LayerUpdate Layer to “Ground”

Launch “CatController” Script

Add Instance Variables

Add 4 instancevariables beforeStart() method

Initialize Animator

Add 1 line of codeinside Start() method

Add New Method to Update StatusAdd new method UpdateGroundedStatus

… just before class ends at the bottom

Call Method to Update Grounded Status

Add 1 line of code

… just before FixedUpdate() ends

Verify New Items

Select cat in Hierarchy

Verify 2 new public items under Cat Controller script

Assign Ground Check Transform

With Cat selected…Drag “groundCheck”into Ground CheckTransform

Assign Ground Check Layer Mask

With Cat selected…set Ground CheckLayer Mask to“Ground”

Toggling the Flames

Launch “CatController” Script

Add Public Variable for Jetpack

Add 1 line of codejust before Start() method

Add Method to Adjust Jetpack

Add new method AdjustJetpack()

… just before class ends at the bottom

Call Method to Adjust Jetpack

Add 1 line of code

… just before FixedUpdate() ends

Assign Ground Check Transform

With Cat selected…drag “jetpackFlames”into Jetpack

Run the Game! Run

• The cat should run only while on the ground• The jetpack particles should only be visible while in the air

End of Part 3