0% found this document useful (0 votes)
6 views

겜프 7

Uploaded by

cracraty159
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

겜프 7

Uploaded by

cracraty159
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Jonghyeok Park

Division of Computer Engineering,


Hankuk University of Foreign Studies (HUFS)

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Unity 2D
• Unity is a complete platform for 2D
– Unity 2D core project template

• How to make a 2D Game project (abstraction)


– Add game object
– Add sprite image
– Write a script
§ Collision
§ Physics engine
– Add UI
– Game scene

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Sprite
• Image or a series of images used to represent 2D graphic objects.
• Visual elements in games
– Backgrounds
– Character
– Items
– Effects

• Unity Sprite Renderer component

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Flappy Boo

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Make a 2D Project
• Set project name: FlappyBoo
• Download the resource (FlappyBoo.zip)

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Project Setup
• Create folders
– Scripts
– Prefabs
– Sprites

• Copy resource images to Assets folder

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Create Boo Character
• Crate game object and name it as “Boo”
• Sprite rendering
– Add Component > Rendering > Sprite Renderer
– Add boo.png to sprite

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Change Background Color
• Change Main Camera’s background color
– RGB (141, 113, 80)

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Change GameView Screen size
• Set FullHD (1920*1080)

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Add Component to Boo
• Add RigidBody 2D
• Add Capsule Collider 2D
– Set size (2,4)

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Write Boo.cs C# script
• Create Boo.cs (save in Scripts folder) and attach script
• Change Boo’s name to ”HUFS Boo” once game is started
• Make Boo jump whenever the space key is pressed

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


TODO 1. Tune flap strength of Boo
• Make a variable “flapstrength” in Boo Class
• Change it in the inspector view
– Hint1. Use public float variable (simple)
– Hint2. Use [SerializeField] + private float variable

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Spawning new objects (1)
• Create Pipe object
– Create Empty > name it as Pipe
– Create Empty on Pipe object > name it as Top Pipe
– Add Component > Rendering > Sprite Renderer
– Set the size

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Spawning new objects (2)
• Create Pipe object
– Duplicate Top Pipe and name it as “Bottom Pipe”
– Set the size (revert Y-axis)
– We made parent-child relationship

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Spawning new objects (3)
• Write PipeMove.cs C# script
– Attach to Pipe (parent)

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Spawning new objects (4)
• Make Pipe as a Prefab
– Drag Pipe (whole parent-child) to Prefabs folder
– Delete Pipe in the hierarchical view

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Spawning new objects (5)
• Create Pipe Spawner object
• Write PipeSpawn.cs and attach to the Pipe Spawner
• Instantiating Prefabs (Pipe) at run time

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


What’s the problem?

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Spawning new objects (6)
• Add spawn rate using timer
• Change the PipeSpawner position

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park
Spawning new objects (7)
• Make a variation of Pipe heights
• Modify the SpwanPipe() function in PipeSpawn.cs

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park
Spawning new objects (8)
• Clear pipe when Boo is passed
– Define the deadzone for spawning pipes
• Modify PipeMove.cs

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Set Score UI (1)
• Create UI > Legacy > Text
• Set Canvas
– UI Scale Mode: Scale With Screen Size
– Reference Resolution : X: 1920 Y: 1080

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Set Score UI (2)
• Modify Canvas > Text position (Left Top)
• Set Text to 0 and adjust the size
– Width and Height
– Font Size and color

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Set Score UI (3)
• Create > Empty Object and name it “Logic Manger”
• Write Logic.cs Script and attach to Logic Manger

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Set Score UI (4)
• Drag to Canvas > Text to Logic Manager’s Text

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Set Score UI (5)
• Use “ContextMenu”

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Score Management (1)
• How to add score ?
– Increase the score when Boo is passed middle of the Pipe

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Score Management (2)
• Add middle pipe in the Pipe (in the Prefab)
– Add Box Collider 2D
– Set the size (0.85 * 5)
– Check Is Trigger

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Score Management (3)
• Write PipeMiddle.cs
– And attach to Middle

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Score Management (4)
• How to interact with Logic Manger and Middle Pipe
– Create Tag, and name it “Logic” for Logic

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Score Management (5)
• Let’s make sure that it was actually Boo went through the middle of pipe
– We can set the layer, instead of tag
– Add layer of the Boo and set the Layer

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Score Management (5)
• How to pass the variable in addScore function?
– Interact with Middle Pipe and Logic Manger

PIpeMiddle.cs Logic.cs

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Game Over Scene Management (1)
• Create empty and name it as ”Game Over Screen” in the Canvas
– Add UI > Legacy > Text on “Game Over Screen”
§ Set text as “Game Over”
§ Adjust the position, size, and color
– Add UI > Legacy > Button on “Game Over Screen”
§ Resize and set text as “Paly Again”
§ Adjust the position, size, and color

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Game Over Scene Management (2)
• Add restartGame() function in Logic.cs

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Game Over Scene Management (3)
• Attach Logic Manger and restartGame() function on Button click

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


What’s the Problem?

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Game Over Scene Management (4)
• Disable Game Over Screen

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Game Over Scene Management (5)
• Add gameOver() function in Logic.cs

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Game Over Scene Management (6)
• Attach Game Over Screen

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Game Over Scene Management (7)
• Don’t forget add box collider 2D

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


What’s the Problem?

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Game Over Scene Management (8)
• Add Boolean variable

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Add Audio Source (1)
• Add audio source in Logic manager
– Add ding.wma into AudioClip
– Disable Play on Awake

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Add Audio Source (2)
• Add audio source when Boo pass the middle of pipe

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


TODO 2. Add Game Over Situation
• When Boo is
– Hint1. Use transform component
– Hint2. Check the y position in the game view

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


Extract the Game
• File > Build Setting > Build and Run
– GameName: StudentID

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park


References
• https://github.com/Britishgaming/GMTK-Unity-Tutorial/tree/main/Assets

Game Programming (T01402201) Hankuk University of Foreign Studies Jonghyeok Park

You might also like