Menu
CoddyTech

Recap: Modular Turret

Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 5 of 64.

challenge icon

Challenge

Easy

Build Turret1's weapon. TurretControl and Cannon are ready.

  1. In Ammo, write public bool Take(): use a shell and return true, or log name + " is empty" and return false.
  2. Make Weapon abstract with [RequireComponent(typeof(Ammo))], and Fire() protected abstract with no body.
  3. Pull() does nothing during cooldown; else, if Take(), restart the cooldown and call Fire().

Press Play and fire until it is empty. Then Check.

Try it yourself

using UnityEngine;

// What every turret weapon shares.
public class Weapon : MonoBehaviour
{
    public GameObject shellPrefab;
    public float cooldown = 1f;

    public void Pull()
    {
        // wait out the cooldown, take a shell from the Ammo, then fire
    }

    // Each weapon fires its own way.
    protected virtual void Fire()
    {
    }

    // A shell from this spot, seen from the turret, flying the way the turret faces.
    protected void Launch(Vector3 offset)
    {
        Instantiate(shellPrefab, transform.position + transform.rotation * offset, transform.rotation);
    }
}

All lessons in Game Architecture in Unity

Practice on your own: Unity playground