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 1BLOCK BREAKER SLIDE DECK
Trang 2In this video…
• Why we’re adding this section.
• When we will be adding content.
• How to continue the course.
Trang 3Why 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 4When we will be adding content
• During the week of the 10th November
Trang 5How 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 6About Block Breaker
Trang 7What Block Breaker Teaches
• 2D Collisions & destroying objects
• Triggering SFX and music
• Responding to mouse input
• Automated playtesting
• Build & share your own levels
Trang 8Creating A 2D Paddle Sprite
Trang 9In this lecture…
• Researching brick sizes
• Source an appropriate image
• Edit our brick sprites
• Create new project and import
Trang 10Researching 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 11Source 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 12Edit 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 13Create 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 14Import & Modify The Menu System
Trang 15In this lecture…
• Export the menus from Number Wizard UI
• Import to this project
• Customise the words & fonts
• Wire up the buttons
Trang 16Wire 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 17Playing Background Music
Trang 18In 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 19Add a music track of your choice
• Register and activate www.freesound.org
• Go for Creative Commons license
• Download and drag into “Assets > Sounds”
Trang 20Introducing Static Variables
Trang 21In 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 22Setting Up Your Play Space
Trang 23Ball + Gravity + Colliders = Fun
Trang 24Add 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 25Colliders, 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 27In this video
• If colliders overlap during a frame then…
• … messages may be passed by the engine
• What is message passing?
Trang 28What 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 29Signatures 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 30Types 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 31Collider 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 32Using Unity’s 2D Sprite Editor
Trang 33In 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 35Add 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 36Tidying Up Before Moving On
Trang 37In 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 38Loose collider loads next level
• Ball falls off the screen
• Triggers the Loose Collider
• The Win Screen scene loads
• Music plays throughout
Trang 39Choosing The Right Collider
Trang 40In this lecture…
• Add our brick sprite as a player paddle
• Choosing our paddle collider type
• CHALLENGE: Add components to our paddle
Trang 42Choosing 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 43Add a collider to the paddle
• Find & add the appropriate 2D collider
• Find & add the RigidBody2D component
• Ensure ball stops when it hits paddle
Trang 44Using Physics Materials For Bounce
Trang 45In this lecture…
• What is a physics material
• Add a bouncy material
• Observe funky physics
Trang 46Physics Materials
“The Physics Material is used to adjust friction and bouncing effects of colliding objects.”
http://docs.unity3d.com/Manual/class-PhysicMaterial.html
Trang 47How 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 48GameObject Movement By Mouse
Trang 49In this lecture…
• Screen.width to get screen width
• Move the paddle
• Fix the paddle’s “Pixels per unit” to 128
Trang 50Print mousePosInBlocks
• Setup a variable of appropriate type
• Set to the expression we just created
• Print the variable not the expression
Trang 51Constrain 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 52Launching Ball On Mouse Click
Trang 53In this lecture…
• Start the ball sitting on the paddle
• Capture the relative position from the editor
Trang 54Invisible Colliders & Gravity Scale
Trang 55In this lecture…
• Setup all your play space wall colliders
• Adjust the initial velocity and gravity
Trang 56Add Top and Right Colliders
• Add colliders of the same width
• Ensure there are no spaces
• Test by playing
Trang 57Understanding 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 58Creating & Using Unity Prefabs
Trang 59In this lecture…
• What is a prefab
• Why prefabs are useful
• Setting up your prefabs
• How prefab linking works
Trang 60What is a prefab
“…a collection of predefined GameObjects &
Components that are re-usable throughout your game”
http://docs.unity3d.com/Manual/InstantiatingPrefabs.html
Trang 61Make 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 62Unity Editor Snap To Grid
Trang 63In 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 64Build Your First Level
• Lay a couple of lines of bricks
• Organise hierarchy with empty objects
• Have fun!
Trang 65Making Everything A Prefab
Trang 66In this lecture…
• Make everything a prefab!
• Set Main Camera background to black
• Move & group Loose Collider
• Test by making new level
Trang 67Create 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 68Using GameObject.FindObjectOfType
Trang 69In 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 70Do the same for LooseCollider.cs
• Make the public instance variable private
• Find the LevelManager in Start()
• Test that levels still load properly
Trang 71Level Management & Build Order
Trang 72In 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 73Modify your loose collider prefab
• Change to call LevelManager.LoadNextLevel()
• Test that it all works OK
Trang 74Destroying gameObjects When Hit
74
Trang 75In 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 76Only 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 77Creating & Importing Sprite Sheets
Trang 78In 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 79Your image editor needs…
• To support transparent backgrounds
• To allow you to resize your “canvas”
• Ideally to support layers
Trang 80Create 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 81Arrays & Swapping Sprites In Script
Trang 82In this lecture…
• The affordance principle
• What is an array?
• Using arrays to store these sprites
• Loading sprite when hit
Trang 83The 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 84What 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 85Consolidating Hit Counting
Trang 86In 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 87Statics To Detect Win Condition
Trang 88In 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 89Add 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 90Playing Sound Effects On Impact
Trang 91In this lecture…
• Using audio.Play() to play “boing” sound;
• Why AudioSource.PlayClipAtPoint useful
• Using this for playing “crack”
• Test & demonstrate
Trang 92Write Correct Method in Ball.cs
• OnCollisionEnter2D() or OnTriggerEnter2D()?
• Write the method signature
• Play the attached audio every time (for now)
Trang 93Why 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 94Stop Boring Loops With Random.Range()
Trang 95Automated Play Testing
Trang 96Create & 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 97Build & Share On The Web
Trang 98In 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 99How 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 100Recap & What’s next
Trang 101Recap & 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