Barrel

The Barrel is the core extension of a weapon that defines how it shoots. It controls spawn points, shooting modes, firing rate, and triggers all visual/audio cues via UnityEvents.

Warning

Barrel is an abstract base class. Do not use it directly — use a subclass like RaycastBarrel or ProjectileBarrel.

Summary

This component:

  • Defines spawn points for bullets or effects
  • Handles bullet count, fire rate, and shooting mode
  • Fires UnityEvents on start, shot, and end
  • Loops through multiple spawn points if defined
  • Meant to be extended for specific firing behavior

Fields

FieldTypeDescription
_spawnPointsList<Transform>List of locations used to spawn projectiles or traces.
magazineMagazineThe ammo container assigned to this barrel.
weaponWeaponThe owning weapon. Automatically assigned during setup.
bulletsFiredintNumber of bullets or shots fired per trigger.
shootingModeShootingModeHow the barrel fires (auto, semi, burst).
fireRatefloatHow many times per second the barrel can fire.

Unity Events

EventDescription
_OnStartShootingCalled when the player starts shooting.
_OnShotCalled when a bullet/projectile is fired.
_OnEndShootingCalled when the player stops shooting.

Methods

MethodDescription
virtual void StartShooting()Triggers _OnStartShooting. Override for custom behavior.
virtual void StopShooting()Triggers _OnEndShooting. Override for custom behavior.
virtual void Shoot()Called when it’s time to fire. Override to implement projectile or ray logic.
virtual void Prepare(Action callback)Called before Shoot(). Use this for warmups, charge, or animations.

Spawn Point Behavior

If multiple _spawnPoints are assigned, the barrel cycles through them in order for each shot, useful for multi-barrel weapons or offset spread.

Usage Example

Do not use Barrel directly. Instead:

public class RaycastBarrel : Barrel
{
    public override void Shoot()
    {
        base.Shoot();
        // Perform raycast logic here
    }
}

See Also