# 1. Weapon Design and WeaponDefinition
Part of [[Weapons, Hitscan and Projectiles]]. Next: [[2. Fire Control Rate Modes Magazines and Reload]].
## 1.1 Start with a job, not a gun
[[Level Up! (Review)]] keeps coming back to one rule for weapons: each one must answer a question the level asks. A shotgun answers "something is in my face"; a sniper rifle answers "something is far away and not looking at me". If two weapons answer the same question, one of them is dead weight. Before writing code, fill in a sheet for every weapon:
| Field | Rifle | Shotgun | Grenade launcher |
|---|---|---|---|
| Job | Reliable at mid range | Wins close fights | Clears groups and cover |
| Range band | 10–60 m | 0–12 m | 15–40 m, arcing |
| Counter | Being out-ranged | Distance | Moving targets, close range (self damage) |
| Readability | Steady tracer, punchy recoil | Big kick, wide pellets | Slow visible shell, big blast |
| Time to kill (body) | 0.5 s | 1 shot at 5 m | 1 direct + splash |
**Time to kill** is the number players feel most. It falls out of damage, fire rate and target health:
```
shotsToKill = ceil(health / damagePerShot)
TTK = (shotsToKill - 1) * (60 / RPM) // the first shot is instant
```
A 100 HP target, a 25-damage rifle at 600 RPM: 4 shots, so TTK is 3 × 0.1 s = 0.3 s. Drop damage to 24 and it's 5 shots and 0.4 s. A one-point change moved TTK by 33 %. That's why damage numbers want breakpoints in mind, and why they belong in data you can tune without recompiling.
## 1.2 Why a ScriptableObject
[[Game Programming Patterns (Review)]] calls this the **Type Object** pattern: instead of a `Rifle` subclass and a `Shotgun` subclass, you have one `WeaponDefinition` class and many assets. Designers make a new weapon by duplicating an asset. The behaviour scripts (`HitscanWeapon`, `ProjectileLauncher`) read the asset, and the runtime *state* (rounds left, current bloom) lives in plain C# objects that the asset creates. Keeping state out of the asset matters: a ScriptableObject is shared by every user of that weapon, and in the Editor, changes made to it in Play mode stick after you exit.
Damage *feel* isn't repeated here. The `attack` field is the `AttackDefinition` from [[1. Combat Feel and the Hit Pipeline]], so knockback, hit-stop and impact feedback work the same as for melee.
## 1.3 WeaponDefinition.cs
```csharp
using UnityEngine;
/// <summary>
/// One weapon's numbers as data (Type Object). Damage feel (hit-stop, knockback, impact VFX/audio)
/// reuses AttackDefinition from Combat Feel and Hit Feedback, so guns and swords share one hit pipeline.
/// </summary>
[CreateAssetMenu(menuName = "Gameplay/Weapon Definition", fileName = "Weapon")]
public sealed class WeaponDefinition : ScriptableObject
{
public string displayName = "Rifle";
public AttackDefinition attack; // damage, knockback, hit-stop, feedback
[Header("Fire control")]
public FireController.Mode mode = FireController.Mode.Auto;
[Min(1f)] public float roundsPerMinute = 600f;
[Min(1)] public int burstCount = 3;
[Min(1)] public int magazineSize = 30;
[Min(0)] public int startingReserve = 90;
[Min(0f)] public float reloadSeconds = 1.8f;
[Min(0f)] public float emptyReloadSeconds = 2.4f;
public bool chamberRound = true;
[Header("Hitscan")]
[Min(1)] public int pellets = 1; // >1 for shotguns
[Min(1f)] public float range = 150f;
public float falloffNear = 20f;
public float falloffFar = 60f;
[Range(0f, 1f)] public float falloffMin = 0.5f;
[Min(0f)] public float headshotMultiplier = 2f;
[Header("Spread (cone half-angle, degrees)")]
public float spreadBase = 0.3f;
public float spreadPerShot = 0.4f;
public float spreadMax = 5f;
public float spreadRecovery = 2f; // degrees per second; must be well below perShot × fire rate or bloom never builds
[Range(0f, 1f)] public float adsSpreadMultiplier = 0.35f;
[Header("Recoil kicks (yaw, pitch) per shot, degrees")]
public Vector2[] recoilPattern = { new Vector2(0f, 0.6f), new Vector2(0.1f, 0.7f), new Vector2(-0.15f, 0.8f), new Vector2(0.25f, 0.8f), new Vector2(-0.3f, 0.7f) };
public float recoilNoise = 0.08f;
public float recoilReset = 0.35f;
[Header("Projectile (leave empty for hitscan)")]
public Projectile projectilePrefab;
public float muzzleVelocity = 60f;
public float projectileGravity = 9.81f;
public float projectileDrag = 0f;
public bool IsHitscan => projectilePrefab == null;
public FireController CreateFireController() =>
new FireController(roundsPerMinute, mode, magazineSize, startingReserve, reloadSeconds, emptyReloadSeconds, burstCount, chamberRound);
public SpreadModel CreateSpread() => new SpreadModel(spreadBase, spreadPerShot, spreadMax, spreadRecovery);
public RecoilPattern CreateRecoil(int seed)
{
var kicks = new (float yaw, float pitch)[recoilPattern.Length];
for (var i = 0; i < kicks.Length; i++)
kicks[i] = (recoilPattern[i].x, recoilPattern[i].y);
return new RecoilPattern(kicks, recoilNoise, recoilReset, seed);
}
public DamageFalloff Falloff => new DamageFalloff(falloffNear, falloffFar, falloffMin);
}
```
Create assets with **Assets > Create > Gameplay > Weapon Definition**. Suggested starting values:
| Field | Rifle | Shotgun | Grenade launcher |
|---|---|---|---|
| mode / RPM | Auto / 600 | Semi / 70 | Semi / 60 |
| magazine / reserve | 30 / 90 | 6 / 24 | 4 / 8 |
| pellets | 1 | 8 | 1 |
| falloff near / far / min | 20 / 60 / 0.5 | 5 / 15 / 0.2 | not used |
| spread base / per shot / max | 0.3 / 0.4 / 5 | 4 / 0 / 4 | 0.5 / 0 / 0.5 |
| projectile | none | none | Grenade prefab, 30 m/s |
The attack for each: rifle `damage` 25, shotgun 12 per pellet, grenade 30 direct (plus `ExplosionDamage` on the shell, part 6).
The Create* methods are the only way runtime code should get state from a definition. Every user gets fresh state, and the asset stays read-only.
## Exercises
1. Fill in the design sheet for a fourth weapon whose job is "punish players who stand still at long range". What's its counter?
2. Work out the TTK of each starting weapon against 100 HP and 150 HP targets. Which breakpoint would you change first?