# Auto Gun

**Auto Gun** component will manage shooting, ammo, and slide reloading logic. Auto Gun system includes built in logic for [Auto Ammo](#auto-ammo),[ Auto Gun Target](auto-gun/auto-gun-target), [Auto Gun Effects](#auto-gun-effects)

The gun takes a [Place Point](../place-point) called `Magazine Point` that should be configured to take the appropriate grabbables with the Auto Ammo component.&#x20;

The gun does not keep track of its ammo directly it only knows if it has one bullet in the chamber or not, and if it has an [Auto Ammo](#auto-ammo) placed in its `Magazine Point`

If an [Auto Ammo](#auto-ammo) is place in this point you can call the AutoGun.LoadSlide() event from a function or from a Unity Event to load that first bullet into the Auto Gun (in the Demo the rifle's slide uses a [GrabbableHeldJoint ](grabbable-held-joint)component which calls the LoadSlide() event from it's Unity Event)

Shooting the gun will automatically reload the slide until there is no more ammo in the [Auto Ammo](#auto-ammo) or if the clip has been removed

<div align="left"><figure><img src="https://2959669391-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5zKO0EvOjzUDeT2aiFk3%2Fuploads%2FbztvdL9j8UBgkqbeQv62%2Fimage.png?alt=media&#x26;token=12aecbd5-af3f-4acb-8ab3-d278a71289f1" alt=""><figcaption></figcaption></figure></div>

`Shoot Forward` Determines the origin and direction for bullet raycasting, essential for calculating bullet paths.

`Slide Joint` (Optional) [Grabbable Held Joint](grabbable-held-joint) is recommended for triggering the LoadSlide() event, if connected this will trigger the slide movement when shooting. (Check the events on the rifle slide in the demo for example)

`Magazine Point` The place point for the ammo, this [Place Point](../place-point) should only take objects this gun considers ammo. Ammo grabbables should always include the `Auto Ammo` component



`Automatic Fire Rate` how quickly the gun will repeat fire, in seconds, while holding down the trigger. Set to 0 for semi-automatic

`Hit Force` The physics force applied to the target's rigidbody when hit

`Kick Back Force` The physics force applied to the gun when shooting

`Use Bullet Penetration` If true the gun will hit every target until it hits a static collider

`Require Slide Loading` whether or not the LoadSlide() function needs to be called externally or if it is automatically called when the ammo is loaded

`Auto Eject Empty Clip` Whether or not the ammo clip should automatically be removed from the place point when it's empty



### Gun Events

<div align="left"><figure><img src="https://2959669391-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5zKO0EvOjzUDeT2aiFk3%2Fuploads%2FA2HlDmiLDKLa8ZWPIWtV%2Fimage.png?alt=media&#x26;token=5611ef65-6fea-434c-87ac-5029927b11ef" alt=""><figcaption></figcaption></figure></div>

`On Shoot` called when the gun is successfully shot, using an ammo

`On Empty Shoot` called when the shoot event is called but there is no ammo

`On Hit` [called ](#user-content-fn-1)[^1]when a target is hit

`On Ammo Place` called when a new ammo is placed in the ammo place point&#x20;

`On Ammo Remove` called when a new ammo is removed from the ammo place point&#x20;

`On Slide Load` called when the LoadSlide() or FireLoadSlide() function is called. Includes an enum to distiguish between a hand load event and a fire load event



## Auto Ammo

This component should be attached to any object being used in the Gun's attached `Magazine Point` Place Point. Without this component the placed object won't be considered valid ammo

<div align="left"><figure><img src="https://2959669391-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5zKO0EvOjzUDeT2aiFk3%2Fuploads%2FEMYPE3cNMS1KPtXkRSUw%2Fimage.png?alt=media&#x26;token=13349794-9f6e-4dad-95e0-9a1ec0be44f6" alt=""><figcaption></figcaption></figure></div>

`Current Ammo` The current amount of ammo left in this clip

`Ammo Text` Updates to show the ammo count whenever it changes



## Auto Gun Effects

This is an additional component that can be added onto the Auto Gun to add additional visual/audio details without coding a new script. This script is not required and does not change how the core gun works

<div align="left"><figure><img src="https://2959669391-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5zKO0EvOjzUDeT2aiFk3%2Fuploads%2Fdjswgr7C5PUZuj5Cawfw%2Fimage.png?alt=media&#x26;token=2dc38e00-d08e-4bd7-bb94-7b9757b20a3c" alt=""><figcaption></figcaption></figure></div>

`Eject Unfired Bullet` Whether or not racking the slide with a bullet already in the chamber ejects that bullet or not

`Bullet` This is the unfired bullet prefab that will be instantiated and ejected if the 'ejectUnfiredBullet' value is true and the slide is racked while full

`Bullet Shell` This is the fired bullet shell prefab that will be instantiated and ejected when the gun is shot

`Shoot Particle` This is the fired bullet shell prefab that will be instantiated and ejected if the when the gun is shot

`Ejected Bullet Lifetime` The lifetime of the ejected bullets before they are added to the pool

`Shell Ejection Spawn Point` The position and rotation where the bullets are instantiated

`Shell Ejection Direction` The forward direction of this point represents the direction the shells should be ejected

`Shell Ejection Force` The amount of force to add to the ejected bullet

`Shoot Sound` This sound will play when a bullet is fired

`Empty Shoot Sound` This sound will play when a the trigger is pressed and there is nothing to shoot

[^1]: 
