# Weapons and Items

## Overview

Weapons play a very important role in the demo project, as they play reloading animations, handle the firing and attachments at the same time.

All weapons and items are derived from the **FPSItem** class - it contains a general interface for all entities, and its methods are used by the **FPSController** to start an action. For example:

{% code title="FPSController.cs" %}

```csharp
public void OnCycleScope()
{
    if (!IsAiming()) return;
            
    GetActiveItem().OnCycleScope();
    PlayTransitionMotion(settings.aimingMotion);
}
```

{% endcode %}

In this snippet, when we receive an *OnCycleScope* event from the **Unity Input System**, we effectively call the general *OnCycleScope* method on our currently equipped item. The implementation might differ, depending on the actual item the character is holding.

You can leverage this behavior to implement custom functions for each character action.

## Weapon Spawn

All weapons and items are spawned when the game starts, it happens in the **FPSController**:

{% code title="FPSController.cs" overflow="wrap" %}

```csharp
foreach (var prefab in settings.weaponPrefabs)
{
    var weapon = Instantiate(prefab, transform.position, Quaternion.identity);

    var weaponTransform = weapon.transform;

    weaponTransform.parent = _weaponBone;
    weaponTransform.localPosition = Vector3.zero;
    weaponTransform.localRotation = Quaternion.identity;

    _instantiatedWeapons.Add(weapon.GetComponent<FPSItem>());
    weapon.gameObject.SetActive(false);
}
```

{% endcode %}

{% hint style="danger" %}
**Note:** make sure **ALL** items and weapons are parented to the IK WeaponBone game object!
{% endhint %}

## Weapon Change

When changing weapons, the system first unequips anactive item:

{% code title="FPSController.cs" overflow="wrap" %}

```csharp
private void UnequipWeapon()
{
    DisableAim();
    _actionState = FPSActionState.WeaponChange;
    GetActiveItem().OnUnEquip();
}
```

{% endcode %}

The actual logic is defined in the weapon class, so you have more flexibility here. We also need to disable aiming or any other kind of action.

This will effcetively play an unequip animation, which is procedurally applied using the **Animator Controller**:

{% code title="Weapon.cs" overflow="wrap" %}

```csharp
public override void OnUnEquip()
{
    _controllerAnimator.CrossFade(CurveUnequip, 0.15f);
}
```

{% endcode %}

As you can see the unequip callback is simply playing a **CurveUnequip** motion. This motion resides in the IK layer of the demo animator:

<figure><img src="https://784345943-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxcUmJ78NSw1bSUlSO9oP%2Fuploads%2F5EMbhLPo2cczzq2FlxxY%2Fimage.png?alt=media&#x26;token=78454e8c-cc4d-4a4f-80e4-ee92aa47d823" alt="" width="247"><figcaption><p>Procedural animations.</p></figcaption></figure>

To equip the next gun, the system invokes the **EquipWeapon** method using the **equipDelay** property of the [#controller](https://kinemation.gitbook.io/scriptable-animation-system/controller#controller "mention") settings:

{% code title="FPSController.cs" overflow="wrap" %}

```csharp
Invoke(nameof(EquipWeapon), settings.equipDelay);
```

{% endcode %}

This action will be followed by the **OnEquip** callback of the new weapon:

{% code title="FPSController.cs" overflow="wrap" %}

```csharp
private void EquipWeapon()
{
    if (_instantiatedWeapons.Count == 0) return;

    _instantiatedWeapons[_previousWeaponIndex].gameObject.SetActive(false);
    GetActiveItem().gameObject.SetActive(true);
    GetActiveItem().OnEquip(gameObject);

    _actionState = FPSActionState.None;
}
```

{% endcode %}

## Weapon Properties

<figure><img src="https://784345943-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxcUmJ78NSw1bSUlSO9oP%2Fuploads%2FyXaAWV7xGRD7nW05a2Cd%2Fimage.png?alt=media&#x26;token=b24978a9-e508-4fa2-8687-a8487dee4fd2" alt="" width="436"><figcaption><p>Weapon settings.</p></figcaption></figure>

* **Field Of View**: deifnes the default FOV for this weapon. This value can be scaled by scope attachments, depending on the zoom value.
* **Reload Clip**: reloading animation to play.
* **Camera Reload Animation**: camera reloading animation to play.
* **Grenade Clip**: grenade animation to play.
* **Camera Grenade Animation**: camera grenade motion to play.
* **Overlay Type**: the type of the weapon.
* **Recoil Data**: recoil animation asset settings.
* **Recoil Pattern Settings**: general recoil system settings.
* **Camera Shake**: camera shake asset, played when a shot is fired.
* **Fire Rate**: weapon RPM.
* **Burst Length**: the length of the burst fire.
* **Barrel Attachments**: defines barrel attachments, e.g. suppressors, muzzle brakes, etc.
* **Grip Attachments**: defines grip attachment.
* **Scope Groups**: defines scope attachment groups.

Now it is time to find out more about the attachment system.
