AnimationTrack
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
ควบคุมการเล่นของอนิเมชั่นบน AnimationController นี้ โอเคตัวนี้ไม่สามารถสร้างได้ แต่จะถูกส่งกลับโดยวิธี Animator:LoadAnimation()
สรุป
คุณสมบัติ
วัตถุ Animation ที่ใช้สร้างวัตถุนี้ AnimationTrack
สมบัติสถานที่อ่านเท่านั้นที่จะกลับมาเป็นจริงเมื่อ AnimationTrack กำลังเล่น
สมบัติสรรพสินค้าที่อ่านเท่านั้นที่รวมความยาว (ในวินาที) ของ AnimationTrack นี้จะกลับ 0 จนกว่าอนิเมชั่นจะโหลดเสร็จสิ้นและดังนั้นจึงอาจไม่สามารถใช้งานได้ทันที
ตั้งค่าว่าอนิเมชันจะทำซ้ำหลังจากที่เสร็จสิ้น หากมันเปลี่ยนแปลงในขณะที่เล่นผลลัพธ์จะมีผลหลังจากที่อนิเมชันจบ
ตั้งค่าความสาبานของ AnimationTrack ตามที่ตั้งไว้ การเล่นหลายอนิเมชันพร้อมกันจะมองไปที่สมบัตินี้เพื่อหาว่า Keyframe ใดจะเล่นกันเอง การเล่นหลายอนิเมชันพร
ความเร็วของ AnimationTrack เป็นสมบัติที่อ่านเท่านั้นที่ให้ความเร็วเล่นปัจจุบันของ AnimationTrack ได้ มีค่าเริ่มต้นคือ 1 เมื่อความเร็วเท่ากับ
กลับตำแหน่งในเวลาในวินาทีที่ AnimationTrack แอนิเมชันแอนิเมชัน
สมบัติที่อ่านเท่านั้นที่ให้ค่าน้ำหนักปัจจุบันของ AnimationTrack มีค่าเริ่มต้น 1
สมบัติที่อ่านเท่านั้นที่ให้น้ำหนักปัจจุบันของ AnimationTrack
วิธีการ
เปลี่ยนแปลงค่า AnimationTrack.Speed แอนิเมชันค่าบวกสำหรับความเร็วเล่นอนิเมชั่นไปข้างหน้า และค่าลบสำหรับความเร็วเล่นอนิเมชั่นไปข้างหลัง และ 0 ช่วยให้ชะงัก
เปลี่ยนแป้นน้ำหนักของอนิเมชันด้วยตัวประกวดเวลาที่เลือนได้ เพื่อกำหนดว่าใช้เวลานานเท่าไรสำหรับ AnimationTrack.WeightCurrent เพื่อถึง AnimationTrack.WeightTarget
กลับคืน event ที่เรียกใช้เมื่อ KeyframeMarker ที่กำหนดไว้ถูกกดใน animation
กลับตำแหน่งเวลาของ Class.Keyframe ตัวแรกที่มีชื่อใน <a href="/reference/engine/datastores">Class.AnimationTrack</a>
เล่น AnimationTrack ตามลำดับ เมื่อเรียกว่า AnimationTrack จะเล่นกับเวลาที่เรียกไว้ น้ำหนัก และความเร็ว
หยุดการเรียก AnimationTrack เมื่อเรียกเล่นของ AnimationTrack จะหยุดและน้ำหนักของอนิเมชันจะเคลื่อนไปยังศูนย์ในช่วงเวลาที่กำหนดโดยตัวประกายเวลาเสื่อมสลาย
อีเวนต์
เกิดขึ้นเมื่อ AnimationTrack ห่วงเหนี่ยวกลับสุดท้ายของวงจรเมื่อการอัปเดตครั้งต่อไป
เกิดขึ้นเมื่อ AnimationTrack เสร็จสิ้นการย้ายอะไรใด ๆ ในโลก อนิเมชั่นได้เสร็จสิ้นแล้ว และสิ่งที่อยู่ในตำแหน่งกลางได้รับการเรียบเรียง
เปิดทุกครั้งที่เล่นของ AnimationTrack ไปยัง Keyframe ที่ไม่มีชื่อเริ่มต้น - "Keyframe."
เกิดขึ้นเมื่อ AnimationTrack เสร็จสิ้นการเล่น แทร็กอนิเมชันอาจยังคงอนิเมชันเนื้อหาขณะที่อนิเมชัน "หายไป" หากต้องการจับตอนที่ AnimationTrack เสร็จสิ้นการเคลื่อนไหวใด ๆ ใ
คุณสมบัติ
Animation
วัตถุ Animation ที่ใช้สร้าง AnimationTrack นี้ คุณต้องโหลดวัตถุ AnimationTrack บน 1> Class.Animator:LoadAnimation()1> โดยใช้วิธีการ 4> Class.Animator:Load
ตัวอย่างโค้ด
The following code sample prints the name of an animation whenever an AnimationTrack plays on a Humanoid. This script can be placed in a model with a Humanoid child.
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
animator.AnimationPlayed:Connect(function(animationTrack)
local animationName = animationTrack.Animation.Name
print("Animation playing " .. animationName)
end)
IsPlaying
สมบัติสถานที่อ่านเท่านั้นที่จะกลับมาเป็นจริงเมื่อ AnimationTrack กำลังเล่น
สมบัตินี้สามารถใช้โดยผู้พัฒนาเพื่อตรวจสอบว่าอนิเมชันกำลังเล่นก่อนเล่นมัน (รีสตาร์ท) หากผู้พัฒน
ตัวอย่างโค้ด
This code sample includes a simple function that will play an AnimationTrack if it is not playing, or otherwise adjust its speed and weight to match the new parameters given.
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
สมบัติสรรพสินค้าที่อ่านเท่านั้นที่รวมความยาว (ในวินาที) ของ AnimationTrack นี้จะกลับ 0 จนกว่าอนิเมชั่นจะโหลดเสร็จสิ้นและดังนั้นจึงอาจไม่สามารถใช้งานได้ทันที
เมื่อ AnimationTrack.Speed ของ AnimationTrack เท่ากับ 1 อนิเมชันจะใช้เวลา AnimationTrack.Length (ในวินาที) เพื่อเสร็จสิ้น
ตัวอย่างโค้ด
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).
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
สิ่งนี้ตั้งค่าว่าอนิเมชันจะทำซ้ำหลังจากที่มันจบลง หากมันเปลี่ยนแปลงในขณะที่เล่นผลลัพธ์จะมีผลหลังจากที่อนิเมชันจบลง
คุณสมบัติ Looped สำหรับ AnimationTrack เริ่มต้นด้วยวิธีที่กำหนดไว้ในเครื่องมือเรนเดอร์ อย่างไรก็ตามคุณสมบัตินี้สามารถเปลี่ยนแปลงได้ เ
สมบัตินี้ช่วยให้ผู้พัฒนามีตัวแปรที่เป็นห่วงและไม่เป็นห่วงของเดียวกันของแอนิเมชันเดียวกันโดยไม่จำเป็นต้องอัปโหลดสองเวอร์ชันไปยัง Roblox
ตัวอย่างโค้ด
The animation in this example normally loops. After the player and the animation are loaded the animation is played in a non-looped fashion then in a looped fashion.
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()
The function in this code sample will play an AnimationTrack on a loop, for a specific number of loops, before stopping the animation.
In some cases the developer may want to stop a looped animation after a certain number of loops have completed, rather than after a certain amount of time. This is where the DidLoop event can be used.
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("loop: ", numberOfLoops)
if numberOfLoops >= number then
animationTrack:Stop()
connection:Disconnect() -- it's important to disconnect connections when they are no longer needed
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)
Priority
คุณสมบัตินี้ตั้งค่าความสาبานของ AnimationTrack ตามที่ตั้งไว้ การเล่นหลายอนิเมชันพร้อมกันจะดูเหมือนคุณสมบัตินี้เพื่อหาว่า Keyframe ต่างๆ ควรเล่นกั
สมบัติค่า Priority สำหรับ AnimationTrack ปกติจะเป็นวิธีที่มันถูกตั้งและเผยแพร่จาก Studio Editor อนิเมชั่น มันใช้ Enum.AnimationPriority ซึ่งมี 7 ระดับความสำคัญ:
- Action4 (ความสำคัญสูงสุด)
- แอคชั่น3
- แอคชั่น2
- การกระทํา
- การเคลื่อนที่
- ไม่ได้ใช้งาน
- หลัก (ความสำคัญต่ำสุด)
กำหนดความสำคัญของอนิเมชันโดยเฉพาะผ่านเครื่องมือเรียบเรียงหรือผ่านคุณสมบัตินี้จะช่วยให้ผู้ใช้สามารถเล่นอนิเมชันได้หลายรายการโดยไม่ต้องมีการชน
สมบัตินี้ยังช่วยให้ผู้พัฒนาเล่นแอนิเมชันเดียวกันในความสามารถที่แตกต่างกันโดยไม่จำเป็นต้องอัปโหลดเวอร์ชันเพิ่มเติมไปยัง Roblox
Speed
ความเร็วของ AnimationTrack เป็นสมบัติที่อ่านเท่านั้นที่ให้ความเร็วเล่นปัจจุบันของ AnimationTrack ได้ มีค่าเริ่มต้นคือ 1 เมื่อความเร็วเท่ากับ
หากความเร็วถูกปรับแล้ว เวลาที่เรียกเก็บเงินจากแทร็กสามารถคำนวณได้โดยการแบ่งความยาวโดยความเร็ว ความเร็วเป็นปริมาณที่ไม่มีหน่วย
ความเร็วสามารถใช้เพื่อเชื่อมโยงความยาวของอนิเมชันไปยังเหตุการณ์เกมที่แตกต่างกัน (เช่นการรีชาร์จความสามารถ) แอนิเมชัน
คุณสมบัตินี้อ่านเท่านั้นและคุณสามารถเปลี่ยนแปลงได้โดยใช้ AnimationTrack:AdjustSpeed()
ตัวอย่างโค้ด
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
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("At normal speed the animation will play for", normalSpeedTime, "seconds")
print("At 3x speed the animation will play for", fastSpeedTime, "seconds")
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).
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
กลับตำแหน่งในเวลาในวินาทีที่ AnimationTrack แอนิเมชันแอนิเมชัน
แอนิเมชันแต่ AnimationTrack จะต้องเล่นเพื่อทำเช่นนั้น สามารถใช้ในคอมบินกับ AnimationTrack:AdjustSpeed() เพื่อแช่แข็งอนิเมชั
ตัวอย่างโค้ด
The following code sample includes two functions that demonstrate how AdjustSpeed and TimePosition can be used to freeze an animation at a particular point.
The first function freezes an animation at a particular point in time (defined in seconds). The second freezes at it at a percentage (between 0 or 100) by multiplying the percentage by the track length.
As TimePosition can not be used when an AnimationTrack is not playing, the functions check to make sure the animation is playing before proceeding.
function freezeAnimationAtTime(animationTrack, timePosition)
if not animationTrack.IsPlaying then
-- Play the animation if it is not playing
animationTrack:Play()
end
-- Set the speed to 0 to freeze the animation
animationTrack:AdjustSpeed(0)
-- Jump to the desired TimePosition
animationTrack.TimePosition = timePosition
end
function freezeAnimationAtPercent(animationTrack, percentagePosition)
if not animationTrack.IsPlaying then
-- Play the animation if it is not playing
animationTrack:Play()
end
-- Set the speed to 0 to freeze the animation
animationTrack:AdjustSpeed(0)
-- Jump to the desired 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
เมื่อตั้งค่าน้ำหนักใน AnimationTrack ไม่เปลี่ยนแปลงโดยทันที แต่ย้ายจาก WeightCurrent ไปยัง AnimationTrack.WeightTarget ตัวเวลาที่ใช้ในการทำสิ่งนี้จะถูกกำหนดโดยปารามเตอร์ fadeTime เมื่อเล่นอ
WeightCurrent สามารถตรวจสอบได้กับ AnimationTrack.WeightTarget เพื่อดูว่าน้ำหนักที่ต้องการได้รับหรือไม่ หมายเหตุว่าค่าเหล่านี้ไม่ควรได้รับการตรวจสอบสำหรับความเท่าเทียมก
ระบบน้ำหนักอนิเมชันใช้เพื่อการประเมินว่าอนิเมชัน AnimationTracks
ตัวอย่างโค้ด
This code sample loads two animations onto the local player's Humanoid and demonstrates how the fadeTime paramater in AnimationTrack.Play determines how long it takes for an AnimationTrack's WeightCurrent to reach it's WeightTarget.
As WeightCurrent and WeightTarget are floats the == operator cannot be used to compare, instead it is more appropriate to check that the difference between them is sufficiently small to assume the weight fade has completed.
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) -- arbitrary wait time to allow the character to fall into place
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("got there")
done = true
end
end
WeightTarget
AnimationTrack.WeightTarget เป็นสมบัติที่อ่านเท่านั้นที่ให้ค่าน้ำหนักปั
WeightCurrent สามารถตรวจสอบได้กับ AnimationTrack.WeightTarget เพื่อดูว่าน้ำหนักที่ต้องการได้รับหรือไม่ หมายเหตุว่าค่าเหล่านี้ไม่ควรได้รับการตรวจสอบสำหรับความเท่าเทียมก
ระบบน้ำหนักอนิเมชันใช้เพื่อการประเมินว่าอนิเมชัน AnimationTracks
ตัวอย่างโค้ด
This code sample loads two animations onto the local player's Humanoid and demonstrates how the fadeTime paramater in AnimationTrack.Play determines how long it takes for an AnimationTrack's WeightCurrent to reach it's WeightTarget.
As WeightCurrent and WeightTarget are floats the == operator cannot be used to compare, instead it is more appropriate to check that the difference between them is sufficiently small to assume the weight fade has completed.
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) -- arbitrary wait time to allow the character to fall into place
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("got there")
done = true
end
end
วิธีการ
AdjustSpeed
ฟังก์ชันนี้เปลี่ยนค่า AnimationTrack.Speed ของอนิเมชัน มูลค่าบวกสําหรับความเร็วเล่นอนิเมชันไปข้างหน้าและมูลค่าลบสําหรับความเร็วเล่นอนิเมชันไปข้างหลัง และ 0 ช่วยให้ชะลอมัน
ความเร็วเริ่มต้นของ AnimationTrack ตั้งไว้เป็นปารามิเตอร์ใน AnimationTrack:Play() อย่างไรก็ตามความเร็วของเส้นทางสามารถเปลี่ยนแปลงได้ในระหว่างการเล่นโดยใช้ AdjustSpeed เมื่อ
เมื่อปรับแต่งแล้ว เวลาที่เหลือเวลาจะเล่นสนามสามารถคำนวณได้โดยการแบ่งความยาวโดยความเร็ว ความเร็วเป็นปริมาณที่ไม่มีที่สิ้นสุด
ความเร็วสามารถใช้เพื่อเชื่อมโยงความยาวของอนิเมชันไปยังเหตุการณ์ที่เล่นแตกต่างกัน (เช่นการรีชาร์จความสามารถ) แอนิเมชัน
พารามิเตอร์
ความเร็วการเล่นที่เปลี่ยนแปลงไป
ส่งค่ากลับ
ตัวอย่างโค้ด
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).
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)
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
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("At normal speed the animation will play for", normalSpeedTime, "seconds")
print("At 3x speed the animation will play for", fastSpeedTime, "seconds")
AdjustWeight
เปลี่ยนแป้นน้ำหนักของอนิเมชันด้วยตัวประกวดเวลาที่เลือนได้ เพื่อกำหนดว่าใช้เวลานานเท่าไรสำหรับ AnimationTrack.WeightCurrent เพื่อถึง AnimationTrack.WeightTarget
เมื่อตั้งค่าน้ำหนักใน AnimationTrack ไม่เปลี่ยนแปลงโดยทันที แต่ย้ายจาก WeightCurrent ไปยัง AnimationTrack.WeightTarget ตัวเวลาที่ใช้ในการทำสิ่งนี้จะถูกกำหนดโดยปารามเตอร์ fadeTime เมื่อเล่นอ
WeightCurrent สามารถตรวจสอบได้กับ AnimationTrack.WeightTarget เพื่อดูว่าน้ำหนักที่ต้องการได้รับหรือไม่ หมายเหตุว่าค่าเหล่านี้ไม่ควรได้รับการตรวจสอบสำหรับความเท่าเทียมก
ระบบจัดอันดับอนิเมชันใช้เพื่อการประเมินว่าอนิเมชันใดจะ
พารามิเตอร์
น้ำหนักของอนิเมชันที่จะเปลี่ยนแปลง
ระยะเวลาของเวลาที่อนิเมชันจะเปลี่ยนแปลงระหว่างน้ำหนักเก่าและน้ำหนักใหม่
ส่งค่ากลับ
ตัวอย่างโค้ด
This code sample includes a function that changes the weight of an AnimationTrack and yields until the weight has changed to the new target weight.
The purpose of this sample is to demonstrate how the fadeTime parameter of AnimationTrack.AdjustWeight works. In most cases, if a developer wishes to yield over the fadeTime it is recommended they use wait(fadeTime).
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("Time taken to change weight " .. 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
ฟังก์ชันนี้กลับมาเป็น event ที่คล้ายกับเหตุการณ์ AnimationTrack.KeyframeReached ยกเว้นว่ามันจะเริ่มต้นเมื่อ KeyframeMarker ที่ก
เพื่อเรียนรู้เพิ่มเติมเกี่ยวกับการใช้งานฟังก์ชันนี้, ดู อีเว้นท์แอนิเมชัน ในบทความ แอนิเมชันเอดิตอร์
เพิ่มเติมเกี่ยวกับ Keyframes
Keyframe ชื่อสามารถตั้งได้ใน Roblox เครื่องมือแก้ไขอนิเมชัน เมื่อสร้างหรือแก้ไขอนิเมชัน พวกเขาไม่สามารถตั้งได้โดย Script บนอนิเมชันที่มีอยู่ก่อนที่จะเล่
Keyframe ชื่อไม่จำเป็นต้องเป็นเอกลักษณ์ เช่น หาก Animation มีสามชุดคีย์เฟรมที่มีชื่อว่า "EmitParticles" อีเมตเมื่อถึงชุดคีย์เฟรมหนึ่งใด ๆ จะได้รับการเรียกเรื่องอีเมตเ
ดูเพิ่มเติม:
พารามิเตอร์
ชื่อของ KeyFrameMarker สัญญาณที่กำลังสร้าง
ส่งค่ากลับ
สัญญาณที่สร้างขึ้นและยิงเมื่ออนิเมชั่นถึงที่สร้าง KeyFrameMarker
ตัวอย่างโค้ด
This LocalScript code waits for the local player's Humanoid object to load, then it creates a new Animation instance with the proper Animation.AnimationId. The animation is then loaded onto the humanoid, creating an AnimationTrack, and the track is played with AnimationTrack:Play(). Following that, the AnimationTrack:GetMarkerReachedSignal() function detects when the "KickEnd" marker is hit.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.Character:Wait()
local humanoid = character:WaitForChild("Humanoid")
-- Create new "Animation" instance
local kickAnimation = Instance.new("Animation")
-- Set its "AnimationId" to the corresponding animation asset ID
kickAnimation.AnimationId = "rbxassetid://2515090838"
-- Load animation onto the humanoid
local kickAnimationTrack = humanoid:LoadAnimation(kickAnimation)
-- Play animation track
kickAnimationTrack:Play()
-- If a named event was defined for the animation, connect it to "GetMarkerReachedSignal()"
kickAnimationTrack:GetMarkerReachedSignal("KickEnd"):Connect(function(paramString)
print(paramString)
end)
GetTimeOfKeyframe
กลับตำแหน่งเวลาของ Class.Keyframe ตัวแรกในชื่อที่ให้ใน Keyframe หากมีการแชร์ชื่อของ Class.Keyframe|Keyframes หลายตัว จะกลับสู่ตัวแรกในอนิเมชัน
ฟังก์ชันนี้จะกลับค่าข้อผิดพลาดหากมีการใช้งานกับชื่อของ keyframe ที่ไม่ถูกต้อง (เช่นไม่มีสำหรับตัวอย่าง) หรือถ้า Animation ที่อยู่เบื้องหลังไม่ได้โหลดอีกต่อไป
เพื่อตรวจสอบว่าอนิเมชันโหลดแล้ว ตรวจสอบว่า AnimationTrack.Length ใหญ่กว่าศูนย์
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
This sample includes a function that will jump to the first keyframe of a specified name in an AnimationTrack.
As AnimationTrack.TimePosition cannot be set while the animation is not playing the function first checks to see if the animation is playing.
This sample will only work once an Animation has loaded.
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
เมื่อ AnimationTrack:Play() ถูกเรียกอนิเมชันของเส้นทางจะเริ่มเล่นและน้ำหนักของอนิเมชันจะเพิ่มขึ้นจาก 0 เป็นน้ำหนักที่กำหนด (ปกติคือ 1) ในเวลาชุดที่กำหนด (ปกติคือ 0.1)
ความเร็วที่ AnimationTrack จะเล่นจะถูกกำหนดโดยตัวปรับความเร็ว (ปกติคือ 1) เมื่อความเร็วเท่ากันกับ 1 จำนวนวินาทีที่เส้นทางจะใช้เพื่อเสร็จสิ้นจะ
น้ำหนักและความเร็วของอนิเมชันนี้ยังสามารถเปลี่ยนแปลงได้หลังจากที่อนิเมชันเริ่มเล่นโดยใช้วิธีการ AnimationTrack:AdjustWeight() และ AnimationTrack:AdjustSpeed()
หากผู้พัฒนาต้องการเริ่มอนิเมชันที่จุดที่เฉพาะเจาะจงโดยใช้ AnimationTrack.TimePosition สิ่งสำคัญคือต้องเล่นอนิเมชันก่อนที่จะทำเช่นนี้
พารามิเตอร์
แอนิเมชัน
น้ำหนักของอนิเมชันที่จะเล่นที่
แอนิเมชัน
ส่งค่ากลับ
ตัวอย่างโค้ด
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).
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)
The following code sample includes two functions that demonstrate how AdjustSpeed and TimePosition can be used to freeze an animation at a particular point.
The first function freezes an animation at a particular point in time (defined in seconds). The second freezes at it at a percentage (between 0 or 100) by multiplying the percentage by the track length.
As TimePosition can not be used when an AnimationTrack is not playing, the functions check to make sure the animation is playing before proceeding.
function freezeAnimationAtTime(animationTrack, timePosition)
if not animationTrack.IsPlaying then
-- Play the animation if it is not playing
animationTrack:Play()
end
-- Set the speed to 0 to freeze the animation
animationTrack:AdjustSpeed(0)
-- Jump to the desired TimePosition
animationTrack.TimePosition = timePosition
end
function freezeAnimationAtPercent(animationTrack, percentagePosition)
if not animationTrack.IsPlaying then
-- Play the animation if it is not playing
animationTrack:Play()
end
-- Set the speed to 0 to freeze the animation
animationTrack:AdjustSpeed(0)
-- Jump to the desired 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)
Stop
หยุดการเรียก AnimationTrack เมื่อเรียกเล่นของ AnimationTrack จะหยุดและน้ำหนักของอนิเมชันจะเคลื่อนไปยังศูนย์ในช่วงเวลาที่กำหนดโดยตัวประกายเวลาเสื่อมสลาย
เช่นหากเรียก Stop ด้วยเวลาจมดอกของ 2 วินาทีจะใช้เวลาสองวินาทีสำหรับน้ำหนักของ AnimationTrack แอนิเมชันโปรดทราบ
ไม่แนะนำให้ใช้เวลาหายไปของ 0 วินาทีเพื่อพยายามที่จะเปลี่ยนแปลงผลลัพธ์นี้และสิ้นสุดการอนิเมชั่นทันทีเมื่อปัจจุบันนี้ส่งผลให้ AnimationTrack ตำแหน่งนี้เก็บแข็ง
พารามิเตอร์
เวลาที่ใช้ในวินาทีสำหรับน้ำหนักของอนิเมชันที่จะหายไป
ส่งค่ากลับ
ตัวอย่างโค้ด
This code sample includes a function that stops an AnimationTrack with a specific fadeTime, and yields until the fade is completed and the weight of the AnimationTrack is equal to zero.
The purpose of this sample is to demonstrate how the fadeTime parameter of AnimationTrack.Stop works. In most cases, if a developer wishes to yield over the fadeTime it is recommended they use wait(fadeTime).
local function fadeOut(animationTrack, fadeTime)
animationTrack:Stop(fadeTime)
local startTime = tick()
while animationTrack.WeightCurrent > 0 do
task.wait()
end
local timeTaken = tick() - startTime
print("Time taken for weight to reset: " .. 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 สำเร็จการอัปเดต โดยจะทำการอัปเดตใหม่ในภายหลัง
ขณะนี้อาจยิงที่จุดสิ้นสุดที่แน่นอนของแทร็กอนิเมชันที่ไม่มีห่วงโซ่ แต่พฤติกรรมนี้ไม่ควรเชื่อถือได้
ตัวอย่างโค้ด
The function in this code sample will play an AnimationTrack on a loop, for a specific number of loops, before stopping the animation.
In some cases the developer may want to stop a looped animation after a certain number of loops have completed, rather than after a certain amount of time. This is where the DidLoop event can be used.
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("loop: ", numberOfLoops)
if numberOfLoops >= number then
animationTrack:Stop()
connection:Disconnect() -- it's important to disconnect connections when they are no longer needed
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 เสร็จสิ้นการย้ายอะไรใด ๆ ในโลก อนิเมชั่นได้เสร็จสิ้นแล้ว และสิ่งที่อยู่ในตำแหน่งกลางได้รับการเรียบเรียง
คุณสามารถใช้สิ่งนี้เพื่อดำเนินการเมื่อเส้นทางอนิเมชันของเรื่องกลับมาในตำแหน่งที่กลางที่ไม่ได้รับผลกระทบจาก AnimationTrack หรือเพื่อทำความสะอาด AnimationTrack หรือการเชื่อม
ตัวอย่างโค้ด
The function in this code sample plays an animationTrack and yields until it has stopped and ended, printing at each step along the way.
local InsertService = game:GetService("InsertService")
local Players = game:GetService("Players")
-- Create an NPC model to animate.
local npcModel = Players:CreateHumanoidModelFromUserId(129687796)
npcModel.Name = "JoeNPC"
npcModel.Parent = workspace
npcModel:MoveTo(Vector3.new(0, 15, 4))
local humanoid = npcModel:WaitForChild("Humanoid")
-- Load an animation.
local animationModel = InsertService:LoadAsset(2510238627)
local animation = animationModel:FindFirstChildWhichIsA("Animation", true)
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
-- Connect to Stopped event. This fires when animation stops of
-- it's own accord, or we explicitly call Stop.
animationTrack.Stopped:Connect(function()
print("Animation stopped")
end)
-- Connect to Ended event. This fires when when animation is completely
-- finished affecting the world. In this case it will fire 3 seconds
-- after we call animationTrack:Stop because we pass in a 3
-- second fadeOut.
animationTrack.Ended:Connect(function()
print("Animation ended")
animationTrack:Destroy()
end)
-- Run, give it a bit to play, then stop.
print("Calling Play")
animationTrack:Play()
task.wait(10)
print("Calling Stop")
animationTrack:Stop(3)
KeyframeReached
เปิดทุกครั้งที่เล่นของ AnimationTrack ไปยัง Keyframe ที่ไม่มีชื่อเริ่มต้น - "Keyframe."
เหตุการณ์นี้ช่วยให้ผู้พัฒนาดำเนินการโค้ดที่กำหนดไว้ในแอนิเมชัน (ตั้งโดยชื่อ Keyframe หรือชื่อ Sounds ที่แตกต่างกันในแอนิเมชัน) ได้รับการข
Keyframe ชื่อไม่จำเป็นต้องเป็นเอกลักษณ์ เช่น หากอนิเมชันมีสามช่องสี่สีที่มีชื่อว่า "อนุภาค" เหตุการณ์ KeyframeReached จะเปิดในแต่ละครั้งเมื่อหนึ่งในช่องสีเหล่านี้ถึง
Keyframe สามารถตั้งได้ใน Roblox Animation Editor แอนิเมชันชื่อเหล่านี้ไม่สามารถตั้งได้โดย Script ในอนิเมชั่นที่กำลังเล่นได้
พารามิเตอร์
Stopped
เปิดเมื่อ AnimationTrack เสร็จสิ้นการเล่น
เหตุการณ์นี้มีหลายการใช้งาน สามารถใช้เพื่อรอจนกว่า AnimationTrack จะหยุดก่อนที่จะดำเนินการต่อ (เช่น หากเชื่อมต่อสามรูปแบบของอนิเมชันเพื่อเล่นหลังจากกัน
ตัวอย่างโค้ด
The function in this code sample will play an animationTrack and yield until it has stopped, before printing.
local function yieldPlayAnimation(animationTrack, fadeTime, weight, speed)
animationTrack:Play(fadeTime, weight, speed)
animationTrack.Stopped:Wait()
print("Animation has stopped")
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)