# Events

There are multiple ways to hook into the [**Projectile**](/magic-pig-games/projectile-factory/projectile-factory-documentation/projectile.md) and [**Projectile Spawner**](/magic-pig-games/projectile-factory/projectile-factory-documentation/projectile-spawner.md) 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**](/magic-pig-games/projectile-factory/projectile-factory-documentation/observers-global-observers-and-observer-objects.md). These objects will subscribe to the built-in events that Projectiles invoke. Check the dedicated Observers page for more details.

{% content-ref url="/pages/W2Nfh9Vw6Y5AmjTICsMH" %}
[Observers, Global Observers, and Observer Objects](/magic-pig-games/projectile-factory/projectile-factory-documentation/observers-global-observers-and-observer-objects.md)
{% endcontent-ref %}

## Unity Events

You may be familiar with `UnityEvents` from other components.

<figure><img src="/files/tQHecRprgwqiRrEvfjSd" alt=""><figcaption></figcaption></figure>

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**](/magic-pig-games/projectile-factory/projectile-factory-documentation/projectile-spawner.md) documentation page.

{% content-ref url="/pages/QjrszjgnayCZgLCafWvs" %}
[Projectile Spawner](/magic-pig-games/projectile-factory/projectile-factory-documentation/projectile-spawner.md)
{% endcontent-ref %}

## 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.

<figure><img src="/files/HfB7R7yWkyQEiZ6Y0zmf" alt=""><figcaption></figcaption></figure>

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.

<figure><img src="/files/tEPAgkuP6NhKf3GsTF83" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/LzYZ2hTdavhyGpmLSeiP" alt=""><figcaption></figcaption></figure>

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**](/magic-pig-games/projectile-factory/projectile-factory-documentation/behaviors.md).

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`.&#x20;

{% hint style="info" %}
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.
{% endhint %}

## Global Observer Example

The [**Factory Manager**](/magic-pig-games/projectile-factory/projectile-factory-documentation/factory-manager.md) 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**](/magic-pig-games/projectile-factory/projectile-factory-documentation/observers-global-observers-and-observer-objects.md), 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.

```csharp
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**](/magic-pig-games/projectile-factory/projectile-factory-documentation/observers-global-observers-and-observer-objects.md) are Monobehaviors which connect to an external Projectile, and utliize `ProjectileObservers` to perform actions.

<figure><img src="/files/hYMRpwtruCBVc15B9qZy" alt=""><figcaption></figcaption></figure>

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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://infinitypbr.gitbook.io/magic-pig-games/projectile-factory/projectile-factory-documentation/events.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
