Events

v1.0

There are multiple ways to hook into the Projectile and Projectile Spawner lifecycles. This is the primary way to connect the Projectile Factory with the rest of your codebase and other tools and systems from the Asset Store.

Events via Observers & Global Observers

Individual objects can be Observers or Global Observers. These objects will subscribe to the built-in events that Projectiles invoke. Check the dedicated Observers page for more details.

👁️pageObservers, Global Observers, and Observer Objects

Unity Events

You may be familiar with UnityEvents from other components.

These events allow you to add other classes and call specific methods when lifecycle events happen on the Projectile or Projectile Spawner. Get more details about these events, and when they are called, on the Projectile Spawner documentation page.

🔫pageProjectile Spawner

Examples

The Demo Scenes provide examples of how you can use these concepts in your project.

Unity Events Example

As seen in the screenshot above, each Projectile Spawner in the demo scenes features the ProjectileDemoActor calling the AddProjectileLaunched() method whenever a projecitles OnLaunch event is called.

This event fires off each time a Projectile is launched, and the method adds one to a private int which tracks how many Projectiles have been launched by the demo Actor. This value is displayed via a UI Text in the upper left of the demo scene window.

Using the UnityEvents is a straight forward, simple way to connect to the Projectile Factory.

Observer Example

The Projectile Spawner also has one Observer assigned to the Observers list.

This observer has no exposed parameters. As an ProjectileObserver, it automatically subscribes to all lifecycle events on each Projectile spawned -- this class inherits from ProjectileBehavior, as do all of the Behaviors.

Whenever CollisionEnter or TriggerEnter is called by a Projectile, the HitObject method on this observer is called. If the HitObject has a ProjectileDemoActor component, then the Actor who spawned the projectile will run the RegisterHit method, which computes damage and passes it to the hit Actor.

This Projectile Observer is a demo class, intended to showcase one way of connecting your project to the Projectile Factory events. Each project is unique, and you can choose for yourself the most efficient way to get the information you need to process data for your game.

Global Observer Example

The Factory Manager exists in all scenes, as it is a Static Scriptable Object. This manager maintains a List of all ProjectileSpawner and Projectile objects in the scene, along with global observers, which are ProjectileObserver objects.

In the demo scene, the Camera has a CameraShakeOnProjectileCollision component on it. This class inherits from GlobalObserver. This means it automatically subscribes to the FactoryManager lifecycle events.

The class overrides the CollisionEnter method, which is called whenever ANY projectile that is registered with the FactoryManager collides.

protected override void CollisionEnter(Projectile basicProjectile, Collision collision, GameObject objectHit = null, Vector3 contactPoint = default)
{
     var valueOnCurve = DistanceValueOnCurve(contactPoint);
     ShakeCamera(shakeDuration * valueOnCurve, shakeMagnitude * valueOnCurve, shakeFrequency * valueOnCurve);
}

Whenever this occurs, the method will shake the camera, with the shake impact determined by the distance the collision is from the camera itself.

Observer Object Example

Observer Objects are Monobehaviors which connect to an external Projectile, and utliize ProjectileObservers to perform actions.

The PComponent Fire Embers object has a Particle System that is a child of the main Projectile. The Turn Off on Collision Observer is a Projectile Observer which turns off the object it is attached to whenever the Projectile collides.

In this demo, it enables the embers particle to disappear immediately. This is also attached to the Fireball object lower in the prefab hierarchy.

Last updated