Projectile

Projects are Trajectory Objects that can destroy themselves after they collide with an object or a set amount of time. They are used for objects that are fired from the Shootable Weapon component such as rockets or arrows.

Setup

A new projectile can be created by performing the following:

  1. Open the Object Manager and create a new object of type Projectile.
  2. Adjust the Projectile values to match the type of projectile. The most common values that will be adjusted are Destroy On Collision and Spawned Objects On Destruction.
  3. Assign the prefab to the Shootable Weapon that should fire it. This is assigned to the Projectile field under the Firing foldout:

Impact Callback

When the 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.Shared.Events;

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

    /// <summary>
    /// The object has been impacted with another object.
    /// </summary>
    /// <param name="ctx">The context of the impact.</param>
    private void OnImpact(ImpactCallbackContext ctx)
    {
        Debug.Log("Event received " + name + " impacted by " + ctx.ImpactCollisionData.SourceGameObject + " on collider " + ctx.ImpactCollisionData.ImpactCollider + ".");
    }

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

Inspected Fields

Sticky Layers

The layers that the object can stick to.

Destroy On Collision

Should the projectile be destroyed when it colliders with another object? A rocket will want this option enabled but an arrow normally will not.

Destruction Delay

The amount of time after a collision that the object should be destroyed.

Spawned Objects On Destruction

The objects which should spawn when the object is destroyed. Each element within this array has the following options:

  • Object: the object that should be spawned
  • Probability: the likelihood that the object will be spawned (0 – 1). A higher value means it is more likely to be spawned.
  • Random Spin: should a random spin be applied to the object after it has spawned?
Lifespan

The length of time that the projectile should exist before it deactivates if no collision occurs. This will prevent the object from existing forever if for example it is shot into the air and no gravity is applied to it.