T echnojs
Already have an account?
login Login

Or,

Don't have an account?
group_add Register

Showing suggestions...

close
cookie
Cookies Consent

This site uses cookies to offer you a better browsing experience.

Best practices for building Unity 2D Monster Chase game

Monster Chase is the best thrilling game development in 2D Unity that engages and entertains players. Learn Unity 2D by building a Monster Chase Game.

Unity 2D : Monster Chase Game

Learning to build Monster Chase game.

  1. Create a new 2D project named "Monster Chase".
  2. First, rename the sample scenes by the "Gameplay" name and make sure the gameplay scene is inside the Scene folder.
  3. Set the Free Aspect to "Full HD (1920x1080)" in the game mode and import fonts & sprites assets to the project.
  4. Select the Players.png asset from the sprites assets directory.
    /*
    * Inspector (Players.png Sprite Asset)
    * Sprite Mode » Multiple (change) » Apply (button)
    * Sprite Editor » Fullscreen (maximize) 
    * Sprite Editor » Slice » Type » Automatic (select) » Slice (button)
    * Sprite Editor » Players_0 (slice outlined) » Rename » Player_1_1 (any) » Repeat (upto Player_1_8)
    * Sprite Editor » Players_8 (slice outlined) » Rename » Player_2_1 (any) » Repeat (upto Player_2_8)
    * Sprite Editor » Apply (button) » Player 1 & 2 has individual character sprites.
    */
  5. Create the following folder directory inside the project assets. Also, create an animator controller for each player.
    /*
    * Project (tab) » Assets » Animations » Player Animations » Player 1 Animations
    * Player 1 Animations » Right click » Animator Controller (select) » Player 1 Controller (rename)
    * Project (tab) » Assets » Animations » Player Animations » Player 2 Animations
    * Player 2 Animations » Right click » Animator Controller (select) » Player 2 Controller (rename)
    */
    
  6. Drag only the Player 1 sprite asset to the hierarchy and create a prefab of it.
    /*
    * Inspector (Player 1 GameObject)
    * Add Component » Animator
    * Animator Component » Controller » Player 1 Controller (drag from Player 1 Animations folder)
    * For Animation (tab) » three-dot menu » Add tab » Animation (select)
    * Animation » Create (button) » Idle.anim (save as)
    * Animation » timeline (Idle) » Player_1_1.png (drag from sprites assets)
    * Animation » Idle (menu) » Create new clip... » Walk.anim (save as)
    * Animation » timeline (Walk) » Player_1_1 to Player_1_7 PNG (drag from sprites assets)
    * Animation » Walk (animation) » three-dot menu (from timeline) » Set Sample Rate » 24 (control walking speed at fps)
    * Animator (tab) » Go to Windows » Animation » Animator (select)
    * Animator » Parameters » Walk (add)
    * Animator » Idle animation » right-click » Make Transition (select) » Walk animation (click) » Arrow (select) » Conditions (Inspector) » Walk parameter (add) » true (set)
    * Animator » Walk animation » right-click » Make Transition (select) » Idle animation (click) » Arrow (select) » Conditions (Inspector) » Walk parameter (add) » false (set)
    * Animator » Idle to Walk animation arrow (select) » Settings (Inspector) » Transition Duration » 0 (any)
    * Animator » Walk to Idle animation arrow (select) » Settings (Inspector) » Transition Duration » 0 (any)
    * Animator » Walk animation button (click) » Speed » 0.8 (approx.)
    * Add Component » Physics 2D » Capsule Collider 2D » Edit collider (click) » If needed adjust the outline of the collider to body character level
    * Add Component » Physics 2D » Rigidbody 2D
    * Rigidbody 2D » Gravity Scale » 2 (any)
    * Sprite Renderer » Additional Settings » Sorting Layer » Add Sorting Layer... » Player (add)(select)
    * Sprite Renderer » Additional Settings » Order in Layer » 1 (set)
    * Add Component » New Script » Player (named)» Create and Add
    * Tag » Player (Select)
    * Rigidbody 2D » Constraints » Freeze Rotation » Z (checked)
    * Projects » Assets » Prefabs (create)
    * Prefabs » Player 1 (drag from hierarchy)
    * Delete the Player 1 game object from the hierarchy, later it will be added from the script
    */
  7. Drag Player 2 sprite asset to the hierarchy. Repeat the above steps to create animation & prefab of it.
  8. Create a new empty game object on hierarchy called "Game BG". Also, put the Game BG game object just above the Players game object on the hierarchy.
    /*
    * Inspector (Game BG GameObject)
    * Game BG » Transform » Reset
    * Game BG » Background Holder (create game object)
    * Game BG » Background Holder » Transform » Reset
    * Game BG » Background Holder » Background (drag from sprites asset)
    * Game BG » Background Holder » Background » Transform » Reset
    * Game BG » Moon (drag from sprites asset)
    * Game BG » Moon » Transform » Position : x = -6.74 (any), y = 3.01 (any), z = 0
    * Game BG » Ground Holder (create game object)
    * Game BG » Ground Holder » Transform » Reset
    * Game BG » Ground Holder » Ground (drag from sprites asset)
    * Game BG » Ground Holder » Ground » Transform » Position : x = 0 (any), y = -5.78 (approx.), z = 0
    * Game BG » Ground Holder » Ground » Tag » Add Tag... » Ground (add)(select )
    */
  9. Add a sorting layer and order in layer to Background, Moon & Ground game object.
    /*
    * Inspector (Background GameObject)
    * Sprite Renderer » Additional Settings » Sorting Layer » Add Sorting Layer... » Background (add)(select)
    * Sprite Renderer » Additional Settings » Order in Layer » 0 (set)
    */
    
    /*
    * Inspector (Moon GameObject)(GroundGameObject)
    * Sprite Renderer » Additional Settings » Sorting Layer » Background (select)
    * Sprite Renderer » Additional Settings » Order in Layer » 1 (set)
    */
    
  10. Duplicate Background game object using CTRL + D to create more background and for joining the edeges of the backgrounds, press and hold "V" key by selecting one of the background and drag the dotted square box from edge to another edge of background.
  11. Add Box collider 2D to Ground game object & duplicate the Ground to match the Background for player landing.
  12. Create a new script called "CameraFollow" for Main Camera to follow player movement.
    /*
    * Inspector (Main Camera GameObject)
    * Add Component » New Script » CameraFollow (named) » Create and Add
    * Camera Follow (Script) » Min X: -60 (any), Max X: 200 (any)
    */
  13. Select the Enemies.png asset from the sprites assets directory.
    /*
    * Inspector (Enemies.png Sprite Asset)
    * Sprite Mode » Multiple (change) » Apply (button)
    * Sprite Editor » Fullscreen (maximize) 
    * Slice » Type » Automatic (select) » Slice (button)
    * Enemies_0 (select) » Rename » Ghost_1 (any) » Repeat (upto Ghost_7)
    * Enemies_7 (select) » Rename » Enemy_1 (any) » Repeat (upto Enemy_14)
    * Apply (button) » Ghost, Enemy 1 & 2 has individual character sprites.
    * Ghost_1 » Hierarchy (drag) » Ghost (rename)
    * Enemy_1 » Hierarchy (drag) » Enemy 1 (rename)
    * Enemy_8 » Hierarchy (drag) » Enemy 2 (rename)
    */
  14. Create the following folder directory inside the project assets. Also, create an animator controller for each enemies.
    /*
    * Project (tab) » Assets » Animations » Enemy Animations
    * Enemy Animations » Enemy 1 (folder) » Right click » Animator Controller (select) » Enemy 1 Controller (rename)
    * Enemy Animations » Enemy 2 (folder) » Right click » Animator Controller (select) » Enemy 2 Controller (rename)
    * Enemy Animations » Ghost (folder) » Right click » Animator Controller (select) » Ghost Controller (rename)
    */
  15. Create a prefab of Ghost, Enemy 1 and Enemy 2 game object.
    /*
    * Inspector (Ghost, Enemy 1 & Enemy 2 GameObject)
    * Sprite Renderer » Additional Settings » Sorting Layer » Player (select)
    * Sprite Renderer » Additional Settings » Order in Layer » 1 (set)
    * Add Component » Animator
    * Ghost (obj) » Add Component » Physics 2D » Box Collider 2D » Is Trigger » true (enabled)
    * Except Ghost (obj) » Add Component » Physics 2D » Capsule Collider 2D » Edit collider outline if needed (click)
    * Add Component » Physics 2D » Rigidbody 2D
    * Rigidbody 2D » Constraints » Freeze Rotation » Z (checked)
    * Ghost (obj) » Transform » Position: x = 0, y = -3 (approx.), z = 0
    * Ghost (obj) » Rigidbody 2D » Body Type » Kinematic (select)
    * Add Component » New Script » Monster (named)» Create and Add
    * Ghost (obj) » Animator Component » Controller » Ghost Controller (select)
    * Enemy 1 (obj) » Animator Component » Controller » Enemy 1  Controller (select)
    * Enemy 2 (obj) » Animator Component » Controller » Enemy 2 Controller (select)
    * For Animation (tab) » three-dot menu » Add tab » Animation (select)
    * Ghost (obj) » Animation (tab) » Create (button) » Ghost Animation.anim (save as inside animations of Ghost folder)
    * Enemy 1 (obj) » Animation (tab) » Create (button) » Enemy 1 Animation.anim (save as inside animations of Enemy 1 folder)
    * Enemy 2 (obj) » Animation (tab) » Create (button) » Enemy 2 Animation.anim (save as inside animations of Enemy 2 folder)
    * Ghost (obj) » Animation (tab) » timeline (Ghost Animation) » Ghost_1 to Ghost_7 PNG (drag from sprites assets)
    * Enemy 1 (obj) » Animation (tab) » timeline (Enemy 1 Animation) » Enemy_1 to Enemy_7 PNG (drag from sprites assets)
    * Enemy 2 (obj) » Animation (tab) » timeline (Enemy 2 Animation) » Ghost_7 to Enemy_14 PNG (drag from sprites assets)
    * Animation(tab) » Ghost Animation, Enemy 1 Animation & Enemy 2 Animation  (animation)(each obj) » three-dot menu (from timeline) » Set Sample Rate » 24 (set)
    * Tag » Add Tag... » Enemy (save) (select)
    * Layer » Add Layer... » User Layer X: Enemy (add)(select)
    * Projects » Assets » Prefabs » Ghost, Enemy 1 & Enemy 2 (drag from hierarchy)
    * Delete Ghost, Enemy 1 & Enemy 2 game object from hierarchy after creating prefab
    */
  16. Create an "Spawmer" empty game object on hierarchy with transform reset. Inside it, create "Left" and "Right" empty game object.
    /*
    * Inspector (Spawner GameObject)
    * Spawner » Transform » Reset
    * Spawner » Left » Transform » Position: x = -80 (any), y = -1.64 (approx.), z = 0
    * Spawner » Right » Transform » Position: x = 280 (any), y = -1.64 (approx.), z = 0
    * Add Component » New Script » MonsterSpawner (named) » Create and Add
    * Lock the inspector tab if needed and drag Ghost, Enemy 1 & Enemy 2 prefab on Monster Reference heading.
    * Monster Spawner (Script) » Monster Reference » three element (Ghost, Enemy 1 & Enemy 2)
    * Monster Spawner (Script) » Left Pos » Left (obj from hierarchy)
    * Monster Spawner (Script) » Right Pos » Right (obj from hierarchy)
    */
  17. To disabled collision on enemy to enemy object.
    /*
    * Assuming we already add "Enemy" layer to the enemy objects i.e. Ghost, Enemy 1 & Enemy 2.
    * Edit » Project Settings.. » Physics 2D » Layer Collision Matrix » Enemy (horizontal) to Enemy (vertical) » false (disabled)
    */
  18. Create an "Collector Holder" empty game object on hierarchy with transform reset. Inside it, create "Left Collector" and "Right Collector" empty game object.
    /*
    * Inspector (Left Collector & Right Collector GameObject)
    * Add Component » Physics 2D » Box Collider 2D
    * Box Collider 2D » Is Trigger » true (enabled)
    * Box Collider 2D » Offset: x=1, y=1
    * Box Collider 2D » Size: x=0.1, y=18 (approx.)
    * Left Collector (obj) » Transform » Position: x = -85 (any), y = -3.3 (approx.), z = 0
    * Right Collector (obj) » Transform » Position: x = 301 (any), y = -3.3 (approx.), z = 0
    * Add Component » New Script » Collector (named) » Create and Add
    */
  19. Create two new UI button on the hierarchy, then select "Button" from UI menu and renamed it with "Restart" and "Home".
    /*
    * Inspector (Canvas GameObject)
    * Canvas » Render Mode » Screen Space - Camera (select)
    * Canvas » Render Mode » Render Camera » Main Camera (drag from hierarchy)
    * Canvas » Render Mode » Sorting Layer » Add Sorting Layer... » UI (add)(select)
    * Canvas Scaler » UI Scale Mode » Scale With Screen Size (select)
    * Canvas Scaler » Reference Resolution : x = 1920, y = 1080
    * Canvas Scaler » Match » 0.5
    * Add Component » New Script » GameplayUIController (named)» Create and Add
    */
    
    /*
    * Inspector (Restart & Home GameObject)
    * First delete text object of button from the hierarchy
    * Restart (obj) » Image » Source Image » Restart (drag from Sprites folder)
    * Home (obj) » Image » Source Image » Home (drag from Sprites folder)
    * Image » Set Native Size (button) (click)
    * Restart (obj) » On Click () » Add empty event » Canvas (drag from hierarchy) » RestartGame (select method from GameplayUIController script)
    * Home (obj) » On Click () » Add empty event » Canvas (drag from hierarchy) » HomeButton (select method from GameplayUIController script)
    */
  20. Creating new scene for UI System: unity-2d-building-ui-system-for-monster-chase-game
by
question_answer Comments
bar_chart Views
people_alt Contributors
category Category workspaces Group
public Visibility
monetization_on Monetisation
checklist Authorization
save Created
today Updated

Any Question / Leave a comment ?
--- Thank you for your attention! ---

Related Posts