# Sensor System

Sensors are MonoBehaviour components that poll the environment at a configurable interval and write a float value into an `AIBehavior.State` key (or a `WorldStatesManager` category). When the written value changes, the existing state-change pipeline fires automatically — no extra code needed.

***

## Adding a Sensor

1. Add a sensor component (`RangeSensor` or `LineOfSightSensor`) to the same GameObject as `AIBehavior`.
2. The sensor auto-discovers the `AIBehavior` via `GetComponent`. Alternatively, drag the reference into the sensor's **Behavior** field.
3. Set the **State Key** to match the key used in your action requirements (e.g. `"playerInRange"`).
4. Configure detection parameters in the Inspector.
5. Done — the sensor polls every **Update Interval** seconds and writes the result directly into behavior state.

***

## Common Inspector Fields

| Field                    | Description                                                                                                                      |
| ------------------------ | -------------------------------------------------------------------------------------------------------------------------------- |
| **Behavior**             | The AIBehavior this sensor writes to. Leave empty to auto-find on the same GameObject.                                           |
| **State Key**            | The State key written on each poll. Must match a key used in action requirements.                                                |
| **Update Interval**      | How often (seconds) the sensor polls. Default 0.2 s. Lower = more responsive; higher = cheaper.                                  |
| **Write Target**         | `BehaviorState` (writes to the linked AIBehavior's local State) or `WorldState` (writes to a named WorldStatesManager category). |
| **World State Category** | Category name used when Write Target is `WorldState`.                                                                            |

***

## Output Modes

All built-in sensors support three output modes:

| Mode              | Written value                                                        |
| ----------------- | -------------------------------------------------------------------- |
| `Boolean`         | `1` if at least one target is detected; `0` otherwise                |
| `Count`           | Number of detected targets                                           |
| `NearestDistance` | Distance (world units) to the nearest detected target; `0` when none |

***

## RangeSensor

Detects objects within a configurable sphere radius.

| Field                     | Description                                                                                                                                    |
| ------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| **Radius**                | Detection radius in world units                                                                                                                |
| **Detection Layer**       | Physics layers to scan. Ignored when Require Target Binder is true.                                                                            |
| **Required Tag**          | Only include objects with this tag. Leave empty for any tag.                                                                                   |
| **Require Target Binder** | When true, uses `TargetBinderRegistry.All` as the candidate source instead of `Physics.OverlapSphere`. No Collider required on target objects. |
| **Output Mode**           | Boolean / Count / NearestDistance                                                                                                              |

Green wire-sphere gizmo is drawn in Scene view when the object is selected.

***

## LineOfSightSensor

Detects objects within a sphere, optionally filtered by a field-of-view cone, with a line-of-sight raycast to remove occluded targets.

| Field                     | Description                                                                                                |
| ------------------------- | ---------------------------------------------------------------------------------------------------------- |
| **Radius**                | Maximum detection range                                                                                    |
| **Field Of View Angle**   | Half-angle (degrees) of the FOV cone from `transform.forward`. `0` = full 360°. E.g. `45` = 90° total FOV. |
| **Detection Layer**       | Physics layers scanned by the initial sphere.                                                              |
| **Obstruction Layer**     | Physics layers that block line-of-sight raycasts (walls, terrain, etc.).                                   |
| **Required Tag**          | Only include objects with this tag.                                                                        |
| **Require Target Binder** | Same as RangeSensor — uses TargetBinderRegistry when true.                                                 |
| **Output Mode**           | Boolean / Count / NearestDistance                                                                          |

Yellow wire-sphere and FOV cone rays are drawn in Scene view when selected.

**Detection pipeline:** `OverlapSphereNonAlloc` → (optional) tag filter → (optional) FOV angle check → `Physics.Linecast` obstruction check → output.

***

## Force Poll

Call `sensor.ForcePoll()` from code to trigger an immediate poll outside the interval schedule. Returns the value written to state. Useful for synchronising state at scene load or testing via Editor scripts.

***

## Performance Notes

* `_updateInterval` (default 0.2 s) gates all polling — sensors do not run every frame.
* The `States` indexer suppresses redundant `OnStateChange` events when the written value does not change — stable frames produce no re-plan overhead.
* `RangeSensor` and `LineOfSightSensor` use a static pre-allocated `Collider[64]` overlap buffer — zero per-poll GC.
* `RequireTargetBinder = true` skips the physics overlap entirely, which can be cheaper in scenes where targets are already tracked in `TargetBinderRegistry`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://beelabs-dev.gitbook.io/beelabs-docs/goap-engine/sensor-system.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.
