AnimationTrack
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
ควบคุมการเล่นแอนิเมชั่นบน Animator ได้ วัตถุนี้ไม่สามารถสร้างได้ แทนที่จะส่งคืนโดยวิธี Animator:LoadAnimation()
สรุป
คุณสมบัติ
วัตถุ Animation ที่ใช้สร้างสิ่งนี้ AnimationTrack
คุณสมบัติอ่านเฉพาะที่ส่งคืนความจริงเมื่อ AnimationTrack กำลังเล่น
คุณสมบัติอ่านเฉพาะที่ส่งคืนความยาว (ในวินาที) ของ AnimationTrackจะส่งคืน 0 จนกว่าแอนิเมชั่นจะโหลดเสร็จและดังนั้นจึงอาจไม่สามารถใช้งานได้ทันที
ตั้งค่าว่าแอนิเมชั่นจะซ้ำหลังจากเสร็จสิ้น หากมันถูกเปลี่ยนแปลงในขณะที่เล่นผลลัพธ์จะมีผลหลังจากที่แอนิเมชั่นสิ้นสุด
กำหนดลําดับความสําคัญของ AnimationTrackขึ้นอยู่กับสิ่งที่ตั้งค่าไว้ การเล่นแอนิเมชั่นหลายอันพร้อมกันจะมองหาคุณสมบัตินี้เพื่อหาว่าแอนิเมชั่น Class.Keyframe``Class.Pose|Poses ควรเล่นบนกันได้อย่างไร
ความเร็วของ AnimationTrack เป็นคุณสมบัติอ่านเฉพาะที่ให้ความเร็วในการเล่นปัจจุบันของ AnimationTrackนี้มีค่าเริ่มต้นของ 1เมื่อความเร็วเท่ากับ 1 เวลาที่ใช้ในการสร้างแอนิเมชันจะเท่ากับ AnimationTrack.Length (ในวินาที)
ส่งคืนตำแหน่งในเวลาในวินาทีที่ AnimationTrack ผ่านการเล่นแอนิเมชั่นที่มาจากแหล่งของมันสามารถตั้งค่าเพื่อให้สนามแข่งกระโดดไปยังช่วงเวลาที่เฉพาะเจาะจงในแอนิเมชั่น
คุณสมบัติอ่านเฉพาะที่ให้น้ำหนักปัจจุบันของ AnimationTrack มันมีค่าเริ่มต้นของ 1
คุณสมบัติอ่านเฉพาะที่ให้น้ำหนักปัจจุบันของ AnimationTrack
วิธีการ
เปลี่ยน AnimationTrack.Speed แอนิเมชันค่าบวกสำหรับความเร็วเล่นแอนิเมชั่นไปข้างหน้า ค่าลบเล่นมันย้อนกลับ และ 0 หยุดมัน
เปลี่ยนน้ำหนักของแอนิเมชันด้วยพารามิเตอร์ fadeTime ที่เลือกได้ที่กำหนดระยะเวลาที่ใช้สำหรับ AnimationTrack.WeightCurrent ในการไปถึง AnimationTrack.WeightTarget
ส่งคืน event ที่จะยิงเมื่อ KeyframeMarker ที่กำหนดไว้ถูกโจมตีใน animation
ส่งคืนตำแหน่งเวลาของครั้งแรก Keyframe ของชื่อที่กำหนดใน AnimationTrack
เล่น AnimationTrack ครั้งเดียวเรียกว่า AnimationTrack จะเล่นด้วยเวลาการจางและน้ำหนักที่กำหนดและความเร็ว
หยุด AnimationTrack .
อีเวนต์
ไฟเมื่อลูป AnimationTrack บนการอัปเดตครั้งต่อไปหลังจากสิ้นสุดลูปแอนิเมชั่นก่อนหน้า
ไฟเมื่อ AnimationTrack เสร็จสิ้นการย้ายสิ่งใดในโลกอย่างสมบูรณ์แอนิเมชั่นสิ้นสุดการเล่นแล้ว "จางไป" สิ้นสุดแล้ว และหัวข้ออยู่ในท่าทางเป็นกลาง
ไฟไหม้ทุกครั้งที่เล่นซ้ำของ ไม่มีชื่อเริ่มต้น - "Keyframe"
เกิดไฟไหม้เมื่อ AnimationTrack สิ้นสุดการเล่นแทร็กแอนิเมชันยังอาจแอนิเมชันหัวข้อในขณะที่แอนิเมชัน "จางไป"เพื่อจับเมื่อแทร็กแอนิเมชันเสร็จสิ้นการเคลื่อนไหวสิ่งใดในโลกแล้วใช้เหตุการณ์ AnimationTrack.Ended
คุณสมบัติ
Animation
วัตถุ Animation ที่ใช้สร้างสิ่งนี้ AnimationTrackเพื่อสร้าง AnimationTrack , คุณต้องโหลดวัตถุ Animation บน Animator โดยใช้วิธี Animator:LoadAnimation()
ตัวอย่างโค้ด
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 กำลังเล่น
คุณสมบัตินี้สามารถใช้โดยนักพัฒนาเพื่อตรวจสอบว่าแอนิเมชั่นกําลังเล่นอยู่แล้วก่อนที่จะเล่น (เนื่องจากจะทําให้มันรีสตาร์ท)หากนักพัฒนาต้องการรับทั้งหมดที่เล่น AnimationTracks บน Animator หรือ Humanoid พวกเขาควรใช้ Animator:GetPlayingAnimationTracks()
ตัวอย่างโค้ด
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
คุณสมบัตินี้กำหนดว่าแอนิเมชั่นจะทำซ้ำหลังจากเสร็จสิ้นหรือไม่หากมันถูกเปลี่ยนแปลงในขณะที่เล่นผลลัพธ์จะมีผลหลังจากที่แอนิเมชั่นสิ้นสุด
คุณสมบัติที่ซ้ำสำหรับ AnimationTrack จะเป็นค่าเริ่มต้นของวิธีที่กำหนดไว้ในตัวแก้ไขแอนิเมชันอย่างไรก็ตามคุณสมบัตินี้สามารถเปลี่ยนแปลงได้โดยอนุญาตให้ควบคุมเกมในขณะที่กำลังดำเนินอยู่ AnimationTrackซ้ำยังจัดการแอนิเมชั่นที่เล่นในเชิงย้อนกลับ (เชิงลบ AnimationTrack.Speed ) ได้อย่างถูกต้องหลังจากกุญแจเฟรมแรกถูกถึงแล้ว จะรีสตาร์ทที่กุญแจเฟรมสุดท้าย
คุณสมบัตินี้ช่วยให้นักพัฒนาสามารถมีตัวแปรซ้ำและไม่ซ้ำของแอนิเมชั่นเดียวกันโดยไม่จำเป็นต้องอัปโหลดสองรุ่นไปยัง 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ขึ้นอยู่กับสิ่งที่ตั้งค่าไว้ การเล่นแอนิเมชั่นหลายอันพร้อมกันจะมองหาคุณสมบัตินี้เพื่อหาว่าแอนิเมชั่น Class.Keyframe``Class.Pose|Poses ควรเล่นบนกันได้อย่างไร
คุณสมบัติลําดับความสําคัญสำหรับ AnimationTrack ค่าเริ่มต้นจากวิธีที่กําหนดและเผยแพร่จาก Studio's เครื่องมือแอนิเมชั่นมันใช้ Enum.AnimationPriority ซึ่งมี 7 ระดับความสําคัญ:
- การดำเนินการ 4 (ลำดับความสำคัญสูงสุด)
- การกระทํา 3
- การดำเนินการ 2
- การดำเนินการ
- การเคลื่อนไหว
- ว่างเปล่า
- คอร์ (ลําดับความสําคัญต่ําสุด)
กำหนดลําดับความสําคัญของแอนิเมชั่นอย่างถูกต้อง ทั้งผ่านตัวแก้ไขหรือผ่านคุณสมบัตินี้ ช่วยให้สามารถเล่นแอนิเมชั่นหลายรายการโดยไม่มีการชนกันเมื่อแอนิเมชั่นการเล่นสองแอนิเมชั่นเปลี่ยนเป้าหมายให้ย้ายส่วนหนึ่งเดียวในลักษณะที่แตกต่างกัน แอนิเมชั่น AnimationTrack ที่มีลําดับความสําคัญสูงสุดจะแสดงหากทั้งสองแอนิเมชั่นมีลําดับความสําคัญเดียวกัน น้ำหนักของสนามจะถูกใช้เพื่อรวมแอนิเมชั่น
คุณสมบัตินี้ยังช่วยให้นักพัฒนาสามารถเล่นแอนิเมชันเดียวกันในลําดับความสําคัญที่แตกต่างกันโดยไม่จําเป็นต้องอัปโหลดเวอร์ชันเพิ่มเติมไปยัง Roblox
Speed
ความเร็วของ AnimationTrack เป็นคุณสมบัติอ่านเฉพาะที่ให้ความเร็วในการเล่นปัจจุบันของ AnimationTrackนี้มีค่าเริ่มต้นของ 1เมื่อความเร็วเท่ากับ 1 เวลาที่ใช้ในการสร้างแอนิเมชันจะเท่ากับ AnimationTrack.Length (ในวินาที)
หากความเร็วถูกปรับแต่งแล้วเวลาที่ใช้ในการเล่นจริงสามารถคำนวณได้โดยการหารความยาวโดยความเร็วความเร็วเป็นปริมาณที่ไม่มีหน่วย
ความเร็วสามารถใช้เพื่อเชื่อมโยงความยาวของแอนิเมชันกับเหตุการณ์เกมที่แตกต่างกัน (เช่นการชาร์จใหม่ความสามารถ) โดยไม่ต้องอัปโหลดตัวแปรที่แตกต่างกันของแอนิเมชันเดียวกัน
คุณสมบัตินี้อ่านได้เท่านั้นและคุณสามารถเปลี่ยนแปลงได้โดยใช้ 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() เพื่อแช่แข็งแอนิเมชั่นในจุดที่ต้องการ (โดยการตั้งความเร็วเป็น 0)
ตัวอย่างโค้ด
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 มันจะไม่เปลี่ยนแปลงทันที แต่จะย้ายจากน้ำหนักปัจจุบันไปที่ AnimationTrack.WeightTargetเวลาที่ใช้ในการทำเช่นนี้จะถูกกำหนดโดยพารามิเตอร์ fadeTime ที่ให้เมื่อเล่นแอนิเมชั่นหรือปรับน้ำหนัก
WeightCurrent สามารถตรวจสอบได้กับ AnimationTrack.WeightTarget เพื่อดูว่าน้ำหนักที่ต้องการถูกบรรลุแล้วหรือไม่โปรดทราบว่าค่าเหล่านี้ไม่ควรตรวจสอบเพื่อความเท่าเทียมกันกับตัวเลือก == เนื่องจากทั้งสองค่าเป็นจํานวนเต็มเพื่อดูว่า WeightCurrent ได้ถึงน้ำหนักเป้าหมายแล้วหรือยัง แนะนำให้ตรวจสอบว่าระยะห่างระหว่างค่าเหล่านั้นมีขนาดเล็กพอ (ดูตัวอย่างโค้ดด้านล่าง)
ระบบน้ำหนักแอนิเมชันใช้เพื่อกำหนดว่าแอนิเมชันที่เล่นในลําดับความสําคัญเดียวกันจะผสมรวมกันอย่างไร AnimationTracksน้ำหนักเริ่มต้นคือหนึ่งและไม่มีการเคลื่อนไหวที่จะปรากฏบน AnimationTrack ด้วยน้ำหนักเป็นศูนย์ท่าที่แสดงในช่วงเวลาใดก็ได้ถูกกำหนดโดยค่าเฉลี่ยเบาะแสของทั้งหมด Poses และน้ำหนักปัจจุบันของแต่ละ AnimationTrackในกรณีส่วนใหญ่การผสมผสานภาพเคลื่อนไหวไม่จำเป็นและการใช้ AnimationTrack.Priority เหมาะสมยิ่งขึ้น
ตัวอย่างโค้ด
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 เป็นคุณสมบัติอ่านได้ที่ให้น้ำหนักปัจจุบันของ AnimationTrackมันมีค่าเริ่มต้นของ 1 และตั้งค่าเมื่อ AnimationTrack:Play() , AnimationTrack:Stop() หรือ AnimationTrack:AdjustWeight() ถูกเรียกเมื่อตั้งน้ำหนักใน AnimationTrack มันจะไม่เปลี่ยนแปลงทันที แต่จะย้ายจากน้ำหนักปัจจุบันไปที่ AnimationTrack.WeightTargetเวลาที่ใช้ในการทำเช่นนี้จะถูกกำหนดโดยพารามิเตอร์ fadeTime ที่ให้เมื่อเล่นแอนิเมชั่นหรือปรับน้ำหนัก
WeightCurrent สามารถตรวจสอบได้กับ AnimationTrack.WeightTarget เพื่อดูว่าน้ำหนักที่ต้องการถูกบรรลุแล้วหรือไม่โปรดทราบว่าค่าเหล่านี้ไม่ควรตรวจสอบเพื่อความเท่าเทียมกันกับตัวเลือก == เนื่องจากทั้งสองค่าเป็นจํานวนเต็มเพื่อดูว่า WeightCurrent ได้ถึงน้ำหนักเป้าหมายแล้วหรือยัง แนะนำให้ตรวจสอบว่าระยะห่างระหว่างค่าเหล่านั้นมีขนาดเล็กพอ (ดูตัวอย่างโค้ดด้านล่าง)
ระบบน้ำหนักแอนิเมชันใช้เพื่อกำหนดว่าแอนิเมชันที่เล่นในลําดับความสําคัญเดียวกันจะผสมรวมกันอย่างไร AnimationTracksน้ำหนักเริ่มต้นคือหนึ่งและไม่มีการเคลื่อนไหวที่จะปรากฏบน AnimationTrack ด้วยน้ำหนักเป็นศูนย์ท่าที่แสดงในช่วงเวลาใดก็ได้ถูกกำหนดโดยค่าเฉลี่ยเบาะแสของทั้งหมด Poses และน้ำหนักปัจจุบันของแต่ละ AnimationTrackในกรณีส่วนใหญ่การผสมผสานภาพเคลื่อนไหวไม่จำเป็นและการใช้ AnimationTrack.Priority เหมาะสมยิ่งขึ้น
ตัวอย่างโค้ด
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:Play()อย่างไรก็ตามความเร็วของสนามแข่งสามารถเปลี่ยนแปลงได้ระหว่างการเล่นโดยใช้ AdjustSpeedเมื่อความเร็วเท่ากับ 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)
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
เปลี่ยนน้ำหนักของแอนิเมชันด้วยพารามิเตอร์ fadeTime ที่เลือกได้ที่กำหนดระยะเวลาที่ใช้สำหรับ AnimationTrack.WeightCurrent ในการไปถึง AnimationTrack.WeightTarget
เมื่อน้ำหนักถูกตั้งค่าใน AnimationTrack มันจะไม่เปลี่ยนแปลงทันที แต่จะย้ายจากน้ำหนักปัจจุบันไปที่ AnimationTrack.WeightTargetเวลาที่ใช้ในการทำเช่นนี้จะถูกกำหนดโดยพารามิเตอร์ fadeTime ที่ให้เมื่อเล่นแอนิเมชั่นหรือปรับน้ำหนัก
WeightCurrent สามารถตรวจสอบได้กับ AnimationTrack.WeightTarget เพื่อดูว่าน้ำหนักที่ต้องการถูกบรรลุแล้วหรือไม่โปรดทราบว่าค่าเหล่านี้ไม่ควรตรวจสอบเพื่อความเท่าเทียมกันกับตัวเลือก == เนื่องจากทั้งสองค่าเป็นจํานวนเต็มเพื่อดูว่า WeightCurrent ได้ถึงน้ำหนักเป้าหมายแล้วหรือยัง แนะนำให้ตรวจสอบว่าระยะห่างระหว่างค่าเหล่านั้นมีขนาดเล็กพอ (ดูตัวอย่างโค้ดด้านล่าง)
ระบบน้ำหนักแอนิเมชันใช้เพื่อกำหนดว่าแอนิเมชันที่เล่นในลําดับความสําคัญเดียวกันจะผสมรวมกันอย่างไร AnimationTracksน้ำหนักเริ่มต้นคือหนึ่งและไม่มีการเคลื่อนไหวที่จะปรากฏบน AnimationTrack ด้วยน้ำหนักเป็นศูนย์ท่าที่แสดงในช่วงเวลาใดก็ได้ถูกกำหนดโดยค่าเฉลี่ยเบาะแสของทั้งหมด Poses และน้ำหนักปัจจุบันของแต่ละ AnimationTrackดูด้านล่างสำหรับตัวอย่างการผสานแอนิเมชั่นในการปฏิบัติในกรณีส่วนใหญ่การผสมผสานภาพเคลื่อนไหวไม่จำเป็นและการใช้ AnimationTrack.Priority เหมาะสมยิ่งขึ้น
พารามิเตอร์
น้ำหนักที่อนิเมชั่นจะต้องเปลี่ยนเป็น
ระยะเวลาที่แอนิเมชั่นจะจางไประหว่างน้ำหนักเก่าและน้ำหนักใหม่
ส่งค่ากลับ
ตัวอย่างโค้ด
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 ที่กำหนดไว้ถูกโจมตีใน animationความแตกต่างช่วยให้ควบคุมได้ดีขึ้นเมื่ออีเวนต์จะยิงได้
เพื่อเรียนรู้เพิ่มเติมเกี่ยวกับการใช้ฟังก์ชันนี้ดู กิจกรรมแอนิเมชั่น ในบทความ ตัวแก้ไขแอนิเมชั่น
เพิ่มเติมเกี่ยวกับกุญแจเฟรม
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
ส่งคืนตำแหน่งเวลาของครั้งแรก Keyframe ของชื่อที่กำหนดใน AnimationTrackหากหลาย Keyframes แชร์ชื่อเดียวกัน จะส่งคืนชื่อเร็วที่สุดในแอนิเมชั่น
ฟังก์ชันนี้จะส่งคืนข้อผิดพลาดหากใช้กับชื่อกรอบรูปที่ไม่ถูกต้อง (ชื่อที่ไม่มีอยู่เช่นตัวอย่าง) หรือหากรากฐาน 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.Lengthตัวอย่างเช่น ความเร็ว 2 จะทำให้สนามเล่นเร็วขึ้นสองเท่า
น้ำหนักและความเร็วของแอนิเมชั่นยังสามารถเปลี่ยนแปลงได้หลังจากที่แอนิเมชั่นเริ่มเล่นโดยใช้วิธีการ 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)
SetTargetInstance
พารามิเตอร์
ส่งค่ากลับ
Stop
หยุด AnimationTrack .เมื่อเรียกแล้ว น้ำหนักของแอนิเมชั่นจะเคลื่อนไปสู่ศูนย์ภายในระยะเวลาที่กำหนดโดยพารามิเตอร์ fadeTime ปิดStop() ถูกเรียกด้วย fadeTime ของ 2 น้ำหนักของสนามจะใช้เวลาสองวินาทีในการถึงศูนย์และผลกระทบจะสิ้นสุดอย่างสมบูรณ์โปรดทราบว่านี่จะเป็นกรณีไม่ว่าน้ำหนักเริ่มต้นของแอนิเมชั่นจะเป็นเท่าใด
ไม่แนะนำให้ใช้ fadeTime ของ 0 ในการพยายามที่จะเอาชนะผลกระทบนี้และสิ้นสุดแอนิเมชั่นทันทีสำหรับ Motor6Ds ที่มีการตั้งค่า Motor.MaxVelocity เป็นศูนย์เนื่องจากส่งผลให้ข้อต่อแช่แข็งอยู่ที่เดียวหากต้องสิ้นสุดทันที ให้ตรวจสอบว่า Motor.MaxVelocity ของ Motor6Ds ในริกของคุณสูงพอที่จะสแน็ปได้อย่างถูกต้อง
พารามิเตอร์
เวลาในวินาทีที่น้ำหนักแอนิเมชันจะจางไป
ส่งค่ากลับ
ตัวอย่างโค้ด
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 ชื่อ)ซึ่งช่วยให้ฟังก์ชันเริ่มต้นของแอนิเมชั่น Roblox สามารถขยายได้โดยการเพิ่ม Sounds หรือ ParticleEffects ในจุดต่างๆ ในแอนิเมชั่น
Keyframe ชื่อไม่จำเป็นต้องเป็นเอกลักษณ์ตัวอย่างเช่น หากแอนิเมชั่นมีเฟรมสําคัญสามเฟรมที่ชื่อว่า "พาร์ติเคิล" เหตุการณ์ KeyframeReached จะยิงทุกครั้งที่เฟรมสําคัญเหล่านี้หนึ่งถูกถึง
Keyframe ชื่อสามารถตั้งค่าได้ใน Roblox Animation Editor แอนิเมชันScript ในแอนิเมชั่นที่มีอยู่ก่อนที่จะเล่น
พารามิเตอร์
Stopped
ไฟไหม้เมื่อใดก็ตามที่ AnimationTrack จบการเล่น
อีเวนต์นี้มีการใช้งานหลายอย่างสามารถใช้เพื่อรอจนกว่า AnimationTrack จะหยุดก่อนที่จะดำเนินการต่อ (ตัวอย่างเช่น หากเชื่อมโยงซีรีส์ของแอนิเมชั่นเพื่อเล่นหลังจากกันแล้ว)นอกจากนี้ยังสามารถใช้เพื่อล้างสิ่งที่สร้างขึ้นในระหว่างการเล่นแอนิเมชันได้ Instances
ตัวอย่างโค้ด
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)