Engine Class
AnimationTrack
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
สรุป
คุณสมบัติ
วิธีการ
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
ตัวอย่างโค้ด
การเล่น AnimationTrack
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()เล่น AnimationTrack เพื่อจำนวนรอบ
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("ที่ความเร็ว 3x อนิเมชันจะเล่นเป็นเวลา", 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)
freezeAnimationAtPercent(animationTrack, 50)WeightCurrent
ตัวอย่างโค้ด
WeightCurrent และ WeightTarget
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
ตัวอย่างโค้ด
WeightCurrent และ WeightTarget
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("ที่ความเร็ว 3x อนิเมชันจะเล่นเป็นเวลา", fastSpeedTime, "วินาที")AdjustWeight
ส่งค่ากลับ
()
ตัวอย่างโค้ด
การเปลี่ยนแปลงน้ำหนัก AnimationTrack
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
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
การฟัง Keyframe Markers
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"
-- โหลดอนิเมชันเข้าสู่วัตถุ humanoid
local kickAnimationTrack = humanoid:LoadAnimation(kickAnimation)
-- เล่นแทร็กอนิเมชัน
kickAnimationTrack:Play()
-- หากมีเหตุการณ์ชื่อที่ถูกกำหนดไว้สำหรับอนิเมชันเชื่อมต่อกับ "GetMarkerReachedSignal()"
kickAnimationTrack:GetMarkerReachedSignal("KickEnd"):Connect(function(paramString)
print(paramString)
end)GetTargetInstance
GetTimeOfKeyframe
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
กระโดดไปที่ Keyframe
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 = "Test"
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)
freezeAnimationAtPercent(animationTrack, 50)SetParameter
SetTargetInstance
Stop
พารามิเตอร์
| ค่าเริ่มต้น: 0.100000001 |
ส่งค่ากลับ
()
ตัวอย่างโค้ด
หยุด AnimationTrack
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
ตัวอย่างโค้ด
เล่น AnimationTrack เพื่อจำนวนรอบ
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
ตัวอย่างโค้ด
การสิ้นสุดของ AnimationTrack
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 ซึ่งจะถูกเรียกเมื่ออนิเมชั่น
-- เสร็จสิ้นการมีผลต่อโลก ในกรณีนี้จะถูกเรียก 3 วินาที
-- หลังจากที่เราเรียก animationTrack:Stop เนื่องจากเราผ่านค่า 3
-- วินาที fadeOut
animationTrack.Ended:Connect(function()
print("อนิเมชั่นเสร็จสิ้น")
animationTrack:Destroy()
end)
-- ทำงาน, ให้เวลาเล่นหน่อย แล้วหยุด
print("กำลังเรียก Play")
animationTrack:Play()
task.wait(10)
print("กำลังเรียก Stop")
animationTrack:Stop(3)KeyframeReached
Stopped
ตัวอย่างโค้ด
การหยุดของ AnimationTrack
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)