โž•Attachment System

In this section you will find more about attachments.

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:

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:

BaseAttachment.cs
public class BaseAttachment : MonoBehaviour
{
    public List<FPSAnimatorLayerSettings> attachmentLayerSettings;
}

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:

ScopeAttachment.cs
public class ScopeAttachment : BaseAttachment
{
    [Min(0f)] public float aimFovZoom = 1f;
    [Range(0f, 2f)] public float sensitivityMultiplier = 1f;
    public Transform aimPoint;
}

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.

AttachmentGroup.cs
public class AttachmentGroup<T> where T : BaseAttachment

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.

Last updated