Previously, in order to perform some logic once a Keyframe has been hit in an animation, you would have to name the Keyframe and then listen for the KeyframeReached signal on an Animation Track. However, this would fire for any Keyframe and then you would have to check against the Keyframe name first. You can see this old flow here.
We’ve decided to clean up this flow a bit and also have it be more integrated within the Animation Editor. This is where Keyframe Markers come in. Please be aware that we did not remove the old flow, and anyone using KeyframeReached for animation events should not experience any changes.
What are Keyframe Markers?
Keyframe Markers are new instances that are parented to a Keyframe. Whenever a Keyframe is hit on an Animation Track, we now look through all of the markers that are parented to the Keyframe and fire a corresponding event. You can connect to a marker by name, similar to how you would do with Instance:GetPropertyChangedSignal. Keyframe Marker’s also have a value property, which is a string parameter value meant to be passed to the event handler.
In the Animation Editor
The Animation Editor now has a new section of the timeline dedicated to Animation Events:
Events also get their own icon, separate from Keyframes and Poses, to make it easier to identify at what times events will be fired in your animation:
You can add new events via right-clicking on the area of the timeline containing the event icons, or click the “Create Event Button” to the left of the timeline area:
There is a specific window for editing all of the events that a particular Keyframe has. Here you can specify the name of the event upon adding as well as the optional parameter that will be passed to your connecting function in script:
In Script
If we wanted to use this new animation event system to play sound effects whenever the player’s foot touches the ground, we could create an event called “FootStep” in the editor and then place that event on any keys where the foot hits the ground. Let’s also pass a parameter value for the volume of the sound.
In Script, assuming you have access to both the Animation Track for this animation and a sound effect that you would like to play, the code would look something like this:
animationTrack:GetMarkerReachedSignal("FootStep"):Connect(function(value)
footStepSound.Volume = tonumber(value)
footStepSound:Play()
end)
We hope you enjoy this feature and find it useful. Please let us know if you encounter any inhibiting bugs or have ideas to improve it.