Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


Detect constraint events

Detect when the force or torque on a Physics Constraint component exceeds a threshold, using UnityEvents in the Inspector.
Read time 2 minutesLast updated 15 days ago

To detect when the force or torque on a constraint exceeds a threshold, use the Callbacks section of the Physics Constraint component. Unity calls a method or sets a parameter when the constraint exceeds the threshold you set.
Note
This documentation is about using Pose, Area, and Constraint components. To use 2D physics in the Unity Editor using components like the Rigidbody 2D component, refer to 2D physics instead.
To detect when a constraint's force or torque exceeds a threshold, follow these steps:
  1. Select the GameObject with the Physics Constraint component.
  2. In the
    Inspector

    ?

    window, expand Definition, expand Thresholds, then set Force Threshold or Torque Threshold to the amount of linear or rotational force that triggers the event.
  3. Expand Callbacks, then set Callback Source to Events.
  4. Expand Event settings.
  5. Set Activation to control when the event fires: Editor and Runtime, or Runtime Only.
  6. Drag the object you want the event to affect to the Object property, or select the picker (⊙).
  7. From the Functions dropdown, select the method to call or the parameter to set on that object.
  8. To call more than one method or set more than one parameter, select Add (+), then repeat the previous two steps.
Unity now calls the method or sets the parameter whenever the constraint's force or torque crosses the threshold you set.
To detect the same event using a script instead of the Inspector, refer to
PhysicsConstraintCallbacks.IJointThresholdCallback
or
PhysicsCallbacks.IJointThresholdCallback
in the
U2D.Physics
API.
For more information about the Callback Source and Event settings properties, refer to a constraint's component reference page, for example Physics Constraint Distance component reference.

Verify that an event fired

To check whether a constraint's event fired, expand Info > Callbacks in the Physics Constraint component, and check the Joint Threshold value. This value counts how many times Unity dispatched the event to a valid receiver.

Additional resources