AnimationTrack
*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。
Class.AnimationController にアニメーションのプレイを制御する。このオブジェクトは、Animator:LoadAnimation() メソッドによって作成できません。
概要
プロパティ
この AnimationTrack を作成するために使用された Class.Animation オブジェクト。
Class.AnimationTrack がプレイ中のときに true を返す読み取り専用プロパティ。
Class.AnimationTrack の長さ (秒単位) を返す読み取り専用プロパティ。これは、アニメーションが完全に読み込まれるまで 0 を返し、すぐに利用できるわけではないため、Class.AnimationTrack を使用することができません。
アニメーションが終了後に繰り返されるかどうかを設定します。アニメーションが終了すると、結果の変更はアニメーション終了後に有効になります。
Class.AnimationTrack の優先度を設定します。これを設定すると、複数のアニメーションを同時にプレイすると、このプロパティに沿って、Keyframe 、Poses がそれぞれをプレイする順序を決めるようになります。
Class.AnimationTrack の速度は、AnimationTrack の現在の再生速度を読み取る読み取り専用プロパティです。これには、デフォルトの値 1 があります。速度が 1 で等しいと、アニメーションの完了時間は AnimationTrack.Length (秒) です。
ソースアニメーションをプレイすることで、AnimationTrack が位置を更新した秒数を返します。これを特定のアニメーションの時間にトラックをジャンプさせるように設定できます。
Class.AnimationTrack の現在の重量を与える読み取り専用プロパティ。デフォルト値は 1 です。
現在の重量を与える AnimationTrack のプロパティのみを読み取ります。
方法
アニメーションの AnimationTrack.Speed を変更します。速度の値が正の場合、速度はアニメーションを前にプレイし、速度の値が負の場合、速度はアニメーションを後ろにプレイします。0 は、停止します。
アニメーションプションの fadeTime パラメーターを使用して、 AnimationTrack.WeightCurrent が AnimationTrack.WeightTarget に到達するまでの時間を決定します。
指定された event が KeyframeMarker でヒットされたときに発動する animation を返します。
Class.AnimationTrack の次の AnimationTrack の時間位置を返します。
プレイする AnimationTrack 。 AnimationTrack を呼び出すと、指定されたフェイドタイム、重量、および速度でプレイします。
Class.AnimationTrack のプレイバックを呼び出すと、AnimationTrack のプレイバックの再生が停止し、Class.AnimationTrack の重量が指定された期間にわたって、アニメーションのスタイルが変更されます。
イベント
前のアニメーションループの終了後に次の更新で AnimationTrack ループを繰り返すと、発動します。
世界のどの場所にいても、AnimationTrack が完了したときにファイアを起動します。アニメーションはプレイを終了し、「フェイドアウト」が完了し、対象は中立ポーズになります。
Class.AnimationTrack の再生時に、デフォルトの名前でない Keyframe に到達するたびに発動します。
Class.AnimationTrack がプレイを終了するときにファイア。アニメーショントラックは、アニメーション「Class.AnimationTrack」が終了するまで、アニメーションを変更する可能性があります。AnimationTrack.Ended イベントを捕捉するには、Class.AnimationTrack.Ended イベントを使用してください。
プロパティ
Animation
この AnimationTrack を作成するために使用された AnimationTrack オブジェクト。1> Class.AnimationTrack1> を作成するには、4> Class.Animator:LoadAnimation()4> メソッドを使用して、7> Class.Animation7> オブジェク
コードサンプル
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
Class.AnimationTrack がプレイ中のときに true を返す読み取り専用プロパティ。
このプロパティは、開発者がアニメーションをプレイする前にアニメーションがすでにプレイされているかどうかをチェックするために使用できます (それは、再起動を引き起こす可能性があるため)。開発者が Humanoid または
コードサンプル
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
Class.AnimationTrack の長さ (秒単位) を返す読み取り専用プロパティ。これは、アニメーションが完全に読み込まれるまで 0 を返し、すぐに利用できるわけではないため、Class.AnimationTrack を使用することができません。
Class.AnimationTrack.Speed の Class.AnimationTrack が 1 で等しいと、アニメーションは AnimationTrack (秒単位) を完了するために必要な時間を取ります。
コードサンプル
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
このプロパティは、アニメーションが終了した後に繰り返されるかどうかを設定します。結果をプレイすると、アニメーションが終了した後に効果が出ます。
Class.AnimationTrack のループプロパティは、アニメーションエディタで設定される方法によってデフォルトで設定されます。しかし、このプロパティは変更可能であり、ゲームが実行されている間、AnimationTrack のアニメーションをリバース( Class.AnimationTrack
このプロパティは、開発者が同じアニメーションのループと非ループのバリアントを持つことができるようになります。たとえ、RobloRoblox(ロブロックス) に 2つのバージョンをアップロードする必要があっても。
コードサンプル
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 の優先度を設定します。これにより、複数のアニメーションを同時にプレイすると、このプロパティによって、1つのKeyframe 、Poses が別々にプレイされるようになります。
Class.AnimationTrack のプライオリティプロパティは、Class.AnimationTrack の公開元 から設定され、Enum.AnimationPriority を使用しています。これは、7つの優先レベルを使用しています:
- アクション4 (最高優先度)
- アクション3
- アクション2
- アクション
- 移動
- 待機
- コア (最低優先度)
アニメーションの優先度を設定するには、エディターまたはこのプロパティを通じてアニメーションを複数回再生することなくアニメーションをプレイすることができます。2つのプレイアニメーションが同じ方法でターゲットを動かす場合、最高優先度の AnimationTrack が表示されます。2つのアニ
このプロパティは、開発者が同じアニメーションを異なる優先度で再生できるようにし、Roblox に追加のバージョンをアップロードする必要はありません。
Speed
Class.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 が位置を更新した秒数を返します。これを特定のアニメーションの時間にトラックをジャンプさせるように設定できます。
TimePosition はアニメーションの特定のポイントに移動するように設定できますが、 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 に設定すると、重量は即座に変更されませんが、AnimationTrack.WeightTarget に移動します。これを行う時間は、アニメーションが再生されるとき、または重量が調整されるときに指定されたフェイドタイムパラメータによって決まります。
WeightCurrent は、AnimationTrack.WeightTarget とチェックすることで、必要な重量が達成されているかどうかを確認することができます。これらの値は、Class.AnimationTrack.WeightTarget と同じである必要がありません。WeightCurrent がターゲットの重量に到達したかどうかを確認するには、 Class.AnimationTrack.Distance の距
アニメーションの重量システムは、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 は、AnimationTrack の現在の重量を指定する読み取り専用プロパティです。デフォ
WeightCurrent は、AnimationTrack.WeightTarget とチェックすることで、必要な重量が達成されているかどうかを確認することができます。これらの値は、Class.AnimationTrack.WeightTarget と同じである必要がありません。WeightCurrent がターゲットの重量に到達したかどうかを確認するには、 Class.AnimationTrack.Distance の距
アニメーションの重量システムは、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 の初期速度は Class.AnimationTrack:Play にパラメータとして設定されます。しかし、トラックの速度は、AdjustSpeed を使用してプレイ中に変更できます。スピードが 1 であると、アニメーションの完了時間は Class.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 に移動します。これを行う時間は、アニメーションが再生されるとき、または重量が調整されるときに指定されたフェイドタイムパラメータによって決まります。
WeightCurrent は、AnimationTrack.WeightTarget とチェックすることで、必要な重量が達成されているかどうかを確認することができます。これらの値は、Class.AnimationTrack.WeightTarget と同じである必要がありません。WeightCurrent がターゲットの重量に到達したかどうかを確認するには、 Class.AnimationTrack.Distance の距
アニメーションの重量システムは、どのように AnimationTracks が同じ優先度で再生されているかを決
パラメータ
戻り値
コードサンプル
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 にヒットしたときのみ発動します。その違いにより、イベントが発動するタイミングをより多く制御できます。
この機能の詳細については、アニメーションイベント を参照してください。
キーフレームについて詳しくは、「もっと」
Keyframe 名前は、アニメーションを作成または編集する Roblox アニメーションエディタ で設定できます。ただし、既存のアニメーションをプレイする前に、Roblox Script によって設定されることはできません。
Keyframe 名前はユニークでなくてもよい。たとえ、Animation に 3つのキーフレームがある場合、この関数によって接続されたイベントは、これらのキーフレームの 1つが「エミットパーティクル」という名前で呼び出されるたびに発動します。
参照してください:
パラメータ
信号が作成されるための名前は、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.AnimationTrack の中の最初の Class.Keyframe の時間位置を返します。AnimationTrack の複数の共有名が同じ名前を持つ場合、最初のものがアニメーションに表示されます。
この関数は、無効なキーフレーム名 (例えば存在しない) で使用されている場合、または基本 Class.Animation がまだ読み込まれていません。これを処理するには、キーフレーム名のみを正しく使用し、アニメーションが読み込まれる前に基本 Class.Animation を読み込んでいることを確認してください。
アニメーションが読み込まれているかどうかを確認するには、AnimationTrack.Length がゼロより大きいかどうかを確認してください。
パラメータ
Class.Keyframe と関連付けられた名前。
戻り値
コードサンプル
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
Class.AnimationTrack:Play() が呼び出されると、トラックのアニメーションが再生され、トラックの重量が指定された重量から 0 に増加します (デフォルトは 1) 、指定されたフェイドタイム (デフォルトは 0.1) で開始します。
Class.AnimationTrack がプレイする速度は、速度パラメータ (デフォルトは 1) によって決まります。速度が 1 で等しいと、トラックが完了するのにかかる秒数は、トラックの AnimationTrack.Length プロパティと同等です。たとえば、2 の速度はトラックを 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)
Stop
Class.AnimationTrack のプレイバックを呼び出すと、AnimationTrack のプレイバックの再生が停止し、Class.AnimationTrack の重量が指定された期間にわたって、アニメーションのスタイルが変更されます。
たとえば、Stop が fadeTime の 2 秒で呼び出された場合、AnimationTrack の重量に到達するまで 2 秒かかります。これは、アニメーションの初期重量に関わらず、すべてのエフェクトが完了するまでに 2 秒かかることに注意してください。これはアニメーションの初期重量に関わらず、すべてのエフェクトが完了するまでに 2 秒かかることに注
このエフェクトをオーバーライドし、すぐにアニメーションを終了するために 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 に戻ったときにアクションを実行するか、Class.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
Class.AnimationTrack の再生がデフォルトの名前でない Keyframe に到達するたびに、「キーフレーム」という名前で実行されます。
このイベントは、アニメーションのプレディファインポイントにコードを実行することができます ( Keyframe 名によって設定)。これにより、Roblox アニメーションのデフォルト機能が拡張され、Sounds またはParticleEffects を別のポイントに追加する
Keyframe 名前はユニークでなくても構わりません。たとえ、アニメーションの 3つのキーフレームが "パーティクル" という名前であっても、キーフレームに到達したイベントは、それぞれのキーフレームの 1つが "パーティクル" という名前である場合に発生します。
Keyframe 名前は、アニメーションを作成または編集するときに Roblox アニメーションエディタに設定できます。ただし、既存のアニメーションをプレイする前に Script によって設定されることはできません。
パラメータ
Class.Keyframe の名前に到達しました。
Stopped
Class.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)