AnimationTrack
*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.
Kiểm soát việc phát lại một hoạt hình trên một Animator . Đối tượng này không thể được tạo, thay vào đó nó được trả lại bởi phương pháp Animator:LoadAnimation() .
Tóm Tắt
Thuộc Tính
Vật thể Animation đã được sử dụng để tạo ra cái này AnimationTrack .
Một thuộc tính chỉ đọc trả về true khi AnimationTrack đang chơi.
Một thuộc tính chỉ đọc trả về chiều dài (theo giây) của một AnimationTrack .Điều này sẽ trả về 0 cho đến khi hoạt hình đã được tải hoàn toàn và do đó có thể không có sẵn ngay lập tức.
Xác định xem hoạt hình có lặp lại sau khi hoàn thành hay không. Nếu nó được thay đổi trong khi chơi, kết quả sẽ có hiệu lực sau khi hoạt hình hoàn thành.
Đặt ưu tiên của một AnimationTrack .Tùy thuộc vào những gì được đặt vào, chơi nhiều hoạt hình cùng một lúc sẽ xem xét tính chất này để xác định hoạt hình nào Class.Keyframe``Class.Pose|Poses nên được chơi trên nhau.
Tốc độ của một AnimationTrack là một tính năng chỉ đọc cho phép cung cấp tốc độ phát lại hiện tại của AnimationTrack .Nó có giá trị mặc định là 1.Khi tốc độ bằng 1, số thời gian một hoạt hình cần để hoàn thành bằng với AnimationTrack.Length (theo giây).
Trả vị trí trong thời gian bằng giây mà một AnimationTrack đang chơi phần hoạt hiệu ứng độngnguồn của nó.Có thể được đặt để làm cho đường đua nhảy đến một thời điểm cụ thể trong hoạt hiệu ứng động.
Thuộc tính chỉ đọc cung cấp trọng lượng hiện tại của AnimationTrack. Nó có giá trị mặc định là 1.
Tính chất chỉ đọc cho phép cung cấp trọng lượng hiện tại của AnimationTrack .
Phương Pháp
Thay đổi AnimationTrack.Speed của một hiệu ứng động. Giá trị dương cho tốc độ chơi hoạt hình tiến lên, giá trị âm chơi nó lùi lại, và 0 tạm dừng nó.
Thay đổi trọng lượng của một hiệu ứng động, với tham số fadeTime bắt buộc xác định thời gian mà AnimationTrack.WeightCurrent đạt được AnimationTrack.WeightTarget .
Trả về một event được kích hoạt khi một KeyframeMarker được đánh vào trong một animation .
Trả vị trí thời gian của đầu tiên Keyframe của tên được cho trong một AnimationTrack .
Chơi the AnimationTrack . Một khi được gọi một lần, AnimationTrack sẽ chơi với thời gian biến mờ được định, trọng lượng và tốc độ.
Dừng AnimationTrack .
Sự Kiện
Bắt lửa khi một vòng lặp AnimationTrack lặp lại trong bản cập nhật tiếp theo sau khi kết thúc vòng lặp hoạt hình trước.
Bắt lửa khi AnimationTrack hoàn thành hoàn toàn di chuyển bất cứ thứ gì trên thế giới.Hoạt hình đã kết thúc chơi, "mờ dần" đã kết thúc, và chủ đề ở trong tư thế trung lập.
Bắt lửa mỗi khi phát lại của một AnimationTrack đạt đến một Keyframe không có tên mặc định - "Keyframe".
Bắt lửa khi AnimationTrack kết thúc chơi.AnimationTrack vẫn có thể hoạt hình chủ đề trong khi hoạt hình "mờ dần".Để bắt khi AnimationTrack hoàn thành hoàn toàn di chuyển bất cứ thứ gì trên thế giới, hãy sử dụng sự kiện AnimationTrack.Ended.
Thuộc Tính
Animation
Vật thể Animation đã được sử dụng để tạo ra cái này AnimationTrack .Để tạo một AnimationTrack , bạn phải tải một đối tượng Animation vào một Animator bằng cách sử dụng phương pháp Animator:LoadAnimation().
Mẫu mã
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
Một thuộc tính chỉ đọc trả về true khi AnimationTrack đang chơi.
Tính năng này có thể được các nhà phát triển sử dụng để kiểm tra xem liệu có phải một hoạt họa đã được chơi trước khi chơi nó (vì điều đó sẽ khiến nó khởi động lại).Nếu một nhà phát triển muốn có được tất cả các chơi AnimationTracks trên một Animator hoặc một Humanoid , họ nên sử dụng Animator:GetPlayingAnimationTracks()
Mẫu mã
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
Một thuộc tính chỉ đọc trả về chiều dài (theo giây) của một AnimationTrack .Điều này sẽ trả về 0 cho đến khi hoạt hình đã được tải hoàn toàn và do đó có thể không có sẵn ngay lập tức.
Khi AnimationTrack.Speed của một AnimationTrack bằng 1, hoạt hình sẽ mất AnimationTrack.Length (trong giây lát) để hoàn thành.
Mẫu mã
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
Tính năng này xác định xem liệu hoạt hình có lặp lại sau khi hoàn thành hay không.Nếu nó được thay đổi trong khi chơi kết quả sẽ có hiệu lực sau khi hoàn thành hoạt hình.
Thuộc tính Looped cho AnimationTrack mặc định cách nó được đặt trong trình soạn thảo hoạt hình.Tuy nhiên, tính chất này có thể được thay đổi, cho phép kiểm soát AnimationTrack trong khi trò chơi đang chạy.Looped cũng xử lý đúng cách các hoạt hình được chơi lại theo chiều ngược lại (negative AnimationTrack.Speed ).Sau khi keyframe đầu tiên được đạt đến, nó sẽ khởi động lại ở keyframe cuối cùng.
Tính năng này cho phép nhà phát triển có một biến thể lặp lại và không lặp lại của cùng một hoạt hiệu ứng động, mà không cần phải tải hai phiên bản lên Roblox.
Mẫu mã
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
Thuộc tính này đặt ưu tiên của một AnimationTrack .Tùy thuộc vào những gì được đặt vào, chơi nhiều hoạt hình cùng một lúc sẽ xem xét tính chất này để xác định hoạt hình nào Class.Keyframe``Class.Pose|Poses nên được chơi trên nhau.
Thuộc tính ưu tiên cho mặc định cách nó được thiết lập và xuất bản từ Trình soạn thảo hoạt hình của Studio .Nó sử dụng Enum.AnimationPriority có 7 cấp ưu tiên:
- Hành động4 (ưu tiên cao nhất)
- Hành động3
- Hành động2
- Hành động
- Di chuyển
- Nhàn rỗi
- Lõi (ưu tiên thấp nhất)
Đặt ưu tiên hoạt hình một cách thích hợp, qua trình soạn thảo hoặc qua thuộc tính này, cho phép nhiều hoạt hình được chơi mà không xung đột với nhau.Nơi hai hoạt hình chơi hướng dẫn mục tiêu di chuyển cùng một chi trong các cách khác nhau, AnimationTrack với ưu tiên cao nhất sẽ hiển trình diễn.Nếu cả hai hoạt hình có cùng ưu tiên, trọng lượng của các bản nhạc sẽ được sử dụng để kết hợp các hoạt hình.
Tính năng này cũng cho phép nhà phát triển chơi cùng một hoạt họa tại các ưu tiên khác nhau, mà không cần phải tải thêm phiên bản lên Roblox.
Speed
Tốc độ của một AnimationTrack là một tính năng chỉ đọc cho phép cung cấp tốc độ phát lại hiện tại của AnimationTrack .Nó có giá trị mặc định là 1.Khi tốc độ bằng 1, số thời gian một hoạt hình cần để hoàn thành bằng với AnimationTrack.Length (theo giây).
Nếu tốc độ được điều chỉnh, thì thời gian thực mà nó sẽ mất để chơi một bài hát có thể được tính bằng cách chia chiều dài bằng tốc độ.Tốc độ là một lượng không có đơn vị.
Tốc độ có thể được sử dụng để liên kết chiều dài của một hoạt hình với các sự kiện trò chơi khác nhau (ví dụ nạp lại một khả năng) mà không cần phải tải các biến thể khác nhau của cùng một hiệu ứng động.
Thuộc tính này chỉ đọc, và bạn có thể thay đổi nó bằng cách sử dụng AnimationTrack:AdjustSpeed() .
Mẫu mã
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
Trả vị trí trong thời gian bằng giây mà một AnimationTrack đang chơi phần hoạt hiệu ứng độngnguồn của nó.Có thể được đặt để làm cho đường đua nhảy đến một thời điểm cụ thể trong hoạt hiệu ứng động.
Vị trí thời gian có thể được đặt để đi đến một điểm cụ thể trong hoạt hiệu ứng động, nhưng AnimationTrack phải đang chơi để làm như vậy.Nó cũng có thể được sử dụng kết hợp với AnimationTrack:AdjustSpeed() để đóng băng hoạt hình tại điểm mong muốn (bằng cách đặt tốc độ thành 0).
Mẫu mã
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
Khi trọng lượng được đặt trong một AnimationTrack nó không thay đổi ngay lập tức nhưng di chuyển từ WeightCurrent sang AnimationTrack.WeightTarget .Thời gian cần thiết để thực hiện việc này được xác định bởi tham số fadeTime được cung cấp khi hoạt hình được chơi, hoặc trọng lượng được điều chỉnh.
WeightCurrent có thể được kiểm tra chống lại AnimationTrack.WeightTarget để xem liệu trọng lượng mong muốn đã đạt được chưa.Lưu ý rằng các giá trị này không nên được kiểm tra bằng bộ so sánh ==, vì cả hai giá trị này là float.Để xem liệu WeightCurrent đã đạt đến trọng lượng mục tiêu chưa, được khuyến nghị xem xem khoảng cách giữa các giá trị đó có nhỏ đủ không (xem mẫu mã code bên dưới).
Hệ thống cân bằng hoạt hình được sử dụng để xác định cách AnimationTracks chơi cùng ưu tiên được kết hợp lại.Cân nặng mặc định là một, và không có chuyển động nào hiển thị trên một AnimationTrack với cân nặng là không.Tư thế được hiển thị tại bất kỳ thời điểm nào được xác định bởi trung bình trọng số của tất cả Poses và WeightCurrent của mỗi AnimationTrack .Trong hầu hết các trường hợp, không cần phải kết hợp các hoạt hình và sử dụng AnimationTrack.Priority là thích hợp hơn.
Mẫu mã
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 là một thuộc tính chỉ đọc cho phép cung cấp trọng lượng hiện tại của AnimationTrack .Nó có giá trị mặc định là 1 và được đặt khi AnimationTrack:Play() , AnimationTrack:Stop() hoặc AnimationTrack:AdjustWeight() được gọi.Khi trọng lượng được đặt trong một AnimationTrack nó không thay đổi ngay lập tức nhưng di chuyển từ WeightCurrent sang AnimationTrack.WeightTarget .Thời gian cần thiết để thực hiện việc này được xác định bởi tham số fadeTime được cung cấp khi hoạt hình được chơi, hoặc trọng lượng được điều chỉnh.
WeightCurrent có thể được kiểm tra chống lại AnimationTrack.WeightTarget để xem liệu trọng lượng mong muốn đã đạt được chưa.Lưu ý rằng các giá trị này không nên được kiểm tra bằng bộ so sánh ==, vì cả hai giá trị này là float.Để xem liệu WeightCurrent đã đạt đến trọng lượng mục tiêu chưa, được khuyến nghị xem xem khoảng cách giữa các giá trị đó có nhỏ đủ không (xem mẫu mã code bên dưới).
Hệ thống cân bằng hoạt hình được sử dụng để xác định cách AnimationTracks chơi cùng ưu tiên được kết hợp lại.Cân nặng mặc định là một, và không có chuyển động nào hiển thị trên một AnimationTrack với cân nặng là không.Tư thế được hiển thị tại bất kỳ thời điểm nào được xác định bởi trung bình trọng số của tất cả Poses và WeightCurrent của mỗi AnimationTrack .Trong hầu hết các trường hợp, không cần phải kết hợp các hoạt hình và sử dụng AnimationTrack.Priority là thích hợp hơn.
Mẫu mã
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
Phương Pháp
AdjustSpeed
Chức năng này thay đổi AnimationTrack.Speed của một hiệu ứng động.Một giá trị dương cho tốc độ chơi hoạt hình tiến lên, một giá trị âm chơi nó lùi lại, và 0 tạm dừng nó.
Tốc độ ban đầu của một AnimationTrack được đặt là một tham số trong AnimationTrack:Play() .Tuy nhiên, tốc độ của một bản nhạc có thể được thay đổi trong lúc phát, bằng cách sử dụng AdjustSpeed.Khi tốc độ bằng 1, số thời gian một hoạt hình cần để hoàn thành bằng AnimationTrack.Length (theo giây).
Khi điều chỉnh, thời gian thực mà nó sẽ mất để chơi có thể được tính bằng cách chia chiều dài bằng tốc độ. Tốc độ là một lượng không có đơn vị.
Tốc độ có thể được sử dụng để liên kết chiều dài của một hoạt họa với các sự kiện chơi game khác (ví dụ nạp lại một khả năng) mà không cần phải tải các biến thể khác nhau của cùng một hiệu ứng động.
Tham Số
Tốc độ phát lại mà hoạt hình sẽ được thay đổi thành.
Lợi Nhuận
Mẫu mã
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
Thay đổi trọng lượng của một hiệu ứng động, với tham số fadeTime bắt buộc xác định thời gian mà AnimationTrack.WeightCurrent đạt được AnimationTrack.WeightTarget .
Khi trọng lượng được đặt trong một AnimationTrack nó không thay đổi ngay lập tức nhưng di chuyển từ WeightCurrent sang AnimationTrack.WeightTarget .Thời gian cần thiết để thực hiện việc này được xác định bởi tham số fadeTime được cung cấp khi hoạt hình được chơi, hoặc trọng lượng được điều chỉnh.
WeightCurrent có thể được kiểm tra chống lại AnimationTrack.WeightTarget để xem liệu trọng lượng mong muốn đã đạt được chưa.Lưu ý rằng các giá trị này không nên được kiểm tra bằng bộ so sánh ==, vì cả hai giá trị này là float.Để xem liệu WeightCurrent đã đạt đến trọng lượng mục tiêu chưa, được khuyến nghị xem xem khoảng cách giữa các giá trị đó có nhỏ đủ không (xem mẫu mã code bên dưới).
Hệ thống cân bằng hoạt hình được sử dụng để xác định cách AnimationTracks chơi cùng ưu tiên được kết hợp lại.Cân nặng mặc định là một, và không có chuyển động nào hiển thị trên một AnimationTrack với cân nặng là không.Tư thế được hiển thị tại bất kỳ thời điểm nào được xác định bởi trung bình trọng số của tất cả Poses và WeightCurrent của mỗi AnimationTrack .Xem dưới đây để có một ví dụ về việc kết hợp hoạt hình trong thực tế.Trong hầu hết các trường hợp, không cần phải kết hợp các hoạt hình và sử dụng AnimationTrack.Priority là thích hợp hơn.
Tham Số
Cân nặng mà hoạt hình sẽ được thay đổi thành.
Thời gian mà hoạt hình sẽ biến mất giữa trọng lượng cũ và trọng lượng mới.
Lợi Nhuận
Mẫu mã
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
Chức năng này trả về một event tương tự với sự kiện AnimationTrack.KeyframeReached , ngoại trừ nó chỉ bắt lửa khi một KeyframeMarker được đánh vào trong một animation .Sự khác biệt cho phép kiểm soát tốt hơn về thời điểm sự kiện sẽ bắn.
Để tìm hiểu thêm về việc sử dụng chức năng này, xem Sự kiện hoạt hình trong bài viết Trình chỉnh sửa hoạt hình.
Thêm về khung chính
Keyframe tên có thể được đặt trong Roblox Trình chỉnh sửa hoạt hình khi tạo hoặc chỉnh sửa một hiệu ứng động.Tuy nhiên, chúng không thể được đặt bởi một Script trên một hoạt hình hiện có trước khi chơi nó.
Keyframe tên không cần phải duy nhất.Ví dụ, nếu một Animation có ba khung chính có tên là "EmitParticles," sự kiện kết nối được trả lại bởi chức năng này sẽ bắn mỗi khi một trong những khung chính này được tiếp cận.
Xem thêm:
- , giữ áp dụng cho các khớp ở một điểm thời gian nhất định trong một hoạt hiệu ứng động
Tham Số
Tên của KeyFrameMarker tín hiệu đang được tạo ra.
Lợi Nhuận
Tín hiệu được tạo và bắn khi hoạt hình đạt đến tạo KeyFrameMarker .
Mẫu mã
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
Trả vị trí thời gian của đầu tiên Keyframe của tên được cho trong một AnimationTrack .Nếu nhiều Keyframes chia sẻ cùng một tên, nó sẽ trả về cái sớm nhất trong hoạt hiệu ứng động.
Chức năng này sẽ trả lại một lỗi nếu nó được sử dụng với một tên khung chìa khóa không hợp lệ (một trong số chúng không tồn tại ví dụ) hoặc nếu khung cơ sở Animation chưa được tải.Để giải quyết vấn đề này, hãy chắc chắn chỉ sử dụng các tên keyframe chính xác và hoạt hình đã được tải trước khi gọi chức năng này.
Để kiểm tra xem liệu hoạt hình đã được tải hay chưa, hãy xác minh rằng AnimationTrack.Length lớn hơn không.
Tham Số
Lợi Nhuận
Mẫu mã
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
Khi AnimationTrack:Play() được gọi, hoạt hình của bản nhạc sẽ bắt đầu chơi và trọng lượng của hoạt hình sẽ tăng từ 0 lên trọng lượng được chỉ định (mặc định là 1) trong thời gian chuyển tiếp được chỉ định (mặc định là 0.1).
Tốc độ mà AnimationTrack sẽ chơi là được xác định bởi tham số tốc độ (mặc định là 1).Khi tốc độ bằng 1 số giây mà khúc trình sẽ mất để hoàn thành bằng với tính năng AnimationTrack.Length của khúc trình.Ví dụ, tốc độ 2 sẽ khiến bản nhạc chơi nhanh gấp hai lần.
Trọng lượng và tốc độ của hoạt hình cũng có thể được thay đổi sau khi hoạt hình bắt đầu chơi bằng cách sử dụng các phương pháp AnimationTrack:AdjustWeight() và AnimationTrack:AdjustSpeed().
Nếu nhà phát triển muốn bắt đầu hoạt hình ở một điểm cụ thể bằng cách sử dụng AnimationTrack.TimePosition, thì quan trọng là hoạt hình phải được chơi trước khi điều này được thực hiện.
Tham Số
Thời gian mà trọng lượng của hoạt hiệu ứng độngnên mờ dần trong.
Trọng lượng mà hoạt hình sẽ được chơi tại.
Tốc độ phát lại của hoạt hiệu ứng động.
Lợi Nhuận
Mẫu mã
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
Tham Số
Lợi Nhuận
Stop
Dừng AnimationTrack .Một khi được gọi, trọng lượng của hoạt hình sẽ di chuyển về phía không trong một thời gian được xác định bởi tham số tùy chọn fadeTime không bắt buộc.Ví dụ, nếu Stop() được gọi với một fadeTime của 2 , nó sẽ mất hai giây để trọng lượng của đường đua đạt đến không và hiệu ứng của nó hoàn toàn kết thúc.Xin lưu ý rằng đây sẽ là trường hợp bất kể trọng lượng ban đầu của hoạt hiệu ứng động.
Không được khuyến khích sử dụng một fadeTime của 0 trong một nỗ lực để vô hiệu hóa hiệu ứng này và kết thúc hoạt hình ngay lập tức cho Motor6Ds những người có Motor.MaxVelocity được đặt thành không có, vì điều này gây ra sự đóng băng của các khớp ở địa điểm.Nếu nó phải kết thúc ngay lập tức, hãy đảm bảo rằng Motor.MaxVelocity của Motor6Ds trong máy của bạn đủ cao để chúng có thể bật lên đúng cách.
Tham Số
Thời gian, trong giây lát, mà trọng lượng hoạt hình sẽ biến mất.
Lợi Nhuận
Mẫu mã
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)
Sự Kiện
DidLoop
Sự kiện này bắt lửa bất cứ khi nào một vòng lặp AnimationTrack hoàn thành vòng lặp, trong bản cập nhật tiếp theo.
Hiện tại nó cũng có thể bắn tại cuối chính xác của một khúc hoạt hình không lặp lại nhưng hành vi này không nên dựa vào.
Mẫu mã
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
Bắt lửa khi AnimationTrack hoàn thành hoàn toàn di chuyển bất cứ thứ gì trên thế giới.Hoạt hình đã kết thúc chơi, "mờ dần" đã kết thúc, và chủ đề ở trong tư thế trung lập.
Bạn có thể sử dụng điều này để thực hiện hành động khi chủ đề của bản hoạt hình quay trở lại trong tư thế trung lập không bị ảnh hưởng bởi AnimationTrack hoặc để dọn sạch AnimationTrack .hoặc bất kỳ kết nối liên quan nào.
Mẫu mã
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
Bắt lửa mỗi khi phát lại của một AnimationTrack đạt đến một Keyframe không có tên mặc định - "Keyframe."
Sự kiện này cho phép một nhà phát triển chạy mã tại các điểm được xác định trước trong một hoạt hình (đặt bởi Keyframe tên).Điều này cho phép chức năng mặc định của hoạt hình Roblox được mở rộng bằng cách thêm Sounds hoặc ParticleEffects tại các điểm khác nhau trong một hiệu ứng động.
Keyframe tên không cần phải duy nhất.Ví dụ, nếu một hoạt hình có ba khung chính được đặt tên là "Particles" thì sự kiện KeyframeReached sẽ bắn mỗi khi một trong những khung chính này được đạt tới.
Keyframe tên có thể được đặt trong Trình chỉnh sửa hoạt hình Roblox khi tạo hoặc chỉnh sửa một hiệu ứng động.Tuy nhiên, chúng không thể được đặt bởi một Script trên một hoạt hình hiện có trước khi chơi nó.
Tham Số
Stopped
Bắt lửa mỗi khi AnimationTrack kết thúc chơi.
Sự kiện này có một số lần sử dụng.Nó có thể được sử dụng để chờ cho đến khi một AnimationTrack đã dừng lại trước khi tiếp tục ( ví dụ, nếu chuỗi các hoạt hình được chuỗi lại để chơi sau nhau ).Nó cũng có thể được sử dụng để làm sạch bất kỳ Instances được tạo ra trong quá trình phát lại hoạt hình.
Mẫu mã
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)