# HitReactionTrigger

## HitReactionTrigger Inspector

`HitReactionTrigger` is a **drop‑in component** for any projectile, melee weapon, or explosion fragment that should cause procedural reactions on impact. It handles collision detection, figures out the best hit direction, and calls `HitReactionController.React()`—so you don’t have to write a single line of glue code.

> **Add via:** *Component ▸ Hit Reactions ▸ Hit Reaction Trigger* or include it in your projectile / weapon prefab.

***

### Inspector Reference

#### 1. Which target layers should react?

| Field            | Default                | Description                                                                                                                               |
| ---------------- | ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
| **React Layers** | `Everything` (bitmask) | Only collisions with these layers will forward a `React()` call. Exclude *Environment*, *Terrain*, etc., to avoid unnecessary processing. |

#### 2. Lifecycle

| Field              | Default | Description                                                                                      |
| ------------------ | ------- | ------------------------------------------------------------------------------------------------ |
| **Destroy On Hit** | ✔️      | If true, destroys the projectile GameObject after a successful hit—ideal for bullets and arrows. |
| **Destroy Delay**  | `0`     | Time (seconds) to wait before destroying. Useful when you need to play impact VFX first.         |

*(All other properties are hidden—it’s intentionally lightweight.)*

***

### How It Works

1. **OnCollisionEnter** fires.
2. Checks if the other collider’s layer is inside **React Layers**. If not → exit.
3. Tries to get a `BodyPartReactor` on `collision.transform`. If none → exit.
4. Searches parent hierarchy for a `HitReactionController`.
5. Calculates **hit direction** using fallback order:
   1. **Rigidbody velocity** (`Rigidbody.linearVelocity`).
   2. **Transform delta** from previous frame (`transform.position - _lastPos`).
   3. **Collision normal** (negated) if object didn’t move.
6. Calls `controller.React(part.name, hitDir, contactPoint)`.
7. Optionally destroys itself after **Destroy Delay**.

***

### Best Practices

* **Separate layers per team:** In PvP games, configure *PlayerProjectiles* vs. *EnemyCharacters* to avoid friendly fire reactions.
* **Melee weapons:** Place a thin *Trigger Collider* along the blade and enable `Is Trigger` on the collider. The component still works; just swap `OnCollisionEnter` for `OnTriggerEnter` if you need continuous overlap.
* **Grenade shrapnel:** Spawn multiple sphere colliders with triggers enabled; each can carry a `HitReactionTrigger` that destroys itself on the first contact.
* **Pooling systems:** Remember to reset (or disable) `Destroy On Hit` if the pooled projectile should live through multiple bounces.

***

### Extending the Trigger (Hooks)

Need custom logic (e.g., apply damage, play VFX)? Derive a subclass:

```csharp
public class DamageTrigger : HitReactionTrigger {
    public int damage = 25;

    protected override void OnReactionForwarded(GameObject target) {
        target.GetComponent<Health>()?.Take(damage);
    }
}
```

*(Override method not in base yet? Fork the script—only 60 lines!)*

***

#### Next Up

With all four components covered, jump into **Recipes & Workflows** to apply them in common gameplay scenarios.


---

# 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://ironsami-studio.gitbook.io/proceduralhitreactions/modules/hitreactiontrigger.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.
