How to Make a Cannon Game in Unity

Creating a cannon game in Unity is a great way to learn the basics of game development. In this tutorial, we will create a simple game where the player can aim and shoot a cannon to hit targets. We'll cover setting up the project, creating and controlling a cannon, and adding targets.

Step 1: Setting Up the Project

First, open Unity and create a new 2D project. Name your project "CannonGame". Once the project is created, you will be in the main Unity editor window.

Step 2: Creating the Cannon

We need a visual representation of the cannon. You can create a simple rectangle sprite to act as your cannon. To do this:

  1. Right-click in the Hierarchy window and select Create Empty. Name it "Cannon".
  2. Right-click on the "Cannon" object and select 2D Object -> Sprite. Name it "CannonSprite".
  3. In the Inspector window, click the Sprite field and select a sprite for your cannon.

Position the cannon at the bottom of the screen by setting its position to (0, -4, 0) in the Transform component.

Now, create a script to control the cannon. Right-click in the Assets folder, select Create -> C# Script, and name it "CannonController". Double-click the script to open it in your code editor.

// CannonController.cs
using UnityEngine;

public class CannonController : MonoBehaviour
{
    public GameObject cannonballPrefab;
    public float shootForce = 700f;

    void Update()
    {
        Aim();
        if (Input.GetButtonDown("Fire1"))
        {
            Shoot();
        }
    }

    void Aim()
    {
        Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector2 direction = new Vector2(mousePosition.x - transform.position.x, mousePosition.y - transform.position.y);
        transform.up = direction;
    }

    void Shoot()
    {
        GameObject cannonball = Instantiate(cannonballPrefab, transform.position, transform.rotation);
        Rigidbody2D rb = cannonball.GetComponent();
        rb.AddForce(transform.up * shootForce);
    }
}

Step 3: Creating the Cannonball

We need to create a cannonball for the cannon to shoot. Here's how:

  1. Right-click in the Hierarchy window and select 2D Object -> Sprite. Name it "Cannonball".
  2. In the Inspector window, click the Sprite field and select a sprite for your cannonball.
  3. Add a Rigidbody2D component to the cannonball by clicking Add Component and selecting Rigidbody2D.
  4. Add a CircleCollider2D component to the cannonball by clicking Add Component and selecting CircleCollider2D.

Next, drag the cannonball from the Hierarchy to the Assets folder to create a prefab. This prefab will be used by the cannon to shoot cannonballs.

In the Inspector window of the CannonController script (attached to the Cannon object), set the CannonballPrefab field to the cannonball prefab.

Step 4: Creating Targets

Let's add some targets for the player to shoot at:

  1. Right-click in the Hierarchy window and select 2D Object -> Sprite. Name it "Target".
  2. In the Inspector window, click the Sprite field and select a sprite for your target.
  3. Add a BoxCollider2D component to the target by clicking Add Component and selecting BoxCollider2D.

Duplicate the target by right-clicking on it and selecting Duplicate. Position the targets around the screen.

Step 5: Handling Collisions

We need to handle what happens when a cannonball hits a target. Create a script called "Target" to manage this:

  1. Right-click in the Assets folder, select Create -> C# Script, and name it "Target".
  2. Double-click the script to open it in your code editor.
// Target.cs
using UnityEngine;

public class Target : MonoBehaviour
{
    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Cannonball"))
        {
            Destroy(gameObject);
        }
    }
}

Attach the Target script to all target objects. Make sure your cannonball prefab has the tag "Cannonball".

Conclusion

By following these steps, you have created a simple cannon game in Unity. You've learned how to set up a project, create and control a cannon, and add targets. This project covers basic game development concepts such as object creation, user input, physics, and collision detection. You can further enhance this game by adding features like scoring, levels, and different types of targets. Have fun experimenting and learning more about Unity!

Links
Unity 6