概要
方法
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
-- 将动画加载到 animator
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
参数
返回
()
代码示例
在Studio中预览动画
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
参数