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
| Field | Type | Description |
|---|
_barrel | Barrel | Reference to the weapon’s barrel. Required for functionality. |
_recoil | Recoil | Reference to the recoil controller. Optional. |
_config | WeaponConfig | Contains equip time and weapon events. |
shootState | ShootState | Current shooting state of the weapon (Resting, Shooting, Reloading). |
OnShootingCanceled | UnityEvent | Triggered when shooting is interrupted or stopped. |
_waitRoutine, _queuedRoutine, _shootRoutine | Coroutine | Internal coroutines managing equip and shooting. |
Properties
| Property | Type | Description |
|---|
barrel | Barrel | Accessor to the assigned barrel. Throws if not assigned. |
magazine | Magazine | Shortcut to the barrel’s magazine. |
canShoot | bool | Returns true if the weapon is not reloading and has ammo. |
Public Methods
| Method | Description |
|---|
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
| Method | Description |
|---|
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
| Method | Description |
|---|
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.OnEquipedWeaponConfig.OnHolsterOnShootingCanceled
Shooting Modes
The shooting mode is defined via the barrel:
| Mode | Behavior |
|---|
SemiAutomatic | Fires one bullet per press. |
Burst | Fires 3 bullets per press. |
FullAutomatic | Fires continuously while pressed. |
Dependencies
- Requires a
Barrel component assigned (must not be null). - Should connect to a
Magazine and optionally a Recoil.
See Also