Skip to main content

Trigger Sensor

The Trigger Sensor detects objects that intersect a Trigger Collider. It works by listening for the events OnTriggerEnter and OnTriggerExit. The sensor has a similar role as the Range Sensor, with some unique advantages. The downside is that its more difficult to configure. There are some subtle complexities to Trigger Colliders in Unity that must be considered when using this sensor.


A Trigger Sensor listens for collision events on a Trigger Collider. Any colliding objects are detected

Background Knowledge

It's recommended to have an understanding of Trigger Colliders and Collision Detection in Unity. This page of the Unity manual is a good starting point. The Collision action matrix tables at the bottom of that page are especially important to understand. It show's why Trigger Sensors often need to have Kinematic Rigid Bodies attached.

The Trigger Sensor is listening for collision detection events. When the OnTriggerEnter event is received then something is detected. When the OnTriggerExit event is received then a detection is lost.

info

The sensor will display warnings for common configuration issues. For example if it sees that you'll need a Kinematic RigidBody added in order for the above events to be received then it will tell you.

Output Signals

  • Object - Collider.gameObject or Collider.attachedRigidBody.gameObject depending on Detection Mode
  • Strength - Set to 1
  • Shape - The Bounds encapsulates all the Colliders associated to the Signal.

The Trigger Sensor does not need to be pulsed. It's updated immediately when it receives OnTriggerEnter or OnTriggerExit events. It's not possible to test this sensor in edit mode.

Configuration

Layers

There is no property on the sensor to configure the physics layers it detects on. Instead you must configure Unity's physics collision matrix in the project settings.

The sensor is not opinionated how you configure your layers, and youre free to configure them how you want. In the end all that's important for the sensor is that it recieves those OnTriggerEnter and OnTriggerExit events.

My recommended configuration is to create a physics layer called sensors and put your Trigger Sensors on that. In the collision matrix you can then control exactly which layers they detect. Also make sure the sensors layer doesn't collide with itself, you probably don't want two Trigger Sensors detecting each other.


'Sensors' is a dedicated physics layer for Trigger Sensors that will detect objects on 'Player' and 'Enemy' layers

Filters

Any GameObject in the Ignore List will not be detected by the sensor. If the Tag Filter is enabled then a GameObject must have one of the specified tags for it to be detected.

Safe Mode

When you enable Safe Mode the sensor will add a helper component that protects against some quirks in Unity where OnTriggerExit events are not triggered. This will happen when a detected object is deactivated, moved away from the sensor and then reactivated. The OnTriggerExit event is not triggered by deactivated GameObjects and the Trigger Sensor will still show that it's detected. Often this is a problem when using a Pooling system, such as in the following example scenario:

  • An object named SpaceShip is detected by the sensor.
  • The SpaceShip was destroyed, so it's GameObject is disabled and returned to the pool.
  • The SpaceShip is spawned somewhere else in the level, it's moved there and enabled. The sensor still thinks it's detected, even though it could be far away.

In Safe Mode the sensor will additionally check that detected objects send OnTriggerStay events. This will identify objects that are out of detection range but failed to send an OnTriggerExit.

info

You can solve this issue without enabling Safe Mode. Just before you deactivate the object set it's position far from any sensor, eg: object.transform.position = new Vector3(10000, 10000, 10000);, and OnTriggerExit will be sent. This is the best solution but obviously it's a bit of a hassle so Safe Mode is available as an alternative.

warning

It may take a couple of in-game frames before Safe Mode notices an object is not detected. This is because OnTriggerStay is fired during the physics update, which often occurs less frequently then regular updates.


Safe Mode is enabled so a helper component checks for invalid detections

Performance Considerations

The sensor does not implement Update, LateUpdate or FixedUpdate so there is no overhead to update them each frame. The only overhead is on Unity's physics system to calculate collisions between the Trigger Colliders with other Colliders. Generally this is highly optimised, especially if the sensor is not moving. The sensor has good performance scalability if you need a large number of static trigger zones.

Safe Mode does need to update each frame, so there is an increased overhead per-sensor if it's enabled.

API

API page