PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

Speed

AnimationTrack

float

The Speed of an AnimationTrack is a read only property that gives the current playback speed of the AnimationTrack. This has a default value of 1. When speed is equal to 1, the amount of time an animation takes to complete is equal to AnimationTrack/Length (in seconds).

If the speed is adjusted, then the actual time it will take a track to play can be computed by dividing the length by the speed. Speed is a unitless quantity.

Speed can be used to link the length of an animation to different game events (for example recharging an ability) without having to upload different variants of the same animation.

This property is read only and is changed using AnimationTrack/AdjustSpeed.


Code Samples


Playing Animation for a Specific Duration

The following function will play an AnimationTrack for a specific duration. This is done by changing the speed of the animation to the length of the animation divided by the desired playback duration. This could be used in situations where a developer wants to play a standard animation for different duration (for example, recharging different abilities).

function playAnimationForDuration(animationTrack, duration)
	local speed = animationTrack.Length / duration
	animationTrack:AdjustSpeed(speed)
	animationTrack:Play()
end

Animation Speed

In this example a player and an animation is loaded. The Length of an AnimationTrack determines how long the track would take to play if the speed is at 1. If the speed is adjusted, then the actual time it will take a track to play can be computed by dividing the length by the speed.

local ContentProvider = game:GetService("ContentProvider")
local Players = game:GetService("Players")
 
local localPlayer = Players.LocalPlayer
while not localPlayer.Character do wait() end
local character = localPlayer.Character
local humanoid = character:WaitForChild("Humanoid")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507770453"
 
ContentProvider:PreloadAsync({animation})
 
local animationTrack = humanoid:LoadAnimation(animation)
 
local normalSpeedTime = animationTrack.Length / animationTrack.Speed
animationTrack:AdjustSpeed(3)
local fastSpeedTime = animationTrack.Length / animationTrack.Speed
 
print("At normal speed the animation will play for", normalSpeedTime, "seconds")
print("At 3x speed the animation will play for", fastSpeedTime, "seconds")