Weapon

The Weapon script is the central hub for handling player input, firing logic, state control, and coordination between components such as Barrel, Magazine

Summary

This component:

  • Manages weapon shooting states and modes
  • Handles player input and shooting patterns (auto, semi, burst)
  • Connects to other modular components (barrel, magazine, recoil)

Fields

FieldTypeDescription
_barrelBarrelReference to the weapon’s barrel. Required for functionality.
_recoilRecoilReference to the recoil controller. Optional.
_configWeaponConfigContains equip time and weapon events.
shootStateShootStateCurrent shooting state of the weapon (Resting, Shooting, Reloading).
OnShootingCanceledUnityEventTriggered when shooting is interrupted or stopped.
_waitRoutine, _queuedRoutine, _shootRoutineCoroutineInternal coroutines managing equip and shooting.

Properties

PropertyTypeDescription
barrelBarrelAccessor to the assigned barrel. Throws if not assigned.
magazineMagazineShortcut to the barrel’s magazine.
canShootboolReturns true if the weapon is not reloading and has ammo.

Public Methods

MethodDescription
void StartShooting()Begins the firing process if conditions are valid.
void StopShooting()Stops the firing process based on the mode.
void Holster()Triggers holster event and disables the weapon object.
IEnumerator WaitRoutine(float time, Action callback)Waits for time seconds before invoking a callback.

Protected Methods

MethodDescription
void Equip()Runs the equip routine and triggers OnEquiped.
void Fire()Starts the appropriate shooting coroutine based on the mode.
void ShootBullet()Instructs the barrel to fire and evaluates recoil.
void Halt()Cancels shooting, resets state, and stops effects.

Coroutines

MethodDescription
IEnumerator ShootAutomatic()Fires bullets continuously while held.
IEnumerator ShootSemiAutomatic()Fires a single bullet on press.
IEnumerator ShootBurst()Fires a 3-round burst per press.

Input Setup

  • The script uses WeaponControls, listening to Player.ShootPrimary.
  • StartShooting() is triggered on input start.
  • StopShooting() is triggered on input release.

Events

  • WeaponConfig.OnEquiped
  • WeaponConfig.OnHolster
  • OnShootingCanceled

Shooting Modes

The shooting mode is defined via the barrel:

ModeBehavior
SemiAutomaticFires one bullet per press.
BurstFires 3 bullets per press.
FullAutomaticFires continuously while pressed.

Dependencies

  • Requires a Barrel component assigned (must not be null).
  • Should connect to a Magazine and optionally a Recoil.

See Also