🔫Weapons and Items

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

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:

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

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:

FPSController.cs
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);
}

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:

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

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:

Weapon.cs
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:

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

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

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

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

    _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