How to Create a 2D Shooter Game in Unity: Step-by-Step Guide
To create a 2D shooter in Unity, set up a player GameObject with movement controls using
Rigidbody2D and shoot bullets by instantiating projectile prefabs on input. Use simple C# scripts to handle player movement, shooting mechanics, and enemy interactions within the Unity Editor.Syntax
Here is the basic syntax pattern for player movement and shooting in a 2D shooter using Unity C# scripts:
Rigidbody2D: Controls physics-based movement.Input.GetAxis: Reads player input for movement.Instantiate: Creates bullet objects when shooting.Update(): Runs every frame to check input and update states.
csharp
using UnityEngine; public class PlayerController : MonoBehaviour { public float moveSpeed = 5f; public Rigidbody2D rb; public GameObject bulletPrefab; public Transform firePoint; Vector2 movement; void Update() { // Get input movement.x = Input.GetAxis("Horizontal"); movement.y = Input.GetAxis("Vertical"); // Shoot bullet on space key if (Input.GetKeyDown(KeyCode.Space)) { Shoot(); } } void FixedUpdate() { // Move player rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime); } void Shoot() { Instantiate(bulletPrefab, firePoint.position, firePoint.rotation); } }
Example
This example shows a simple 2D shooter player script that moves with arrow keys or WASD and shoots bullets when pressing space. Bullets are instantiated at a fire point and move forward automatically.
csharp
using UnityEngine; public class Bullet : MonoBehaviour { public float speed = 10f; public Rigidbody2D rb; void Start() { rb.velocity = transform.right * speed; Destroy(gameObject, 2f); // Destroy bullet after 2 seconds } } public class PlayerController : MonoBehaviour { public float moveSpeed = 5f; public Rigidbody2D rb; public GameObject bulletPrefab; public Transform firePoint; Vector2 movement; void Update() { movement.x = Input.GetAxis("Horizontal"); movement.y = Input.GetAxis("Vertical"); if (Input.GetKeyDown(KeyCode.Space)) { Shoot(); } } void FixedUpdate() { rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime); } void Shoot() { Instantiate(bulletPrefab, firePoint.position, firePoint.rotation); } }
Output
Player moves with arrow keys or WASD; pressing space shoots bullets forward from the fire point.
Common Pitfalls
Common mistakes when creating a 2D shooter in Unity include:
- Not assigning the
Rigidbody2Dcomponent to the player or bullet, causing movement or physics to fail. - Forgetting to set the bullet's velocity, so bullets don't move.
- Instantiating bullets without a proper fire point, causing bullets to spawn at wrong positions.
- Using
Update()for physics movement instead ofFixedUpdate(), leading to jittery movement. - Not destroying bullets after some time, causing performance issues.
Example of a wrong and right way to move the player:
csharp
// Wrong: Moving player in Update without Rigidbody2D void Update() { transform.position += new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0) * moveSpeed * Time.deltaTime; } // Right: Moving player in FixedUpdate with Rigidbody2D void FixedUpdate() { Vector2 movement = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")); rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime); }
Quick Reference
Tips for creating a 2D shooter in Unity:
- Use
Rigidbody2Dfor smooth physics movement. - Handle input in
Update()and movement inFixedUpdate(). - Instantiate bullets at a designated fire point with correct rotation.
- Set bullet velocity in
Start()and destroy after a few seconds. - Use prefabs for bullets and enemies for easy reuse.
Key Takeaways
Use Rigidbody2D and FixedUpdate for smooth player movement in 2D.
Instantiate bullet prefabs at a fire point and set their velocity to shoot.
Handle input in Update and separate movement logic in FixedUpdate.
Destroy bullets after a short time to keep performance stable.
Assign all required components in the Unity Editor to avoid runtime errors.