Skip to content Skip to sidebar Skip to footer

Widget HTML #1

Create 2D Shooter Game With Unity

Create 2D Shooter Game With Unity

Learn how to add a 2D shooting mechanic to your Top Down 2D Shooter Game! We implement different bullet settings and fire rates using Unity!

Enroll Now

Creating a 2D shooter game with Unity can be an exciting project that introduces you to game development concepts such as sprites, physics, and scripting. This guide will walk you through the essential steps to build a basic 2D shooter game.

1. Setting Up Unity

Download and Install Unity:

  1. Go to the Unity website.
  2. Download the Unity Hub and install it.
  3. Use Unity Hub to install the latest version of the Unity Editor.

Create a New Project:

  1. Open Unity Hub.
  2. Click on the "New" button.
  3. Select the 2D template.
  4. Name your project (e.g., "2D Shooter Game") and click "Create".

2. Preparing Your Game Assets

Sprites: You will need sprites for your player, enemies, bullets, and background. You can either create these assets yourself using tools like Photoshop or GIMP, or download free assets from websites like Kenney.nl.

Importing Assets:

  1. In Unity, go to the "Assets" folder in the Project window.
  2. Right-click and select "Import New Asset...".
  3. Choose your sprites and import them.

3. Setting Up the Player

Create Player GameObject:

  1. In the Hierarchy window, right-click and select "Create Empty".
  2. Rename this GameObject to "Player".

Add Sprite Renderer:

  1. With the "Player" GameObject selected, click "Add Component" in the Inspector.
  2. Search for "Sprite Renderer" and add it.
  3. Assign your player sprite to the Sprite Renderer.

Add Rigidbody2D:

  1. Click "Add Component" and search for "Rigidbody2D".
  2. Add it to the Player GameObject. Set Gravity Scale to 0 (since we don't want the player to be affected by gravity).

Add Collider2D:

  1. Click "Add Component" and search for "Box Collider 2D" (or a suitable collider for your player sprite).
  2. Adjust the collider to fit your player sprite.

4. Player Movement Script

Create Player Movement Script:

  1. In the Project window, right-click and go to Create > C# Script.
  2. Name it "PlayerMovement".
  3. Attach the script to the Player GameObject by dragging it to the Inspector.

Editing the Script: Open the PlayerMovement script and replace the content with the following code:

csharp
using UnityEngine; public class PlayerMovement : MonoBehaviour { public float moveSpeed = 5f; void Update() { float moveX = Input.GetAxis("Horizontal"); float moveY = Input.GetAxis("Vertical"); Vector2 moveDirection = new Vector2(moveX, moveY).normalized; transform.Translate(moveDirection * moveSpeed * Time.deltaTime); } }

5. Shooting Mechanism

Create Bullet Prefab:

  1. In the Hierarchy window, create an empty GameObject and name it "Bullet".
  2. Add a Sprite Renderer and assign your bullet sprite.
  3. Add a Rigidbody2D and set Gravity Scale to 0.
  4. Add a Box Collider 2D and adjust it to fit the bullet sprite.
  5. Drag the Bullet GameObject from the Hierarchy to the Project window to create a prefab, then delete the Bullet GameObject from the Hierarchy.

Create Shooting Script:

  1. Create a new C# script named "PlayerShooting".
  2. Attach the script to the Player GameObject.

Editing the Script: Open the PlayerShooting script and replace the content with the following code:

csharp
using UnityEngine; public class PlayerShooting : MonoBehaviour { public GameObject bulletPrefab; public Transform firePoint; public float bulletSpeed = 10f; void Update() { if (Input.GetButtonDown("Fire1")) { Shoot(); } } void Shoot() { GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation); Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>(); rb.velocity = firePoint.right * bulletSpeed; } }

Create Fire Point:

  1. In the Hierarchy window, right-click on the Player GameObject and select Create Empty.
  2. Rename this GameObject to "FirePoint".
  3. Position the FirePoint where you want the bullets to be shot from.
  4. Assign the FirePoint to the PlayerShooting script in the Inspector.

6. Enemy Setup

Create Enemy GameObject:

  1. In the Hierarchy window, create an empty GameObject and name it "Enemy".
  2. Add a Sprite Renderer and assign your enemy sprite.
  3. Add a Rigidbody2D and set Gravity Scale to 0.
  4. Add a Box Collider 2D and adjust it to fit the enemy sprite.

Create Enemy Movement Script:

  1. Create a new C# script named "EnemyMovement".
  2. Attach the script to the Enemy GameObject.

Editing the Script: Open the EnemyMovement script and replace the content with the following code:

csharp
using UnityEngine; public class EnemyMovement : MonoBehaviour { public float speed = 2f; private Vector2 direction = Vector2.left; void Update() { transform.Translate(direction * speed * Time.deltaTime); } private void OnTriggerEnter2D(Collider2D collision) { if (collision.CompareTag("Bullet")) { Destroy(gameObject); Destroy(collision.gameObject); } } }

7. Spawning Enemies

Create Enemy Spawner Script:

  1. Create an empty GameObject and name it "EnemySpawner".
  2. Create a new C# script named "EnemySpawner".
  3. Attach the script to the EnemySpawner GameObject.

Editing the Script: Open the EnemySpawner script and replace the content with the following code:

csharp
using UnityEngine; public class EnemySpawner : MonoBehaviour { public GameObject enemyPrefab; public float spawnRate = 2f; public float spawnRangeY = 4f; void Start() { InvokeRepeating("SpawnEnemy", 1f, spawnRate); } void SpawnEnemy() { float spawnY = Random.Range(-spawnRangeY, spawnRangeY); Vector2 spawnPosition = new Vector2(transform.position.x, spawnY); Instantiate(enemyPrefab, spawnPosition, Quaternion.identity); } }

8. Setting Up the Scene

Arrange the Scene:

  1. Add a background sprite if you have one.
  2. Position the Player GameObject.
  3. Place the EnemySpawner GameObject at the edge of the screen where you want enemies to spawn from.

Tagging:

  1. Tag the Player GameObject as "Player".
  2. Tag the bullet prefab as "Bullet".
  3. Ensure the enemies are untagged or given a specific tag if necessary.

9. Testing and Debugging

Play the Game:

  1. Click the "Play" button in Unity.
  2. Test the player movement, shooting, and enemy spawning.
  3. Debug any issues that arise, adjusting scripts and components as needed.

10. Adding Polish

Sound Effects:

  1. Import sound effects for shooting and enemy destruction.
  2. Add an AudioSource component to the Player and Enemy GameObjects.
  3. Play the sounds in the appropriate scripts.

UI Elements:

  1. Add a simple UI for the score and health.
  2. Use Unity's UI system to create text elements.
  3. Update the UI elements from your scripts as the game progresses.

11. Building the Game

Build Settings:

  1. Go to File > Build Settings.
  2. Add your current scene to the build.
  3. Choose your target platform (e.g., PC, Android, iOS).
  4. Click "Build" and follow the prompts to create an executable version of your game.

By following these steps, you'll have created a basic 2D shooter game with Unity. From here, you can expand your game with additional features, levels, and mechanics to enhance the gameplay experience. Happy developing!