๐Ÿ”ซWeapons and Items

In this section you are going to learn more about the weapon structure.


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:

public void OnCycleScope()
    if (!IsAiming()) return;

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:

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;


Note: make sure ALL items and weapons are parented to the IK WeaponBone game object!

Weapon Change

When changing weapons, the system first unequips anactive item:

private void UnequipWeapon()
    _actionState = FPSActionState.WeaponChange;

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:

public override void OnUnEquip()
    _controllerAnimator.CrossFade(CurveUnequip, 0.15f);

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

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

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

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


    _actionState = FPSActionState.None;

Weapon Properties

  • 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.

Last updated