This document is about: QUANTUM 2
SWITCH TO

Weapons

Overview

The WeaponsSystem provides a way to fire projectiles or repeat a firing actions at a specific rate.

Weapon Setup

Every weapon is its own entity with a Weapons component and a specialized controller inheriting from the base WeaponController. This setup allows to add and remove weapons from player entities at runtime.

weapon setup example
Projectile Weapon Prototype Example.

WeaponController

All weapon controllers inherit from the WeaponController class and thus will have this properties available to them:

  • FireOffset: Relative offset from hand to weapon barrel / point where the projectile spawns
  • MinRange: Minimum distance from target to start shooting automatically
  • MaxRange: Maximum distance from target to start shooting automatically
  • CriticalChance: Percentage chance to double damage
  • MinRangeAutoFireAngle: Maximum angle from player aim direction to start shooting automatically when distance from the target equals MinRange in degrees
  • MaxRangeAutoFireAngle: Maximum angle from player aim direction to start shooting automatically when distance from target equals MaxRange in degrees
  • AutoFireAngleBendFactor: Controls interpolation linearity of the AutoFire angle between MinRangeAutoFireAngle and MaxRangeAutoFireAngle

Execution Flow

The WeaponsSystem orchestrates the coordination of the WeaponsDesires, the Weapons component and the various WeaponControllers in the game. The following diagram presents the execution flow and the APIs used to interface between the different moving parts.

weapons
Weapons Ochestration.

AutoFire

In the following diagram the relationship between the WeaponController properties and AutoFire is illustrated from a simplified top-down perspective.

autofire
WeaponController Properties and AutoFire Relationship.

The AutoFire behaviour between MinRange and MaxRange is controlled by the AutoFireAngleBendFactor property. The higher the bend factor, the faster the AutoFire angle interpolates from MinRangeAutoFireAngle towards MaxRangeAutoFireAngle; in other words the AutoFire area is shrinking.

autofire angle bend factor
AutoFire Angle Bend Factor.

Included Controllers

The FPS Template implements the WeaponController in two concrete examples:

  • the ProjectileWeaponController; and,
  • the MeleeWeaponController.

ProjectileWeapon

The ProjectileWeaponController can be used for any weapon shooting a projectile. The behaviour is dependent on how the properties are tweaked and which projectile is referenced in the Projectile property.

  • Projectile: Reference to projectile prototype
  • ProjectileCount: Number of projectiles fired on single weapon use
  • MagazineAmmo: Number of projectiles in weapon magazine
  • WeaponAmmo: Total number of projectiles the weapon can hold
  • UnlimitedAmmo: Set to true for unlimited number of projectiles
  • Cadence: Number of projectiles fired per minute
  • ReloadDuration: How long it takes to reload the weapon
  • DispersionAngle: Per-axis fire direction dispersion in degrees

MeleeWeapon

The MeleeWeaponController can be used for any melee type weapon. In addition to simple melee damage, additional effects can be applied by using the Effect property.

  • Effect: Reference to melee effect prototype
  • AttackDelay: How long it takes before spawning the Effect
  • Cooldown: How long it takes to execute next attack

Creating a New Weapon

This is the step-by-step procedure to create a new Weapon in the FPS Template.

  1. In the Quantum solution, create a new MyWeaponController which inherits from WeaponController.
  2. Add serializable fields to the controller (configuration).
  3. If needed, create a MyWeapon.qtn which contains the definition for the MyWeapon component and other data structures such as roll-backable data.
  4. Override the base properties if needed and implement the Initialize(), Deinitialize(), Update() and Fire() methods in the controller.
  5. Implement the available controller interfaces (e.g. IAttributesProvider, ...).
  6. Recompile the quantum.code solution.
  7. In Unity, create an empty GameObject and add the visual components / model as a child object.
  8. Add the Entity, EntityPrototype, EntityComponentController and EntityComponentWeapon scripts to the GameObject.
  9. On the Entity component, set:
    • Transform Synchronization to None if the weapon is always snapped to the handle
    • Require Owner to true if the weapon cannot exist without the owning entity
  10. On the EntityComponentController component, select MyWeaponController from the WeaponController drop-down menu and fill out the defined properties.
  11. On the EntityComponentWeapon fill out the defined properties.
  12. Add other entity components to define the behavior of the entity (e.g. EntityComponentMyWeapon, ...).
  13. Create a prefab of the entity (Optional).
  14. Execute the asset resource generation via the Quantum > Generate Asset Resources menu.
  15. Execute the prefab baking procedure via the Quantum > Bake > Prefabs menu.
  16. The weapon is now ready to use - reference entity prototype.
Back to top