The Shootable Weapon Item Action allows the character to attack using a ranged object such as an assault rifle or a bow and arrow. The Shootable Weapon can attack using a raycast (hitscan) or a projectile. The weapon can also reload when the object no longer has any ammo. The First/Third Person Shootable Weapon Properties component will also be attached to the weapon and it contains properties that are specific for that perspective.

Firing Troubleshooting

If you are unable to fire a new Shootable Weapon ensure you have checked the following:

  • The inventory has ammo. An easy way to ensure this is true is to add your Consumable Item Type to the Default Loadout of the inventory.
  • The use event is being fired. The weapon won’t try to be fired until the used event is triggered and this can be specified with the Use Event Animation Event Trigger on the shootable weapon.
  • The weapon is facing in the fire direction. The weapon won’t fire until after it is facing in the direction that it should be fired. This sensitivity can be adjusted within the Look Sensitivity field of the First/Third Person Shootable Weapon Properties component. If the sensitivity is set to -1 then the weapon will always fire no matter which direction the weapon is facing.

Muzzle Flash

When the weapon fires a muzzle flash can be shown. This muzzle flash uses a mesh that will fade with time. A light can also be attached which will also reduce intensity as time elapses. A muzzle flash can be created with the Object Manager:

  1. Open the Object Manager and create a new object of type Muzzle Flash.
  2. Assign the new object to the Shootable Weapon’s Muzzle Flash field.
  3. When a Shootable Weapon is created the “Muzzle Flash” GameObject will be created by default on the perspective’s visible object. This GameObject should be specified for the Muzzle Flash Location field of the First Person and Third Person Shootable Weapon Properties.

Smoke

When the weapon fires a smoke particle system can be spawned from the weapon. This particle system will be disabled when it has completed. Smoke can be created with the Object Manager:

  1. Open the Object Manager and create a new object of type Smoke.
  2. Assign the new object to the Shootable Weapon’s Smoke field.
  3. Create a new GameObject as a child of the perspective’s visible object. This GameObject should be at the same level in the hierarchy as the Fire Point. This new GameObject for the Smoke Location field of the First Person and Third Person Shootable Weapon Properties.

Reloadable Clip

When the weapon is reloaded the clip that is attached to the weapon can be removed during the reload animation. If the Reloadable Clip Attachment Transform is specified within the First/Third Person Shootable Weapon Properties then the clip will be reparented to the object specified when the Reload Detach Event triggers. The clip will then be parented back to the original trnasform when the Reload Attach Event is triggered.

Scope

A scope can optionally be specified which uses a separate camera to show a zoomed in view of the target. This scope can be setup by doing the following:

  1. Add the scope camera to your item. This camera should be added to both the first and third person item under the rig. It should not be added to the item object that contains the Shootable Weapon component under the character/Items parent.
  2. Reference this camera in the Scope Camera field of the First/Third Person Shootable Weapon Properties component.
  3. Decide when you want the camera to be active. By default the camera will be active but you can only it activate it when the character is aiming by toggling the Disable Scope Camera On No Aim field within the Shootable Weapon component.
  4. Create a new Render Texture asset and point the scope camera’s Target Texture field to this new render texture.
  5. Create a new material that uses the render texture. For the assault rifle we created the material below (Albedo specifies the render texture). This material uses a custom overlay shader to display the scope on top of the texture.
  6. Apply the material to the object that you’d like to use as the scope. The scope is now setup.

Full Screen UI

When the sniper rifle aims a full screen UI appears that blocks the game view besides the center portion. The full screen UI can be setup by performing the following steps:

  1. Add the Full Screen UI Monitor component to the UI object that you’d like to enable when the weapon should aim. In the ID field for this component you should specify the unique ID of the FullScreenUI object. In our case we will just be using this UI object for the sniper rifle so for the ID we used the Animator ID of the sniper, a value of 8.
  2. On the sniper rifle’s Item component set the Full Screen UI ID to the same ID value that was used in step 1.
  3. When the Show Full Screen UI toggle is enabled the Full Screen UI Monitor component will activate. This can be set using the state system so when the Aim ability is activated it enables the toggle.
  4. If you are adding a sniper ensure you have added a view type state that changes the camera state (such as offset or field of view) when the sniper aims. In the demo scene this state is called AimSniperRifle.

Impact Callback

When the hitscan or projectile collides with another object the “OnObjectImpact” event will be sent from the built-in Event System. A corresponding Unity event will also be sent. This event allows you to add new functionality when the impact occurs without having to change the class at all. The following example will subscribe to this event from a new component:

using UnityEngine;
using Opsive.UltimateCharacterController.Events;

public class MyObject : MonoBehaviour
{
    /// <summary>
    /// Initialize the default values.
    /// </summary>
    public void Awake()
    {
        EventHandler.RegisterEvent<float, Vector3, Vector3, GameObject, Collider>(gameObject, "OnObjectImpact", OnImpact);
    }

    /// <summary>
    /// The object has been impacted with another object.
    /// </summary>
    /// <param name="amount">The amount of damage taken.</param>
    /// <param name="position">The position of the damage.</param>
    /// <param name="forceDirection">The direction that the object took damage from.</param>
    /// <param name="attacker">The GameObject that did the damage.</param>
    /// <param name="hitCollider">The Collider that was hit.</param>
    private void OnImpact(float amount, Vector3 position, Vector3 forceDirection, GameObject attacker, Collider hitCollider)
    {
        Debug.Log(name + " impacted by " + attacker + " on collider " + hitCollider + ".");
    }

    /// <summary>
    /// The GameObject has been destroyed.
    /// </summary>
    public void OnDestroy()
    {
        EventHandler.UnregisterEvent<float, Vector3, Vector3, GameObject, Collider>(gameObject, "OnObjectImpact", OnImpact);
    }
}

Inspected Fields

Consumable Item Type

The ItemType that is consumed by the item.

Fire Mode

The mode in which the weapon fires multiple shots.

  • SemiAuto: Fire discrete shots, don’t continue to fire until the player fires again.
  • FullAuto: Keep firing until the ammo runs out or the player stops firing.
  • Burst: Keep firing until the burst rate is zero.
Fire Type

Specifies when the weapon should be fired.

  • Instant: Fire the shot immediately.
  • ChargeAndFire: Wait for the Used callback and then fire.
  • ChargeAndHold: Fire as soon as the Use button is released.
Min Charge Length

If using a charge FireType, the minimum amount of time that the weapon must be charged for in order to be fired.

Full Charge Length

If using a charge FireType, the amount of time that the weapon must be charged for in order to be fully fired.”

Charge Item Substate Parameter Value

The Item Substate parameter value when charging.

Min Charge Strength

If using a charge FireType, the minimum amount strength that the weapon will use if it isn’t fully charged.

Charge Audio Clip Set

A set of AudioClips that can be played when the weapon is charging.

Fire Count

The number of rounds to fire in a single shot.

Burst Count

If using the Burst FireMode, specifies the number of bursts the weapon can fire.

Burst Delay

If using the Burst FireMode, specifies the delay before the next burst can occur.

Spread

The random spread of the bullets once they are fired.

Fire In Look Source Direction

Should the weapon fire in the LookSource direction? If false the weapon will be fired in the direction of the weapon.

Projectile

Optionally specify a projectile that the weapon should use. If no projectile is specified then the weapon will fire a raycast in order to determine if it hit any objects.

Projectile Fire Velocity Magnitude

The magnitude of the projectile velocity when fired. The direction is determined by the fire direction.

Projectile Visibility

Specifies when the projectile should become visible.

  • OnFire: The projectile is only visible when the weapon is being fired.
  • OnAim: The projectile is visible when the character is aiming.
  • Always: The projectile is always visible when the item is equipped.
Projectile Start Layer

The layer that the projectile should occupy when initially spawned.

Projectile Fired Layer

The layer that the projectile object should change to after being fired.

Layer Change Delay

The amount of time after the object has been fired to change the layer.

Projectile Enable Delay After Other Use

The amount of time after another item has been used that the projectile should be enabled again.

Dry Fire Item Substate Parameter Value

The Item Substate parameter value when the weapon tries to fire but is out of ammo.

Dry Fire Audio Clip Set

A set of AudioClips that can be played when the weapon is out of ammo.

Hitscan Fire Range

The maximum distance in which the hitscan fire can reach.

Max Hitscan Collision Count

The maximum number of objects the hitscan cast can collide with.

Impact Layers

A LayerMask of the layers that can be hit when fired at.

Damage Amount

The amount of damage to apply to the hit object.

Impact Force

The amount of much force to apply to the hit object.

Impact Force Frames

The number of frames to add the impact force to.

Impact State Name

The name of the state to activate upon impact.

Impact State Disable Timer

The number of seconds until the impact state is disabled. A value of -1 will require the state to be disabled manually.

Surface Impact

The Surface Impact triggered when the weapon hits an object.

Clip Size

The number of rounds in the clip.

Auto Reload

Specifies when the item should be automatically reloaded.

Reload Type

Specifies how the clip should be reloaded.

  • Full: Reload the entire clip.
  • Single: Reload a single bullet.
Reload Can Camera Zoom

Can the camera zoom during a reload?

Reload Crosshairs Spread

Should the crosshairs spread during a recoil?

Reload Animator Audio State Set

Specifies the animator and audio state from a reload.

Reload Event

Specifies if the item should wait for the OnAnimatorItemReload animation event or wait for the specified duration before reloading.

Reload Complete Event

Specifies if the item should wait for the OnAnimatorItemReloadComplete animation event or wait for the specified duration before completing the reload.

Reload Complete Audio Clip Set

The clip that should be played after the item has finished reloading.

Reload Detach Attach Clip

Should the weapon clip be detached and attached when reloaded?

Reload Detach Event

Specifies if the item should wait for the OnAnimatorItemReloadDetachClip animation event or wait for the specified duration before detaching the clip from the weapon.

Reload Show Projectile Event

Specifies if the item should wait for the OnAnimatorItemReloadShowProjectile animation event or wait for the specified duration before showing the projectile.

Reload Attach Projectile Event

Specifies if the item should wait for the OnAnimatorItemReloadAttachProjectile animation event or wait for the specified duration before parenting the projectile to the fire point.

Reload Drop Clip

The prefab that is dropped when the character is reloading.

Reload Clip Layer Change Delay

The amount of time after the clip has been removed to change the layer.

Reload Clip Target Layer

The layer that the clip object should change to after being reloaded.

Reload Drop Clip Event

Specifies if the item should wait for the OnAnimatorItemReloadDropClip animation event or wait for the specified duration before dropping the clip from the weapon.

Reload Attach Event

Specifies if the item should wait for the OnAnimatorItemReloadAttachClip animation event or wait for the specified duration before attaching the clip back to the weapon.

Position Recoil

The amount of positional recoil to add to the item.

Rotation Recoil

The amount of rotational recoil to add to the item.

Position Camera Recoil

The amount of positional recoil to add to the camera.

Rotation Camera Recoil

The amount of rotational recoil to add to the camera.

Camera Recoil Accumulation

The percent of the recoil to accumulate to the camera’s rest value.

Muzzle Flash

A reference to the muzzle flash prefab.

Pool Muzzle Flash

Should the muzzle flash be pooled? If false a single muzzle flash object will be used.

Shell

A reference to the shell prefab.

Shell Velocity

The velocity that the shell should eject at.

Shell Torque

The torque that the projectile should initialize with.

Shell Eject Delay

Eject the shell after the specified delay.

Smoke

A reference to the smoke prefab.

Smoke Spawn Delay

Spawn the smoke after the specified delay.

Tracer

Optionally specify a tracer that should should appear when the hitscan weapon is fired.

Tracer Spawn Delay

Spawn the tracer after the specified delay.

Disable Scope Camera On No Aim

Should the camera’s scope camera be disabled when the character isn’t aiming?

On Hitscan Impact Event

Unity event invoked when the hitscan hits another object.