Weapon
The Weapon script is the central hub for handling player input, firing logic, ammo, state control, and coordination between components such as Barrel and Magazine.
Summary
This component:
- Manages weapon shooting states
- Handles player input and fires the barrel at its fire rate while shooting
- Owns the magazine and consumes ammo per shot (no magazine means infinite ammo)
Fields
| Field | Type | Description |
|---|---|---|
_barrel | Barrel | Reference to the weapon’s barrel. Required for functionality. |
_magazine | Magazine | The magazine the weapon draws ammo from. Leave empty for infinite ammo. |
_events | WeaponEvents | Contains the OnEquipped and OnHolster events. |
_useBuiltInInput | bool | Read the built-in WeaponControls bindings. Disable when another script drives the weapon (custom input, AI, networking). |
shootState | ShootState | Current shooting state of the weapon (Resting, Shooting, Reloading). |
OnCanceled | UnityEvent | Triggered when shooting is interrupted or stopped. |
Properties
| Property | Type | Description |
|---|---|---|
barrel | Barrel | Accessor to the assigned barrel. Throws if not assigned. Setting it makes the weapon the barrel’s owner. |
magazine | Magazine | The weapon’s magazine. null means infinite ammo. |
triggerHeld | bool | True between StartShooting and StopShooting. |
Public Methods
| Method | Description |
|---|---|
void StartShooting() | Begins the firing process if conditions are valid. |
void StopShooting() | Releases the trigger. The barrel’s modifiers decide when shooting stops. |
void Reload() | Reloads the weapon’s magazine. Bound to the Reload input when built-in input is used. |
void Interrupt() | Stops shooting immediately. Called by the magazine when it runs out. |
void Holster() | Raises the holster event. Called automatically when the weapon is disabled. |
IEnumerator WaitRoutine(float time, Action callback) | Waits for time seconds before invoking a callback. |
Protected Methods
| Method | Description |
|---|---|
bool CanShoot() | True if the weapon is not reloading and the magazine (if any) holds enough ammo. |
void Equip() | Raises the equip event. Called automatically when the weapon is enabled. |
virtual void ShootBullet() | Instructs the barrel to fire and consumes ammo. |
virtual void Halt() | Cancels shooting, resets state, and stops effects. |
Coroutines
| Method | Description |
|---|---|
IEnumerator ShootRoutine() | Fires shots at the barrel’s fire rate until shooting stops. |
Input Setup
When _useBuiltInInput is on, the script uses WeaponControls:
Player.ShootPrimarystarted callsStartShooting().Player.ShootPrimarycanceled callsStopShooting().Player.Reloadstarted callsReload().
Turn it off and call these methods yourself to drive the weapon from your own input, AI or network code.
Events
WeaponEvents.OnEquippedWeaponEvents.OnHolsterOnCanceled
Shooting Modes
The weapon has no shooting modes of its own: it fires while shooting. Semi automatic and burst come from a Firing Mode on the barrel, which stops the shooting once a pull’s shots are out. A barrel without one fires full automatic.
Subclasses
DualWeapon: a primary and a secondary barrel.UpgradableWeapon: a list ofBarrelConfiglevels applied to the barrel, withUpgrade(),SetLevel(int),level,maxLevelandOnUpgraded.
Dependencies
- Requires a
Barrelcomponent assigned (must not be null). - A
Magazineis optional. Recoil is optional and needs no reference: it listens to the barrel’s events.