---
description: In this section you will find more about attachments.
---

# Attachment System

## Overview

The demo project provides a simple attachment system, which allows you to change them in realtime. Make sure to press the **"I"** key, and the attachment editing mode will be activated.

At the current state it is possible to change 3 types of attachments:

* Barrel-type attachments
* Grips
* Scopes

To cycle the attachment type make sure to press 1, 2 and 3 respectively.

## Attachment Prefabs

All attachments are located in the weapon prefab:

<figure><img src="https://784345943-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxcUmJ78NSw1bSUlSO9oP%2Fuploads%2FeQZwp7dVQmi7lOBUpFR4%2Fimage.png?alt=media&#x26;token=b1d4cfa6-a236-470c-8dd3-a9fe888261d3" alt="" width="482"><figcaption><p>AK12 attachments example.</p></figcaption></figure>

**Barrel** and **Grip Attachments** properties contain a single group of attachments of the same type. You can cycle attachments by pressing 1, 2 or 3 keys. For example, if you press 1, the next grip attachment will be equipped.

Scopes, however, are different. Instead of containing a single attachment group, they contain a list of attachment groups. That is because scopes usually have more sockets on the weapon, than grips and barrel attachments.

Make sure to add scopes with different sockets as separate groups, otherwise you will not be able to change sights in game.

## BaseAttachment, ScopeAttachment

All attachments are derived from the **BaseAttachment** class:

{% code title="BaseAttachment.cs" overflow="wrap" %}
```csharp
public class BaseAttachment : MonoBehaviour
{
    public List<FPSAnimatorLayerSettings> attachmentLayerSettings;
}
```
{% endcode %}

**attachmentLayerSettings** deifne a list of animation features, which we want to link when the attachment is equipped.

**BaseAttachment** is used for barrel and grip attachments, as these typically do not require that much logic.

Scopes have their own attachment type:

{% code title="ScopeAttachment.cs" overflow="wrap" %}
```csharp
public class ScopeAttachment : BaseAttachment
{
    [Min(0f)] public float aimFovZoom = 1f;
    [Range(0f, 2f)] public float sensitivityMultiplier = 1f;
    public Transform aimPoint;
}
```
{% endcode %}

**aimFovZoom** will be multiplied by the default FOV when aiming.

**sensitivityMultiplier** will scale the player input when aiming.

**aimPoint** defines the active aim point socket.

## AttachmentGroup

**AttachmentGroup** represents a set of attachments, usually of the same type. This class is used to initialize, cycle and get the active attachment.

{% code title="AttachmentGroup.cs" overflow="wrap" %}
```csharp
public class AttachmentGroup<T> where T : BaseAttachment
```
{% endcode %}

This class is generic, meaning that you can use any BaseAttachment-derived type. This is a good foundation for custom attachment types.

## How to add attachments

The process is quite straightforward:

1. Add the attachment prefab to the weapon.
2. Add the BaseAttachment or ScopeAttachment component to it.
3. Then, based on the attachment type, add your attachment to the list.
