概要
方法
ApplyJointVelocities(motors: Variant):() |
GetTrackByAnimationId(animationId: ContentId):AnimationTrack? |
LoadAnimation(animation: Animation):AnimationTrack |
RegisterEvaluationParallelCallback(callback: function):() |
StepAnimations(deltaTime: number):() |
活動
AnimationPlayed(animationTrack: AnimationTrack):RBXScriptSignal |
API 參考
屬性
方法
ApplyJointVelocities
Animator:ApplyJointVelocities(motors:Variant):()
參數
motors:Variant |
返回
()
GetTrackByAnimationId
參數
animationId:ContentId |
範例程式碼
通過 ID 獲取動畫軌道
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ANIM_ID = "rbxassetid://2515090838"
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
-- 確保角色的 humanoid 包含 Animator 對象
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
-- 創建一個新的 Animation 實例並分配動畫資產 ID
local kickAnimation = Instance.new("Animation")
kickAnimation.AnimationId = ANIM_ID
-- 將動畫加載到動畫器上
local kickAnimationTrack = animator:LoadAnimation(kickAnimation)
-- 在短暫延遲後播放動畫軌道
task.wait(3)
kickAnimationTrack:Play()
local startTime = tick()
RunService:BindToSimulation(function(deltaTime: number)
if tick() - startTime > 1 then
-- 通過 ID 獲取軌道並停止播放
local kickTrack = animator:GetTrackByAnimationId(ANIM_ID)
if kickTrack and kickTrack.IsPlaying then
kickTrack:Stop()
end
end
end)LoadAnimation
RegisterEvaluationParallelCallback
StepAnimations
參數
返回
()
範例程式碼
在工作室中預覽動畫
local RunService = game:GetService("RunService")
local function previewAnimation(model, animation)
-- 找到 AnimationController 和 Animator
local animationController = model:FindFirstChildOfClass("AnimationController")
local animator = animationController and animationController:FindFirstChildOfClass("Animator")
if not animator then return end
-- 載入動畫以創建 AnimationTrack
local track = animationController:LoadAnimation(animation)
track:Play()
-- 預覽動畫
local startTime = tick()
while (tick() - startTime) < track.Length do
local step = RunService.Heartbeat:wait()
animator:StepAnimations(step)
end
-- 停止動畫
track:Stop(0)
animator:StepAnimations(0)
-- 重置關節
for _, descendant in pairs(model:GetDescendants()) do
if descendant:IsA("Motor6D") then
local joint = descendant
joint.CurrentAngle = 0
joint.Transform = CFrame.new()
end
end
end
local character = script.Parent
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
previewAnimation(character, animation)活動
AnimationPlayed
參數