Spring Events
Last updated
Last updated
All springs in the All In 1 Spring Toolkit can optionally fire events that you can subscribe to. These events provide a powerful way to respond to specific spring behaviors without the need for constant polling or checking. To enable events on any spring, you can do so in all Spring Components. In the Inspector, you'll find an option to enable events for each spring type (e.g., position, rotation, scale for TransformSpringComponent).
Once enabled, you'll be able to subscribe to three events:
OnTargetReached: This event fires when the spring's Current Value reaches the Target. It eliminates the need to check if we've arrived at the target every frame, allowing you to respond precisely when the desired state is achieved.
OnClampingApplied: This event fires the moment a value is clamped. If clamping is disabled, this event will never fire. A potential use for this event is interacting with or stopping the spring when it clamps to a known value defined by you.
OnCurrentValueChanged: This event fires when the spring's Current Value is moving towards the Target. Specifically, it triggers when the velocity is non-zero and the Target differs from the Current Value. This event is useful for tracking ongoing spring motion.
You can find a practical implementation of these events in the Demo scene. The script EventTestExamples.cs, located at AllIn1SpringsToolkit\Demo\Scripts\EventTestExamples.cs, demonstrates how to subscribe and unsubscribe to the events.
When working with spring events, remember to unsubscribe from events when they're no longer needed, especially when destroying objects, to prevent future null reference exceptions.