◀ 3.13.23.3 ▶

3.2 — Projectiles: Instantiate, Fire, Destroy

Your turret knows when you are near; now it needs teeth. Everything a projectile needs, you have already written once, somewhere else. Your job today is to recognise that, and then prove it — no API table this time, two files, and neither of them new.

In this lesson you will be able to answer“how do I shoot a projectile?”Solo build
Break It Down~10 minSolo build

A turret that shoots at me. That is the whole feature, and it is far too big to hold in your head at once — which is exactly why every lesson in this course starts the same way.

First, before any code: cut that into parts small enough that you could look each one up on its own.

No parts listed here on purpose. Splitting the problem up is the work of this lesson — write your list before you open the file.

Notice what is NOT on that list: no new API except the spawner, and even that you have seen. Five parts, four of them lifted straight out of lessons you have already finished. That is what a well-sequenced course feels like from the inside — not "here is a new thing you have never seen", but "here is the same five things, pointed at a new target".

The test for a finished split: could you look this part up on its own, right now, in one search? "Make the turret shoot" cannot be looked up. "Clone a prefab at a position" can. If a part is still too big to search for, it is two parts.

Your turn

List the parts of this problem, in the order you would build them.

There is more than one way to make the bullet travel. Name two, say what each one costs you in components, and say which you picked and why.

In the Editor ~15 min
  1. Project window → Assets/Prefabs/Turret_Projectile, then Inspector → an empty Projectile component and nothing else — no collider, no body. Whatever your bullet needs to move, you add it yourself.
  2. Hierarchy → a Turret instance in the Overworld, then Inspector → Turret Controller: two slots sit empty, and they fill up in step 3. Expand the turret and click the child SpawnPoint — that is the muzzle, not the turret's centre and not the world origin.
  3. Once the code half is done — Inspector → Turret Controller → drag Assets/Prefabs/Turret_Projectile from the Project window into the prefab slot, and the Hierarchy's SpawnPoint into the muzzle slot. Do both turrets. An empty slot says nothing at startup and throws the instant you step into range, which looks like a firing bug and is a setup bug.
  4. Game view → press Play and walk into range: a projectile appears at the muzzle and travels. Do not celebrate the fire rate — you are firing every frame, because the range check runs every frame, and that is the next lesson's problem.
In the Code ~35 min

What the finished feature does, from the player's seat:

  1. Something appears at the end of the barrel — not at the turret's centre, and not at the world origin.
  2. It flies toward where I was standing when it was fired. If I keep moving, it does not follow me: it commits to the shot it took.
  3. It travels at the same speed whether I am against the turret or at the far edge of its range.
  4. It cleans itself up a few seconds later if it hits nothing.
Architectural constraint (the only thing I will hand you): you are editing two files and neither knows the other's details. TurretController.cs decides when to fire and where the bullet starts. Projectile.cs owns everything after that — which way it goes, how fast, and when it dies. If you find yourself writing the bullet's speed into the turret, stop: the turret should not know that number exists.

Two Files, One Shot

  1. The turret fires from its muzzle rather than from its centre. Which of your parts does that change, and what would a bullet spawned at the turret's centre look like to the player?
  2. The bullet commits to the direction it was fired in and does not follow you. Which line of your plan makes that true, and what would you have to add to make it home in on the player instead?
  3. To turn this into a shotgun that fires five bullets in a spread, which of the two files do you rewrite — and what does your answer prove about why the split is worth having?

Checkpoint — before moving on, you must be able to:

Reference Docs

Need a deeper explanation? These pages cover the same concepts:

Next: 3.3 — Fire Rate: Cooldowns & Time, where you fix the bullet hose you just built.

0/5 answered

Answers save on this device as you type. At the end of class, click Download and upload the file to the Canvas assignment.

Saved on this device only — the Dashboard tracks your progress.