1. Trang chủ
  2. » Giáo án - Bài giảng

Lecture 55 section notes tủ tài liệu training

101 28 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 101
Dung lượng 1,16 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

In this lecture…• Researching brick sizes.. • Edit our brick sprites.. • Import full brick, left brick, right brick.. Add a ball to your scene• Import the ball sprite to Sprites folder..

Trang 1

BLOCK BREAKER SLIDE DECK

Trang 2

In this video…

• Why we’re adding this section.

• When we will be adding content.

• How to continue the course.

Trang 3

Why we’re adding this section…

• To ease you in to 2D collisions more gently

• To give you a game you can create levels for

• To make the early learning journey less steep

• Because the game is fun!

Trang 4

When we will be adding content

• During the week of the 10th November

Trang 5

How to continue the course

• Read the Game Design Doc, and try it yourself.

• Wait for each video to arrive, and consume it then.

• Skip this whole section for now, and revisit, you’ll be

fine!

This Section Is In Progress

Trang 6

About Block Breaker

Trang 7

What Block Breaker Teaches

• 2D Collisions & destroying objects

• Triggering SFX and music

• Responding to mouse input

• Automated playtesting

• Build & share your own levels

Trang 8

Creating A 2D Paddle Sprite

Trang 9

In this lecture…

• Researching brick sizes

• Source an appropriate image

• Edit our brick sprites

• Create new project and import

Trang 10

Researching brick sizes

• Typical Arkanoid had about 14 bricks wide

• Brick sizes from Wikipedia article

• 128 x 40 gives aspect of 3.2 precisely*

• iPad Air is 2048 x 1536 total (1.33 : 1)

*but need 128*41 for it to work in Unity, trust me!

Trang 11

Source an appropriate image

• I used Google Images

• Filtered for “reuse with modification”

• Looking for textured, even, straight-on

• Cropped out to 128 x 41

Trang 12

Edit our brick sprites

• Desaturate so we can change it’s colour later

• The texture makes the bricks look better

• Create two new images for left and right

• Care to ensure the mortar looks right

Trang 13

Create new project and import

• Create a new project “Block Breaker”

• Import full brick, left brick, right brick

• Put in “Sprites” folder

• Create “_Scenes” folder, and save Level_01

Trang 14

Import & Modify The Menu System

Trang 15

In this lecture…

• Export the menus from Number Wizard UI

• Import to this project

• Customise the words & fonts

• Wire up the buttons

Trang 16

Wire Up The Buttons

• Get the Start button to go to Level_01

• Add temporary “Game Over” button to Level_01

• “Play Again” button to go to Start Menu

• Test that you can navigate fully

Trang 17

Playing Background Music

Trang 18

In this lecture

• How persistent music improves quality

• Add a Music Player Game Object

• Add a music track of your choice

• Test your music plays consistently

Trang 19

Add a music track of your choice

• Register and activate www.freesound.org

• Go for Creative Commons license

• Download and drag into “Assets > Sounds”

Trang 20

Introducing Static Variables

Trang 21

In this lecture…

• How a static can help us here

• Watch Unity’s short video*

• Preventing duplicate music players

http://unity3d.com/learn/tutorials/modules/ intermediate/scripting/statics

Trang 22

Setting Up Your Play Space

Trang 23

Ball + Gravity + Colliders = Fun

Trang 24

Add a ball to your scene

• Import the ball sprite to Sprites folder

• Set a sensible “Pixels Per Unit” value

• Place the ball in the middle of the play space

Trang 25

Colliders, Triggers & Collisions in Unity

Trang 26

“Collider components define the shape of an object for the purposes of physical collisions A collider,

which is invisible, need not be the exact same

shape as the object’s…“

http://docs.unity3d.com/Manual/CollidersOverview.html

Trang 27

In this video

• If colliders overlap during a frame then…

• … messages may be passed by the engine

• What is message passing?

Trang 28

What is message passing?

• Two game objects with colliders meet

• Engine sends a message to the objects

• We “intercept” this message in our script

• Our script decides what to do next

http://en.wikipedia.org/wiki/Message_passing

Trang 29

Signatures of messages passed…

Collision detection…

void OnCollisionEnter2D (Collision2D collision)

Triggers…

void OnTriggerEnter2D (Collider2D collider)

Green object names are our choice, and provide information about the the interaction

Trang 30

Types of colliders explained…

• Static: if it has a collider but NO rigidbody

• Rigidbody: if it’s got a rigidbody component

• Kinematic: if “Kinematic” ticked on rigidbody

If it’s going to move during the game, slap a

Trang 31

Collider interaction matrix

trigger trigger trigger trigger

trigger trigger trigger trigger trigger trigger

trigger

trigger trigger trigger trigger

trigger

trigger

trigger trigger

trigger

trigger trigger

Derived from http://docs.unity3d.com/Manual/CollidersOverview.html

Trang 32

Using Unity’s 2D Sprite Editor

Trang 33

In this lecture…

• Turning an image into a sprite

• “Pixels Per Unit” explained

• Understanding the pivot point

Trang 34

“Pixels Per Unit” explained

• Pixels To Units = 128

• This means 128 pixels equals one world unit

• World units arbitrary, but think of as 1 meter

• Test against the grid size

Trang 35

Add the half-bricks

• Import your half-brick images

• Set same “pixels to world units” as full brick

• Build a small wall (2 full, 2 half bricks)

Trang 36

Tidying Up Before Moving On

Trang 37

In this lecture…

• Delete Music Player on Level_01 scene

• Setting Game window to 800 x 600

• Two handy keyboard shortcuts

• Remove Canvas and Event System from Level_01

• Make loose collider load next level

Trang 38

Loose collider loads next level

• Ball falls off the screen

• Triggers the Loose Collider

• The Win Screen scene loads

• Music plays throughout

Trang 39

Choosing The Right Collider

Trang 40

In this lecture…

• Add our brick sprite as a player paddle

• Choosing our paddle collider type

• CHALLENGE: Add components to our paddle

Trang 42

Choosing our paddle collider

trigger trigger trigger trigger

trigger trigger trigger trigger trigger trigger

trigger

trigger trigger trigger trigger

trigger

trigger

trigger trigger

trigger

trigger trigger

Ball

Trang 43

Add a collider to the paddle

• Find & add the appropriate 2D collider

• Find & add the RigidBody2D component

• Ensure ball stops when it hits paddle

Trang 44

Using Physics Materials For Bounce

Trang 45

In this lecture…

• What is a physics material

• Add a bouncy material

• Observe funky physics

Trang 46

Physics Materials

“The Physics Material is used to adjust friction and bouncing effects of colliding objects.”

http://docs.unity3d.com/Manual/class-PhysicMaterial.html

Trang 47

How bounciness works

• 0 = No energy conserved in collision

• 1 = 100% of energy conserved in collision

• Use square root to convert to COR*

• For 1/2 height bounce Sqrt(0.500) = 0.707

http://en.wikipedia.org/wiki/Coefficient_of_restitution

Trang 48

GameObject Movement By Mouse

Trang 49

In this lecture…

Screen.width to get screen width

• Move the paddle

• Fix the paddle’s “Pixels per unit” to 128

Trang 50

Print mousePosInBlocks

• Setup a variable of appropriate type

• Set to the expression we just created

• Print the variable not the expression

Trang 51

Constrain paddle to game space

• Use the Math.Clamp method*

• Set minimum x value to 0.5f

• Set maximum x value to 15.5f

http://docs.unity3d.com/ScriptReference/Mathf.Clamp.html

Trang 52

Launching Ball On Mouse Click

Trang 53

In this lecture…

• Start the ball sitting on the paddle

• Capture the relative position from the editor

Trang 54

Invisible Colliders & Gravity Scale

Trang 55

In this lecture…

• Setup all your play space wall colliders

• Adjust the initial velocity and gravity

Trang 56

Add Top and Right Colliders

• Add colliders of the same width

• Ensure there are no spaces

• Test by playing

Trang 57

Understanding Gravity Scale

• Could be set in RigidBody, but is a bit weird

• Use Edit > Project Settings > Physics 2D

• Uses equations of uniform acceleration*

http://en.wikipedia.org/wiki/Acceleration#Uniform_acceleration

Trang 58

Creating & Using Unity Prefabs

Trang 59

In this lecture…

• What is a prefab

• Why prefabs are useful

• Setting up your prefabs

• How prefab linking works

Trang 60

What is a prefab

“…a collection of predefined GameObjects &

Components that are re-usable throughout your game”

http://docs.unity3d.com/Manual/InstantiatingPrefabs.html

Trang 61

Make More Brick Types

• Make at least two more brick prefabs

• Give them different colours & maxHits

• Delete all non-prefab instances except paddle

• Test them by playing & checking inspector

Trang 62

Unity Editor Snap To Grid

Trang 63

In this lecture…

• How Edit > Snap Settings works

• Snap initially to get on the grid

• You can do this with multi-select

• Hold cmd (ctrl) while dragging!

Trang 64

Build Your First Level

• Lay a couple of lines of bricks

• Organise hierarchy with empty objects

• Have fun!

Trang 65

Making Everything A Prefab

Trang 66

In this lecture…

• Make everything a prefab!

• Set Main Camera background to black

• Move & group Loose Collider

• Test by making new level

Trang 67

Create Level_02

• Save this current Level_01 scene

• Make a new scene and save it

• Drag all prefabs into Hierarchy.

• Separate out the 3 bricks, snapping to grid

Trang 68

Using GameObject.FindObjectOfType

Trang 69

In this lecture…

• Why linking prefabs programmatically helps

• Unity doesn’t support “nested prefabs”

• Link the ball to the paddle programatically

• Challenge: do this for LevelManager

Trang 70

Do the same for LooseCollider.cs

• Make the public instance variable private

• Find the LevelManager in Start()

• Test that levels still load properly

Trang 71

Level Management & Build Order

Trang 72

In this lecture…

• Create Loose Scene, modify LooseCollider.cs

• Add LoadNextLevel() to LevelManager.cs

• Add all our levels to Project > Build Settings

• Modify Block.cs by adding SimulateWin()

• Test that game transitions between levels

Trang 73

Modify your loose collider prefab

• Change to call LevelManager.LoadNextLevel()

• Test that it all works OK

Trang 74

Destroying gameObjects When Hit

74

Trang 75

In this lecture…

• How the Destroy() method works.*

• Why we destroy gameObject not this

• Challenge: only destroy on max hits

http://docs.unity3d.com/ScriptReference/Object.Destroy.html

Trang 76

Only destroy on max hits

• Use an if statement around Destroy

• Yellow blocks destroy on first hit

• Green on 2nd hit

• Red on 3rd

• Test by playing

Trang 77

Creating & Importing Sprite Sheets

Trang 78

In this lecture

• Why a sprite sheet is useful

• Key features of a sprite sheet

• Creating sprites for partially broken blocks

• Importing sprites into Unity

Trang 79

Your image editor needs…

• To support transparent backgrounds

• To allow you to resize your “canvas”

• Ideally to support layers

Trang 80

Create damaged bricks

• Add two more sprites

• One slightly damaged (1 hit)

• One very damaged (2 hit)

• Import as 1 hit and 2 hit

• Test by applying to prefabs temporarily

Trang 81

Arrays & Swapping Sprites In Script

Trang 82

In this lecture…

• The affordance principle

• What is an array?

• Using arrays to store these sprites

• Loading sprite when hit

Trang 83

The affordance principle

• The player should always know what do to next

• We want multi-hit blocks to show progress

• Player then knows to keep hitting them

http://en.wikipedia.org/wiki/Affordance

Trang 84

What is an array?

“…a collection of data items that can be selected by

indices computed at run-time…”

http://en.wikipedia.org/wiki/Array#In_computer_science

Trang 85

Consolidating Hit Counting

Trang 86

In this lecture…

• What are tags?*

• Why tags are useful for keeping track

• Tagging unbreakable bricks

• Use tags to decide when level is won

http://docs.unity3d.com/Manual/Tags.html

Trang 87

Statics To Detect Win Condition

Trang 88

In this lecture…

• Why loading levels could be problematic

• How a static Brick variable can help

• Keeping track of breakable bricks in the level

• Creating a simple BrickDestroyed “message”

• Testing inc when 2 bricks destroyed at once

Trang 89

Add a static breakableCount variable

• Choose the appropriate type (float, int, bool)

• Declare in the right place in Bricks.cs

• Initialise to zero

• Make it public so we can see in the inspector

• Test by playing and observing the inspector

Trang 90

Playing Sound Effects On Impact

Trang 91

In this lecture…

• Using audio.Play() to play “boing” sound;

• Why AudioSource.PlayClipAtPoint useful

• Using this for playing “crack”

• Test & demonstrate

Trang 92

Write Correct Method in Ball.cs

• OnCollisionEnter2D() or OnTriggerEnter2D()?

• Write the method signature

• Play the attached audio every time (for now)

Trang 93

Why NOT work this out now?

“Truly superior pilots are those who use their

superior judgment to avoid those situations where they might have to use their superior skills.”

Anonymous pilot’s saying

Trang 94

Stop Boring Loops With Random.Range()

Trang 95

Automated Play Testing

Trang 96

Create & tidy you levels

• Ensure Hierarchy is tidy in each level

• Give each level a different 800x600 background

• Play test all the levels

• Ensure level order is right in File > Build Order

Trang 97

Build & Share On The Web

Trang 98

In this lecture…

• Revising setting your Game window

• How to tweak sound levels

• Doing a test web build and playing locally

• Revising building and sharing to web

• Looking forward to seeing your levels!

Trang 99

How to adjust sound levels

• Master volume: Edit > Project Settings > Audio

• “Boing” sound: Ball prefab > Audio Source

• “Crack” sound: Add volume paramater in code*

http://docs.unity3d.com/ScriptReference/

AudioSource.PlayClipAtPoint.html

Trang 100

Recap & What’s next

Trang 101

Recap & What’s Next

• Congratulations, you’ve learnt a lot

• Make your own levels

• Ask a friend to make levels

• Care with commercial music

• Share your creation in Discussions, or

www.CompleteUnityDeveloper.com

Ngày đăng: 17/11/2019, 07:35