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.

Unity 2D: Learning to build Flappy Bird game

Unity C#: Flappy Bird (flarpy blorb) create a 2D game where the player controls a bird and overcomes every obstacle on the screen to set a score.

Flappy Bird game is mostly played on mobile devices where the players controls the bird character by pressing keys from the keyboard and pass through the pipes by flapping its wings. If the bird collides with any pipe obstacle then the game will be over otherwise your score will be increased.

  1. Create 2D unity project named "Flarpy Blorb". In Game mode, Change Free Aspect to "Full HD (1920x1080)".
  2. By right-clicking on the project section, import a new asset i.e. birdbody.png and pipe.png.
  3. Select Main Camera from Hierarchy.
    /* 
    * Inspector (Main Camera)
    * Position: (0,0,10)
    * Rotation: (0,0,0)
    * Scale: (1,1,1)
    * Camera » Background: #314D79 (any)
    * Camera » Size: 15 (approx.)
    */
  4. On the Hierarchy, Right-click the empty space, select "Create Empty" game object, and renamed it with "Bird".
  5. Add Sprite Renderer, Rigidbody 2D, Circle Collider 2D and BirdScript Component on the Bird gameobject.
    /* 
    * Inspector (Bird GameObject)
    * Add Component » Rendering » Sprite Renderer 
    * Sprite Renderer » Sprite » birdbody (drag asset)
    * Add Component » Physics 2D » Rigidbody 2D
    * Add Component » Physics 2D » Circle Collider 2D
    * Circle Collider 2D » Offset: x = -0.03 (any), y = -0.45 (any)
    * Circle Collider 2D » Radius = 1.91 (approx.)
    * Add Component » New Script » BirdScript (named)» Create and Add
    * BirdScript (Script) » Flap Strength » 20 (manual)
    * Rigidbody 2D » Gravity Scale » 4.5 (any)
    * Layer » Add Layer... » User Layer X: Bird (Enter) (we will add it later)
    * Layer » X:Bird (Select) (we will do it later)
    */
  6. Create empty game object called "Pipe" for grouping. Under the Pipe groups, create another empty game object called "Top Pipe".
    /* 
    * Inspector (Top Pipe GameObject)
    * Position: (0,17.7,0)
    * Rotation: (0,0,0)
    * Scale: (1,1,1)
    * Add Component » Rendering » Sprite Renderer 
    * Sprite Renderer » Sprite » pipe (drag asset)
    * Add Component » Physics 2D » Box Collider 2D
    * Box Collider 2D » Offset: (0,0)
    * Box Collider 2D » Size: x = 4.73 (any), y = 21.04 (any)
    */
  7. Duplicate the Top Pipe game object and renamed with "Bottom Pipe".
    /* 
    * Inspector (Bottom Pipe GameObject)
    * Position: (0,-17.7,0)
    * Scale: (1,-1,1)
    * Other same as Top Pipe
    */
  8. For Pipe group, Add PipeMoveScript component.
    /* 
    * Inspector (Pipe GameObject)
    * Add Component » New Script » PipeMoveScript (named)» Create and Add
    */
  9. Create a prefab of Pipe group containing all pipe game objects. Select the Pipe grouped game object and drag it to the project tab asset section. After that, delete all pipe game objects including pipe group from Hierarchy tab.
    /* 
    * Pipe (Prefabs)
    * Top Pipe
    * Middle (We will add it later)
    * Bottom Pipe
    */
  10. Create a empty game object on the Hierarchy called "Pipe Spawner" and add PipeSpawnScript component on it.
    /* 
    * Inspector (Pipe Spawner GameObject)
    * Position: (28.5,0,0)
    * Add Component » New Script » PipeSpawnScript (named)» Create and Add
    * PipeSpawnScript (Script) » Pipe » Pipe (drag prefabs)
    */
  11. From the Hierarchy, Create new Legacy Text UI.
    /* 
    * Inspector (Canvas GameObject)
    * Canvas Scaler » UI Scale Mode » Scale With Screen Size (to look better on every device)
    * Canvas Scaler » Reference Resolution: x = 1920 (16:9), y = 1080 (16:9)
    */
    
    /* 
    * Inspector (Text : Legacy GameObject)
    * Pos X: -749.7 (any), Pos Y: 321.7 (any), Pos Z: 0
    * Width: 342.4 (any), Height: 373.1 (any)
    * Text: 0 (default)
    * Font Style: Bold, Font Size: 212 (any)
    * Color: #fff (white)
    */
  12. Create "Logic Manager" game object on the Hierarchy and add LogicScript component on it.
    /* 
    * Inspector (Logic Manager GameObject)
    * Add Component » New Script » LogicScript (named)» Create and Add
    * LogicScript (Script) » Score Text » Text (Legacy) (drag from hierarchy)
    * LogicScript (Script) » Game Over Screen » Game Over Screen (drag from hierarchy) (We will add it later)
    * Tag » Add Tag... » Logic (Save) (We will add it later)
    * Tag » Select Logic (We will do it later)
    */
  13. On the Pipe prefabs, create new game object named it "Middle" and add PipeMiddleScript component on it.
    /* 
    * Inspector (Pipe Prefab: Middle GameObject)
    * Add Component » Physics 2D » Box Collider 2D
    * Box Collider 2D » Is Trigger » True (check)
    * Box Collider 2D » Size: x = 0.85 (approx.), y = 11.6 (approx.)
    * Add Component » New Script » PipeMiddleScript (named)» Create and Add
    */
  14. Select Logic Manager from the Hierarchy tab, Create new tag called "Logic" and assign it.
    /* 
    * Script (PipeMiddleScript)
    * Finally, you can assign LogicScript on PipeMiddleScript using Logic tag
    */
    LogicScript logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<LogicScript>();
    
  15. Select Bird game object from the Hierarchy tab, Add "Bird" layer and select it. Don't forget to remember the position layer number of Bird layer.
    /* 
    * Script (PipeMiddleScript)
    * You can do this using layer position number
    */
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.layer == 3) // 3 means the bird position layer number that we assign it
        {
            // add score code
        }
    }
    
  16. Create "Game Over Screen" game object under the Canvas Hierarchy. Under it, Create new UI Legacy Text and Button.
    /* 
    * Inspector (Game Over Screen GameObject)
    * Game Object » Deactivate / hidden (uncheck)
    */
    
    /* 
    * Inspector (Game Over Screen » Text : Legacy GameObject)
    * Pos X: 0, Pos Y: 0, Pos Z: 0
    * Width: 942.72 (any), Height: 237 (any)
    * Text: Game Over
    * Font Style: Bold,
    * Font Size: 155 (any)
    * Alignment: Center (Horizontal), Center (Vertical)
    * Color: #fff (white)
    */
    
    /* 
    * Inspector (Button: Legacy GameObject)
    * Pos X: 0, Pos Y: -196.6 (approx.), Pos Z: 0
    * Width: 569.51 (any), Height: 113.1 (any)
    * On Click () » Add empty event » Logic Manager (drag from hierarchy) » restartGame (select method from LogicScript)
    */
    
    /* 
    * Inspector (Button » Text : Legacy GameObject)
    * Text: Play Again
    * Font Style: Normal
    * Font Size: 68 (any)
    * Alignment: Center (Horizontal), Center (Vertical)
    * Color: #323232 (black)
    */
  17. On the Logic Manager from the Hierarchy, Drag the Game Over Screen game object to the Game Over Screen property of Logic Script (Script) component.
  18. Don't forget to disabled Game Over Screen game object.
  19. Scripts: Unity C# Scripting: BirdScript, PipeMiddleScript, PipeSpawnScript & LogicScript
  20. Finally, build the project.
    /* 
    * Build Project
    * File » Build Settings... » Select OS platform (Windows) » Build » Flarpy (folder)
    */
    
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