Overview | Videos | Documentation | Demos | Integrations | API | Support

Doc Home > How To > Receive Custom Item Events

When an Item hits an object it can optionally execute a custom event on that object as well. This allows you to add in new functionality without having to change or subclass any of the Third Person Controller code. As an example, when an object is hit by an Item you could play a new particle effect or new animation. For this example we are just going to apply a new debug output to the Explosive Barrel in the Third Person Shooter scene.

The first step is to create a new component which subscribes to the custom event. For example, if our custom event is named "Damaged" then we can use the component below.

using UnityEngine;
using Opsive.ThirdPersonController;

public class Damaged : MonoBehaviour {

    private void Awake()
        EventHandler.RegisterEvent< float, Vector3, Vector3, GameObject >(gameObject, "Damaged", LogOutput);

    private void LogOutput(float amount, Vector3 point, Vector3 normal, GameObject originator)
        Debug.Log("Barrel took " + amount + " damaged at location " + point + " with normal " + normal + " from object " + originator);

As soon as we attach this component to the barrel it will start to listen for the "Damaged" event. We still need to tell the Item to call this event so on the ShootableWeapon component on your Assault Rifle specify "Damaged" in the Hitscan Damage Event field.

Now whenever you shoot an Explosive Barrel with the Assault Rifle the LogOutput method will execute.

Barrel took 7 damage at location (8.5, 5.7, -2.1) with normal (-0.1, 0.0, -0.1) from object Doug

<- Prevent the Character from Colliding with Triggers
Replace Animations ->