2DShooterWeaponSystem  1.9.0
WeaponSystem Class Reference

The meat of the asset, the WeaponSystem component is used to hold weapon configurations, and bullet pattern setups as well as handling the logic for firing the bullets and setting bullet properties up based on the configuration. More...

Inheritance diagram for WeaponSystem:

Public Types

enum  BulletOption {
  BulletOption.Spherical, BulletOption.TracerHorizontal, BulletOption.TurretHorizontal, BulletOption.Beam1,
  BulletOption.Beam2, BulletOption.Beam3, BulletOption.Beam4, BulletOption.Beam5,
  BulletOption.Beam6
}
 The different bullet options available. More...
 
enum  ShooterType {
  ShooterType.Horizonal, ShooterType.Vertical, ShooterType.FreeAim, ShooterType.TargetTracking,
  ShooterType.HorizontalFaceLeft, ShooterType.VerticalFaceDown
}
 Allows the user to adjust the direction which the bullets/weapons fire. More...
 
enum  BulletPresetType {
  BulletPresetType.CrazySpreadPingPong, BulletPresetType.GatlingGun, BulletPresetType.Simple, BulletPresetType.Shotgun,
  BulletPresetType.WildFire, BulletPresetType.ThreeShotSpread, BulletPresetType.DualSpread, BulletPresetType.ImbaCannon,
  BulletPresetType.Shower, BulletPresetType.DualAlternating, BulletPresetType.DualMachineGun, BulletPresetType.Tarantula,
  BulletPresetType.CircleSpray
}
 Default weapon presets. More...
 

Public Member Functions

delegate void BulletPresetChangeHandler ()
 
delegate void WeaponConfigurationChangedHandler (Transform gunPointTransform, int weaponConfigIndex)
 
delegate void ReloadStartedHandler ()
 
delegate void ReloadFinishedHandler ()
 
delegate void WeaponShootingStartedHandler ()
 
delegate void WeaponShootingFinishedHandler ()
 
void EquipWeaponConfiguration (int slot)
 From version 1.3 onward, you can setup weapon "configurations" which are stored in the weaponConfigs List property. More...
 
IEnumerator LoadMagazine (bool instantReload, bool useReloadSfx)
 Reload magazine coroutine - used for reloading weapon configurations from remaining ammo pool. More...
 
IEnumerator LoadMagazine (bool instantReload, bool useReloadSfx, int amountToReloadWith)
 Reload magazine coroutine - used for reloading weapon configurations with a certain number of bullets instead of a full clip. More...
 

Public Attributes

string weaponName
 The name assigned to the weapon configuration that is currently equipped. More...
 
float bulletSpread
 The spread that bullets will use for their pattern (when more than one bullet) More...
 
float bulletSpreadPingPongMax
 The maximum spread to bounce up to when ping pong spread is enabled. More...
 
float bulletSpreadPingPongMin
 The minimum spread to bounce down to when ping pong spread is enabled. More...
 
float spreadPingPongSpeed = 1f
 The speed at which the bullet spread is bounced between. More...
 
float bulletSpacing
 The spacing between bullets when using more than one bullet. More...
 
int bulletCount
 The number of bullets to fire. More...
 
float bulletRandomness
 An amount of randomness to add to bullets when firing them (adds jitter to bullet streams / patterns). More...
 
float bulletSpeed
 Speed at which bullets travel More...
 
float weaponFireRate
 
float weaponXOffset
 Offset on the X to position the start of a bullet when fired from a 'gunPoint' transform. More...
 
float weaponYOffset
 Offset on the Y to position the start of a bullet when fired from a 'gunPoint' transform. More...
 
float ricochetChancePercent
 The chance a bullet fired from this weapon will have to bounce / ricochet off other colliders. More...
 
float magazineChangeDelay
 The time it takes to reload / change a magazine. More...
 
int magazineSize
 The number of bullets in a magazine. More...
 
int startingAmmo
 The starting ammo for a weapon configuration. More...
 
int ammoUsed
 Amount of ammo used in a weapon configuration thus far. More...
 
int magazineRemainingBullets
 Remaining bullets in the weapon configuration's magazine. More...
 
Transform targetToTrack
 The target for the weapon to track in 'Turret Track' mode. More...
 
float trackingTurnRate
 The rate at which a weapon in 'Turret Track' mode will turn to face it's designated target. More...
 
bool lerpTurnRate
 If enabled, then the turn rate of a weapon system in 'Turret Track' mode whilst tracking its assigned target will be eased (instead of the weapon always instantly facing it's target). More...
 
Color bulletColour = Color.white
 If you are using plain white sprites this color value can be used to dynamically change bullet color in-game. More...
 
Texture2D weaponIcon
 The texture2D assigned to a weapon configuration - can be used for GUI display of selected weapon for example. More...
 
bool autoFire
 Enable this to automatically fire without need for input (useful for enemies etc) More...
 
bool pingPongSpread
 Enable to bounce the spread value of bullets between the pingpong spread min and max values (causes bullet pattern to 'swing' up and down). More...
 
bool circularFireMode
 When enabled, this will rotate the gun point transform around 360 degrees at a constant rate, allowing you to form circular bullet patterns. More...
 
float circularFireRotationRate = 240f
 The rotations per minute that the gunPoint transform will rotate with, if circularFireMode is enabled. More...
 
bool richochetsEnabled
 Enable if bullets fired from the weapon should allow ricochets (based on percent chance for bullets to ricochet). More...
 
bool hitEffectEnabled
 Enable if bullets from this weapon should spawn hit effects (like sparks for example) when impacting colliders. More...
 
bool limitedAmmo
 Enabled if the weapon configuration has limited ammo. More...
 
bool usesMagazines
 Enabled if the weapon is configured to have ammo and use magazines. More...
 
bool isFirstEquip
 Used to track if a weapon configuration is the first equip or not. More...
 
bool playReloadingSfx
 Allow the playback of the reloading SFX when reloading. More...
 
bool playEmptySfx
 If enabled, then the empty SFX is played when attempting to shoot with an empty clip. More...
 
bool weaponRelativeToComponent
 If enabled, the aiming/facing direction is calculated relative to the transform that the WeaponSystem component is placed on. More...
 
bool mirrorX
 Duplicates the number of bullets being used and mirrors the extra bullets on the X-axis More...
 
bool bulletCountsAsAmmo
 If enabled, then every single bullet coming out will count toward ammo usage if limitedAmmo is enabled. More...
 
float aimAngle
 The aim angle the weapon system is currently at in radians. More...
 
AudioClip reloadSfxClip
 The reload SFX clip for the selected weapon configuration More...
 
AudioClip emptySfxClip
 The empty magazine SFX clip for the selected weapon configuration More...
 
AudioClip shotFiredClip
 The shot fired SFX clip for the selected weapon configuration More...
 
Transform gunPoint
 The gun point or transform location that bullets are shot / generated from when shooting. More...
 
BulletOption bulletOptionType
 
ShooterType shooterDirectionType
 

Properties

BulletPresetType BulletPreset [get, set]
 (Legacy - pre version 1.3, but still usable) This property dictates what weapon is selected for the WeaponSystem. More...
 
List< WeaponSystemConfigurationweaponConfigs [get, set]
 The list of weapon configurations on this Weapon System. More...
 

Events

BulletPresetChangeHandler BulletPresetChanged
 Event that fires when a bullet preset is changed. More...
 
WeaponConfigurationChangedHandler WeaponConfigurationChanged
 Event that fires when a Weapon Configuration is changed (weapon changed). More...
 
ReloadStartedHandler ReloadStarted
 Event that fires when reloading starts. More...
 
ReloadFinishedHandler ReloadFinished
 Event that fires when reloading finishes. More...
 
WeaponShootingStartedHandler WeaponShootingStarted
 Event that fires when shooting starts. More...
 
WeaponShootingFinishedHandler WeaponShootingFinished
 Event that fires when shooting stops. More...
 

Detailed Description

The meat of the asset, the WeaponSystem component is used to hold weapon configurations, and bullet pattern setups as well as handling the logic for firing the bullets and setting bullet properties up based on the configuration.

Member Enumeration Documentation

◆ BulletOption

The different bullet options available.

This will be used to select the bullet type pulled from the Object Pool Manager. You can change the bullets here by adding different or custom bullet prefabs to the ObjectPoolManager instance in your scene that map to each of these enum options.

Enumerator
Spherical 
TracerHorizontal 
TurretHorizontal 
Beam1 
Beam2 
Beam3 
Beam4 
Beam5 
Beam6 

◆ BulletPresetType

Default weapon presets.

Feel free to add new ones here! Just don't forget to handle the new bullet presets in the BulletPresetChangedHandler method that is fired with the BulletPresetChanged event.

Enumerator
CrazySpreadPingPong 
GatlingGun 
Simple 
Shotgun 
WildFire 
ThreeShotSpread 
DualSpread 
ImbaCannon 
Shower 
DualAlternating 
DualMachineGun 
Tarantula 
CircleSpray 

◆ ShooterType

Allows the user to adjust the direction which the bullets/weapons fire.

This makes it easy to create top down horizontal or vertical games.

Enumerator
Horizonal 
Vertical 
FreeAim 
TargetTracking 
HorizontalFaceLeft 
VerticalFaceDown 

Member Function Documentation

◆ BulletPresetChangeHandler()

delegate void WeaponSystem.BulletPresetChangeHandler ( )

◆ EquipWeaponConfiguration()

void WeaponSystem.EquipWeaponConfiguration ( int  slot)

From version 1.3 onward, you can setup weapon "configurations" which are stored in the weaponConfigs List property.

You should use this EquipWeaponConfiguration method to change weapons, passing in the index of the list for the weapon you want to select. Use the in-editor inspector on the WeaponSystem component to setup new configurations and add them to the inventory/configurations list.

Parameters
slotThe weapon configuration 'slot' index to equip.

◆ LoadMagazine() [1/2]

IEnumerator WeaponSystem.LoadMagazine ( bool  instantReload,
bool  useReloadSfx 
)

Reload magazine coroutine - used for reloading weapon configurations from remaining ammo pool.

Parameters
instantReload
useReloadSfx
Returns

◆ LoadMagazine() [2/2]

IEnumerator WeaponSystem.LoadMagazine ( bool  instantReload,
bool  useReloadSfx,
int  amountToReloadWith 
)

Reload magazine coroutine - used for reloading weapon configurations with a certain number of bullets instead of a full clip.

This might be useful if you wish to have the player reload one bullet at a time for example.

Parameters
instantReload
useReloadSfx
amountToReloadWith
Returns

◆ ReloadFinishedHandler()

delegate void WeaponSystem.ReloadFinishedHandler ( )

◆ ReloadStartedHandler()

delegate void WeaponSystem.ReloadStartedHandler ( )

◆ WeaponConfigurationChangedHandler()

delegate void WeaponSystem.WeaponConfigurationChangedHandler ( Transform  gunPointTransform,
int  weaponConfigIndex 
)

◆ WeaponShootingFinishedHandler()

delegate void WeaponSystem.WeaponShootingFinishedHandler ( )

◆ WeaponShootingStartedHandler()

delegate void WeaponSystem.WeaponShootingStartedHandler ( )

Member Data Documentation

◆ aimAngle

float WeaponSystem.aimAngle

The aim angle the weapon system is currently at in radians.

◆ ammoUsed

int WeaponSystem.ammoUsed

Amount of ammo used in a weapon configuration thus far.

◆ autoFire

bool WeaponSystem.autoFire

Enable this to automatically fire without need for input (useful for enemies etc)

◆ bulletColour

Color WeaponSystem.bulletColour = Color.white

If you are using plain white sprites this color value can be used to dynamically change bullet color in-game.

◆ bulletCount

int WeaponSystem.bulletCount

The number of bullets to fire.

◆ bulletCountsAsAmmo

bool WeaponSystem.bulletCountsAsAmmo

If enabled, then every single bullet coming out will count toward ammo usage if limitedAmmo is enabled.

(Disable this for shotguns for example so that 10 bullets come out as spray, but only count as one 'bullet' used for ammo).

◆ bulletOptionType

BulletOption WeaponSystem.bulletOptionType

◆ bulletRandomness

float WeaponSystem.bulletRandomness

An amount of randomness to add to bullets when firing them (adds jitter to bullet streams / patterns).

◆ bulletSpacing

float WeaponSystem.bulletSpacing

The spacing between bullets when using more than one bullet.

◆ bulletSpeed

float WeaponSystem.bulletSpeed

Speed at which bullets travel

◆ bulletSpread

float WeaponSystem.bulletSpread

The spread that bullets will use for their pattern (when more than one bullet)

◆ bulletSpreadPingPongMax

float WeaponSystem.bulletSpreadPingPongMax

The maximum spread to bounce up to when ping pong spread is enabled.

◆ bulletSpreadPingPongMin

float WeaponSystem.bulletSpreadPingPongMin

The minimum spread to bounce down to when ping pong spread is enabled.

◆ circularFireMode

bool WeaponSystem.circularFireMode

When enabled, this will rotate the gun point transform around 360 degrees at a constant rate, allowing you to form circular bullet patterns.

Requires weapon system to be set to GunPoint relative mode.

◆ circularFireRotationRate

float WeaponSystem.circularFireRotationRate = 240f

The rotations per minute that the gunPoint transform will rotate with, if circularFireMode is enabled.

◆ emptySfxClip

AudioClip WeaponSystem.emptySfxClip

The empty magazine SFX clip for the selected weapon configuration

◆ gunPoint

Transform WeaponSystem.gunPoint

The gun point or transform location that bullets are shot / generated from when shooting.

This is used if the WeaponSystem configuration is set to relative to gun point mode.

◆ hitEffectEnabled

bool WeaponSystem.hitEffectEnabled

Enable if bullets from this weapon should spawn hit effects (like sparks for example) when impacting colliders.

◆ isFirstEquip

bool WeaponSystem.isFirstEquip

Used to track if a weapon configuration is the first equip or not.

◆ lerpTurnRate

bool WeaponSystem.lerpTurnRate

If enabled, then the turn rate of a weapon system in 'Turret Track' mode whilst tracking its assigned target will be eased (instead of the weapon always instantly facing it's target).

◆ limitedAmmo

bool WeaponSystem.limitedAmmo

Enabled if the weapon configuration has limited ammo.

◆ magazineChangeDelay

float WeaponSystem.magazineChangeDelay

The time it takes to reload / change a magazine.

◆ magazineRemainingBullets

int WeaponSystem.magazineRemainingBullets

Remaining bullets in the weapon configuration's magazine.

◆ magazineSize

int WeaponSystem.magazineSize

The number of bullets in a magazine.

◆ mirrorX

bool WeaponSystem.mirrorX

Duplicates the number of bullets being used and mirrors the extra bullets on the X-axis

◆ pingPongSpread

bool WeaponSystem.pingPongSpread

Enable to bounce the spread value of bullets between the pingpong spread min and max values (causes bullet pattern to 'swing' up and down).

◆ playEmptySfx

bool WeaponSystem.playEmptySfx

If enabled, then the empty SFX is played when attempting to shoot with an empty clip.

◆ playReloadingSfx

bool WeaponSystem.playReloadingSfx

Allow the playback of the reloading SFX when reloading.

◆ reloadSfxClip

AudioClip WeaponSystem.reloadSfxClip

The reload SFX clip for the selected weapon configuration

◆ richochetsEnabled

bool WeaponSystem.richochetsEnabled

Enable if bullets fired from the weapon should allow ricochets (based on percent chance for bullets to ricochet).

◆ ricochetChancePercent

float WeaponSystem.ricochetChancePercent

The chance a bullet fired from this weapon will have to bounce / ricochet off other colliders.

◆ shooterDirectionType

ShooterType WeaponSystem.shooterDirectionType

◆ shotFiredClip

AudioClip WeaponSystem.shotFiredClip

The shot fired SFX clip for the selected weapon configuration

◆ spreadPingPongSpeed

float WeaponSystem.spreadPingPongSpeed = 1f

The speed at which the bullet spread is bounced between.

◆ startingAmmo

int WeaponSystem.startingAmmo

The starting ammo for a weapon configuration.

◆ targetToTrack

Transform WeaponSystem.targetToTrack

The target for the weapon to track in 'Turret Track' mode.

◆ trackingTurnRate

float WeaponSystem.trackingTurnRate

The rate at which a weapon in 'Turret Track' mode will turn to face it's designated target.

◆ usesMagazines

bool WeaponSystem.usesMagazines

Enabled if the weapon is configured to have ammo and use magazines.

◆ weaponFireRate

float WeaponSystem.weaponFireRate

◆ weaponIcon

Texture2D WeaponSystem.weaponIcon

The texture2D assigned to a weapon configuration - can be used for GUI display of selected weapon for example.

◆ weaponName

string WeaponSystem.weaponName

The name assigned to the weapon configuration that is currently equipped.

◆ weaponRelativeToComponent

bool WeaponSystem.weaponRelativeToComponent

If enabled, the aiming/facing direction is calculated relative to the transform that the WeaponSystem component is placed on.

Offset X and Y positioning of bullets is taken into account and applied in this mode.

◆ weaponXOffset

float WeaponSystem.weaponXOffset

Offset on the X to position the start of a bullet when fired from a 'gunPoint' transform.

◆ weaponYOffset

float WeaponSystem.weaponYOffset

Offset on the Y to position the start of a bullet when fired from a 'gunPoint' transform.

Property Documentation

◆ BulletPreset

BulletPresetType WeaponSystem.BulletPreset
getset

(Legacy - pre version 1.3, but still usable) This property dictates what weapon is selected for the WeaponSystem.

Set it to any BulletPresetType Enum value and the event should take care of setting up the weapon for you. Just ensure that if you add a new Enum value to BulletPresetType, that you create an entry for it in the switch statement in the BulletPresetChangedHandler. All you need is a reference to your WeaponSystem script, once you have that, you can change you weapon selection from anywhere in your game using this.

◆ weaponConfigs

List<WeaponSystemConfiguration> WeaponSystem.weaponConfigs
getset

The list of weapon configurations on this Weapon System.

Event Documentation

◆ BulletPresetChanged

BulletPresetChangeHandler WeaponSystem.BulletPresetChanged

Event that fires when a bullet preset is changed.

◆ ReloadFinished

ReloadFinishedHandler WeaponSystem.ReloadFinished

Event that fires when reloading finishes.

◆ ReloadStarted

ReloadStartedHandler WeaponSystem.ReloadStarted

Event that fires when reloading starts.

◆ WeaponConfigurationChanged

WeaponConfigurationChangedHandler WeaponSystem.WeaponConfigurationChanged

Event that fires when a Weapon Configuration is changed (weapon changed).

◆ WeaponShootingFinished

WeaponShootingFinishedHandler WeaponSystem.WeaponShootingFinished

Event that fires when shooting stops.

◆ WeaponShootingStarted

WeaponShootingStartedHandler WeaponSystem.WeaponShootingStarted

Event that fires when shooting starts.


The documentation for this class was generated from the following file: