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

FieldTypeDescription
_barrelBarrelReference to the weapon’s barrel. Required for functionality.
_magazineMagazineThe magazine the weapon draws ammo from. Leave empty for infinite ammo.
_eventsWeaponEventsContains the OnEquipped and OnHolster events.
_useBuiltInInputboolRead the built-in WeaponControls bindings. Disable when another script drives the weapon (custom input, AI, networking).
shootStateShootStateCurrent shooting state of the weapon (Resting, Shooting, Reloading).
OnCanceledUnityEventTriggered when shooting is interrupted or stopped.

Properties

PropertyTypeDescription
barrelBarrelAccessor to the assigned barrel. Throws if not assigned. Setting it makes the weapon the barrel’s owner.
magazineMagazineThe weapon’s magazine. null means infinite ammo.
triggerHeldboolTrue between StartShooting and StopShooting.

Public Methods

MethodDescription
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

MethodDescription
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

MethodDescription
IEnumerator ShootRoutine()Fires shots at the barrel’s fire rate until shooting stops.

Input Setup

When _useBuiltInInput is on, the script uses WeaponControls:

  • Player.ShootPrimary started calls StartShooting().
  • Player.ShootPrimary canceled calls StopShooting().
  • Player.Reload started calls Reload().

Turn it off and call these methods yourself to drive the weapon from your own input, AI or network code.

Events

  • WeaponEvents.OnEquipped
  • WeaponEvents.OnHolster
  • OnCanceled

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 of BarrelConfig levels applied to the barrel, with Upgrade(), SetLevel(int), level, maxLevel and OnUpgraded.

Dependencies

  • Requires a Barrel component assigned (must not be null).
  • A Magazine is optional. Recoil is optional and needs no reference: it listens to the barrel’s events.

See Also