概要
屬性
方法
AdjustSpeed(speed: number):() |
AdjustWeight(weight: number,fadeTime: number):() |
GetParameter(key: string):Variant |
GetTargetInstance(name: string):Instance? |
GetTimeOfKeyframe(keyframeName: string):number |
SetParameter(key: string,value: Variant):() |
SetTargetInstance(name: string,target: Instance?):() |
活動
KeyframeReached(keyframeName: string):RBXScriptSignal |
API 參考
屬性
Animation
範例程式碼
聆聽新的動畫
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
animator.AnimationPlayed:Connect(function(animationTrack)
local animationName = animationTrack.Animation.Name
print("動畫正在播放 " .. animationName)
end)IsPlaying
範例程式碼
動畫軌道正在播放
local function playOrAdjust(animationTrack, fadeTime, weight, speed)
if not animationTrack.IsPlaying then
animationTrack:Play(fadeTime, weight, speed)
else
animationTrack:AdjustSpeed(speed)
animationTrack:AdjustWeight(weight, fadeTime)
end
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
playOrAdjust(animationTrack, 1, 0.6, 1)Length
範例程式碼
為特定時間播放動畫
local function playAnimationForDuration(animationTrack, duration)
local speed = animationTrack.Length / duration
animationTrack:Play()
animationTrack:AdjustSpeed(speed)
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765000"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
playAnimationForDuration(animationTrack, 3)Looped
範例程式碼
動畫循環
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
while not localPlayer.Character do
task.wait()
end
local character = localPlayer.Character
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507770453"
local animationTrack = animator:LoadAnimation(animation)
animationTrack.Looped = false
task.wait(3)
animationTrack:Play()
task.wait(4)
animationTrack.Looped = true
animationTrack:Play()播放動畫軌跡以進行指定次數的循環
local function playForNumberLoops(animationTrack, number)
animationTrack.Looped = true
animationTrack:Play()
local numberOfLoops = 0
local connection = nil
connection = animationTrack.DidLoop:Connect(function()
numberOfLoops = numberOfLoops + 1
print("循環: ", numberOfLoops)
if numberOfLoops >= number then
animationTrack:Stop()
connection:Disconnect() -- 在不再需要連接時,重要的是要斷開連接
end
end)
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
playForNumberLoops(animationTrack, 5)Speed
範例程式碼
動畫速度
local ContentProvider = game:GetService("ContentProvider")
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
while not localPlayer.Character do
task.wait()
end
local character = localPlayer.Character
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507770453"
ContentProvider:PreloadAsync({ animation })
local animationTrack = animator:LoadAnimation(animation)
local normalSpeedTime = animationTrack.Length / animationTrack.Speed
animationTrack:AdjustSpeed(3)
local fastSpeedTime = animationTrack.Length / animationTrack.Speed
print("在正常速度下,動畫將播放", normalSpeedTime, "秒")
print("在 3 倍速度下,動畫將播放", fastSpeedTime, "秒")為特定時間播放動畫
local function playAnimationForDuration(animationTrack, duration)
local speed = animationTrack.Length / duration
animationTrack:Play()
animationTrack:AdjustSpeed(speed)
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765000"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
playAnimationForDuration(animationTrack, 3)TimePosition
範例程式碼
在位置動態凍結動畫
function freezeAnimationAtTime(animationTrack, timePosition)
if not animationTrack.IsPlaying\
then
-- 如果動畫沒有播放,則播放動畫
animationTrack:Play()
end
-- 將速度設置為 0 以凍結動畫
animationTrack:AdjustSpeed(0)
-- 跳轉到所需的 TimePosition
animationTrack.TimePosition = timePosition
end
function freezeAnimationAtPercent(animationTrack, percentagePosition)
if not animationTrack.IsPlaying then
-- 如果動畫沒有播放,則播放動畫
animationTrack:Play()
end
-- 將速度設置為 0 以凍結動畫
animationTrack:AdjustSpeed(0)
-- 跳轉到所需的 TimePosition
animationTrack.TimePosition = (percentagePosition / 100) * animationTrack.Length
end
local animation = Instance.new("Animation")
animation.AnimationId =\
"rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack\
= animator:LoadAnimation(animation)
freezeAnimationAtTime(animationTrack,\
0.5)
f freezeAnimationAtPercent(animationTrack, 50)WeightCurrent
範例程式碼
目前權重和目標權重
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
while not localPlayer.Character do
task.wait()
end
local character = localPlayer.Character
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation1 = Instance.new("Animation")
animation1.AnimationId = "rbxassetid://507770453"
local animation2 = Instance.new("Animation")
animation2.AnimationId = "rbxassetid://507771019"
task.wait(3) -- 任意的 等待時間以便角色進入正確位置
local animationTrack1 = animator:LoadAnimation(animation1)
local animationTrack2 = animator:LoadAnimation(animation2)
animationTrack1.Priority = Enum.AnimationPriority.Movement
animationTrack2.Priority = Enum.AnimationPriority.Action
animationTrack1:Play(0.1, 5, 1)
animationTrack2:Play(10, 3, 1)
local done = false
while not done and task.wait(0.1) do
if math.abs(animationTrack2.WeightCurrent - animationTrack2.WeightTarget) < 0.001 then
print("已達到此點")
done = true
end
endWeightTarget
範例程式碼
目前權重和目標權重
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
while not localPlayer.Character do
task.wait()
end
local character = localPlayer.Character
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation1 = Instance.new("Animation")
animation1.AnimationId = "rbxassetid://507770453"
local animation2 = Instance.new("Animation")
animation2.AnimationId = "rbxassetid://507771019"
task.wait(3) -- 任意的 等待時間以便角色進入正確位置
local animationTrack1 = animator:LoadAnimation(animation1)
local animationTrack2 = animator:LoadAnimation(animation2)
animationTrack1.Priority = Enum.AnimationPriority.Movement
animationTrack2.Priority = Enum.AnimationPriority.Action
animationTrack1:Play(0.1, 5, 1)
animationTrack2:Play(10, 3, 1)
local done = false
while not done and task.wait(0.1) do
if math.abs(animationTrack2.WeightCurrent - animationTrack2.WeightTarget) < 0.001 then
print("已達到此點")
done = true
end
end方法
AdjustSpeed
參數
| 預設值:1 |
返回
()
範例程式碼
為特定時間播放動畫
local function playAnimationForDuration(animationTrack, duration)
local speed = animationTrack.Length / duration
animationTrack:Play()
animationTrack:AdjustSpeed(speed)
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765000"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
playAnimationForDuration(animationTrack, 3)動畫速度
local ContentProvider = game:GetService("ContentProvider")
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
while not localPlayer.Character do
task.wait()
end
local character = localPlayer.Character
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507770453"
ContentProvider:PreloadAsync({ animation })
local animationTrack = animator:LoadAnimation(animation)
local normalSpeedTime = animationTrack.Length / animationTrack.Speed
animationTrack:AdjustSpeed(3)
local fastSpeedTime = animationTrack.Length / animationTrack.Speed
print("在正常速度下,動畫將播放", normalSpeedTime, "秒")
print("在 3 倍速度下,動畫將播放", fastSpeedTime, "秒")AdjustWeight
返回
()
範例程式碼
動畫曲線變更權重
local function changeWeight(animationTrack, weight, fadeTime)
animationTrack:AdjustWeight(weight,
fadeTime)
local startTime = tick()
while math.abs(animationTrack.WeightCurrent
- weight) > 0.001 do
task.wait()
end
print("變更權重所花費的時間 " .. tostring(tick() - startTime))
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid
= script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
changeWeight(animationTrack,
0.6, 1)GetMarkerReachedSignal
參數
範例程式碼
聆聽關鍵幀標記
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.Character:Wait()
local humanoid = character:WaitForChild("Humanoid")
-- 創建新的 "Animation" 實例
local kickAnimation = Instance.new("Animation")
-- 將其 "AnimationId" 設置為相應的動畫資源 ID
kickAnimation.AnimationId = "rbxassetid://2515090838"
-- 將動畫加載到人形上
local kickAnimationTrack = humanoid:LoadAnimation(kickAnimation)
-- 播放動畫軌道
kickAnimationTrack:Play()
-- 如果為動畫定義了命名事件,則將其連接到 "GetMarkerReachedSignal()"
kickAnimationTrack:GetMarkerReachedSignal("KickEnd"):Connect(function(paramString)
print(paramString)
end)GetTimeOfKeyframe
參數
返回
範例程式碼
跳到關鍵幀
local function jumpToKeyframe(animationTrack, keyframeName)
local timePosition = animationTrack:GetTimeOfKeyframe(keyframeName)
if not animationTrack.IsPlaying then
animationTrack:Play()
end
animationTrack.TimePosition = timePosition
end
local ANIMATION_ID = 0
local KEYFRAME_NAME = "測試"
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://" .. ANIMATION_ID
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
jumpToKeyframe(animationTrack, KEYFRAME_NAME)Play
返回
()
範例程式碼
為特定時間播放動畫
local function playAnimationForDuration(animationTrack, duration)
local speed = animationTrack.Length / duration
animationTrack:Play()
animationTrack:AdjustSpeed(speed)
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765000"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
playAnimationForDuration(animationTrack, 3)在位置動態凍結動畫
function freezeAnimationAtTime(animationTrack, timePosition)
if not animationTrack.IsPlaying\
then
-- 如果動畫沒有播放,則播放動畫
animationTrack:Play()
end
-- 將速度設置為 0 以凍結動畫
animationTrack:AdjustSpeed(0)
-- 跳轉到所需的 TimePosition
animationTrack.TimePosition = timePosition
end
function freezeAnimationAtPercent(animationTrack, percentagePosition)
if not animationTrack.IsPlaying then
-- 如果動畫沒有播放,則播放動畫
animationTrack:Play()
end
-- 將速度設置為 0 以凍結動畫
animationTrack:AdjustSpeed(0)
-- 跳轉到所需的 TimePosition
animationTrack.TimePosition = (percentagePosition / 100) * animationTrack.Length
end
local animation = Instance.new("Animation")
animation.AnimationId =\
"rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack\
= animator:LoadAnimation(animation)
freezeAnimationAtTime(animationTrack,\
0.5)
f freezeAnimationAtPercent(animationTrack, 50)SetTargetInstance
Stop
參數
| 預設值:0.100000001 |
返回
()
範例程式碼
動畫追蹤停止
local function fadeOut(animationTrack, fadeTime)
animationTrack:Stop(fadeTime)
local startTime = tick()
while animationTrack.WeightCurrent > 0 do
task.wait()
end
local timeTaken = tick() - startTime
print("重置權重所需時間: " .. tostring(timeTaken))
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
animationTrack:Play()
fadeOut(animationTrack, 1)活動
DidLoop
範例程式碼
播放動畫軌跡以進行指定次數的循環
local function playForNumberLoops(animationTrack, number)
animationTrack.Looped = true
animationTrack:Play()
local numberOfLoops = 0
local connection = nil
connection = animationTrack.DidLoop:Connect(function()
numberOfLoops = numberOfLoops + 1
print("循環: ", numberOfLoops)
if numberOfLoops >= number then
animationTrack:Stop()
connection:Disconnect() -- 在不再需要連接時,重要的是要斷開連接
end
end)
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
playForNumberLoops(animationTrack, 5)Ended
範例程式碼
動畫軌道結束
local InsertService = game:GetService("InsertService")
local Players = game:GetService("Players")
-- 創建一個 NPC 模型以進行動畫。
local npcModel = Players:CreateHumanoidModelFromUserIdAsync(129687796)
npcModel.Name = "JoeNPC"
npcModel.Parent = workspace
npcModel:MoveTo(Vector3.new(0, 15, 4))
local humanoid = npcModel:WaitForChild("Humanoid")
-- 載入一個動畫。
local animationModel = InsertService:LoadAsset(2510238627)
local animation = animationModel:FindFirstChildWhichIsA("Animation", true)
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
-- 連接到 Stopped 事件。當動畫停止時觸發,
-- 無論是自動停止還是我們明確調用 Stop。
animationTrack.Stopped:Connect(function()
print("動畫停止")
end)
-- 連接到 Ended 事件。當動畫完全
-- 終止對世界的影響時觸發。在這個例子中,它會在我們調用 animationTrack:Stop 之後的 3 秒內觸發,
-- 因為我們傳入了一個 3
-- 秒的 fadeOut。
animationTrack.Ended:Connect(function()
print("動畫結束")
animationTrack:Destroy()
end)
-- 執行,讓它播放一段時間,然後停止。
print("呼叫 Play")
animationTrack:Play()
task.wait(10)
print("呼叫 Stop")
animationTrack:Stop(3)KeyframeReached
Stopped
範例程式碼
動畫追踪停止
local function yieldPlayAnimation(animationTrack, fadeTime, weight, speed)
animationTrack:Play(fadeTime, weight, speed)
animationTrack.Stopped:Wait()
print("動畫已停止")
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
yieldPlayAnimation(animationTrack, 1, 0.6, 1)