How to build a 2D Space shooter with efficiency calculation in Unity3D

Shooter

In this blog post I’m going to show you how to build a 2D Space shooter with efficiency calculation in Unity3D.

I followed these two blog posts: post #1 and post #2, and updated it for the use with Unity 4.5 and added the counters for number of bullets fired and number of rocks generated and showed the precision efficiency.

You can try my version, or you can download the project on GitHub. Following are the steps on how to make it your self:

    1. Start a new 2D project in Unity3D
    2. Create folders Scripts, Scenes, Prefabs, Textures
    3. Put all the texture assets to Textures folder
    4. Save the (ctrl +s) scene to Scenes folder with some name
    5. Drag background to Hierarchy
    6. Drag ship to Hierarchy
      1. set y to -4
      2. add Rigidbody2D
      3. check IsKinematic (the gravity doesn’t affect it)
    7. Add script to ship:
    8. Add bullet to Hierarchy
      1. add Physics2D -> Rigidbody 2D
      2. add script:
      3. add bullet to Prefabs folder and delete it from the Hierarchy
      4. drag the bullet from Prefabs folder to the bullet variable in Inspector when the spaceship is selected
    9. Add an enemy (from Textures) to the Hierarchy
      1. add Rigidbody 2D
      2. set IsKinematic
      3. add script:
      4. Add enemy from Hierarchy to Prefabs folder and delete it from Hierarchy
    10. Add spawn object to Hierarchy
      1. position it above the background
      2. add script:
      1. drag enemy prefab to spawn object
    11. For the spaceship, the enemy prefab, and the bullet prefab do the following
      1. add Component -> Physics 2D -> Box Collider 2D
      2. for enemy Box Collider 2D check IsTrigger and this will give you the OnTriggerEnter2D function
    12. Create -> UI -> Text (name it ScoreText)
      1. Canvas -> RenederMode -> World space, no camera
      2. Rect transform – 450×340 (WxH)
      3. Add -> Script -> ScoreScript.js:

    13. Create -> UI -> Text (name it RocksText)
      1. Add -> Script -> RocksScript.js:

    14. Create -> UI -> Text (name it EffText)
      1. Add -> Script -> EffScript.js:
Written by Nikola Brežnjak