AnimationTrack

非推奨を表示

*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。

作成できません

Animator 上のアニメーションの再生を制御します。このオブジェクトは作成できません、代わりに Animator:LoadAnimation() メソッドによって返されます。

概要

プロパティ

  • 読み取り専用
    複製されていません
    並列読み取り

    この Animation を作成するために使用されたオブジェクト AnimationTrack

  • 読み取り専用
    複製されていません
    並列読み取り

    AnimationTrack が再生されているときに真を返す読み込み専用プロパティ。

  • 読み取り専用
    複製されていません
    並列読み取り

    AnimationTrack の長さ (秒) を返す読み取り専用プロパティ。アニメーションが完全にロードされるまで 0 が返され、すぐに利用できない可能性があります。

  • 並列読み取り

    アニメーションが終了後に繰り返されるかどうかを設定します。結果を再生している間に変更された場合、アニメーションが終了すると効果が発生します。

  • AnimationTrack の優先順位を設定します。これが設定されている内容に応じて、複数のアニメーションを一度に再生すると、このプロパティを調べて、どの Keyframe Poses が互いに再生されるべきかを判断します。

  • 読み取り専用
    複製されていません
    並列読み取り

    AnimationTrack の速度は、AnimationTrack の現在の再生速度を提供する読み取り専用プロパティです。これにはデフォルト値 1があります。スピードが 1 と同じとき、アニメーションが完了するまでに必要な時間量は AnimationTrack.Length (秒) です。

  • 複製されていません
    並列読み取り

    AnimationTrack がソースアニメーションを再生することで時間内の位置を返します。トラックをアニメーションの特定の瞬間にジャンプさせるように設定できます。

  • 読み取り専用
    複製されていません
    並列読み取り

    AnimationTrack の現在の重量を提供する読み取り専用プロパティ。デフォルト値は 1 です。

  • 読み取り専用
    複製されていません
    並列読み取り

    AnimationTrack の現在の重量を返す読み取り専用プロパティ。

方法

イベント

  • 前のアニメーションループの終了後、次のアップデートで AnimationTrack ループが発生するとき

  • AnimationTrack が完全に世界の何かを移動するのを終えたときに発火します。アニメーションは再生が終わり、「フェードアウト」が終わり、主題は中立なポーズになっています。

  • 再生の度に、AnimationTrack のプレイバックがデフォルトの名前を持っていないKeyframe に到達するたびに発火します - "キーフレーム"。

  • AnimationTrack が再生を終了すると発火します。アニメーショントラックは、アニメーションが "消えて" いる間、対象をアニメーションする可能性があります。アニメーショントラックが世界の何かを完全に移動するのをキャプチャするには、AnimationTrack.Ended イベントを使用します。

プロパティ

Animation

読み取り専用
複製されていません
並列読み取り

この Animation を作成するために使用されたオブジェクト AnimationTrack 。To create an AnimationTrack , you must load an Animation オブジェクト onto an Animator using the 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.

Listen For New Animations

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.

AnimationTrack IsPlaying

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 が返され、すぐに利用できない可能性があります。

When the AnimationTrack.Speed of an 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).

Playing Animation for a Specific Duration

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 に 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.

Animation Looping

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.

Play AnimationTrack for a Number of Loops

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)
並列読み取り

このプロパティは、AnimationTrack の優先順位を設定します。これが設定されている内容に応じて、複数のアニメーションを一度に再生すると、このプロパティを調べて、どの Keyframe Poses が互いに再生されるべきかを判断します。

優先度プロパティは、AnimationTrack にデフォルトで設定され、Studio のアニメーションエディタ から公開されます。それは Enum.AnimationPriority を使用し、7つの優先レベルを持っています:

  1. アクション4 (最優先)
  2. アクション3
  3. アクション2
  4. 行動
  5. 移動
  6. 待機
  7. コア (最低優先度)

エディタまたはこのプロパティを通じて適切にアニメーションの優先順位を設定すると、複数のアニメーションを衝突させずに再生できます。2つのプレイアニメーションがターゲットに同じ肢を異なる方法で動かすよう指示する場合、最優先の AnimationTrack が表示されます。両方のアニメーションが同じ優先順位を持つ場合、トラックの重量がアニメーションを結合するのに使用されます。

このプロパティはまた、開発者が RobloRoblox(ロブロックス) に追加バージョンをアップロードする必要なく、異なる優先順位で同じアニメーションを再生できるようにします。

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.

Animation 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).

Playing Animation for a Specific Duration

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() と組み合わせて、アニメーションを指定したポイントで凍結することもできます (速度を 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.

Freeze Animation at Position

function freezeAnimationAtTime(animationTrack, timePosition)
if not animationTrack.IsPlaying then
-- Play the animation if it is not playing
animationTrack:Play()
end
-- Set the speed to 0 to freeze the animation
animationTrack:AdjustSpeed(0)
-- Jump to the desired TimePosition
animationTrack.TimePosition = timePosition
end
function freezeAnimationAtPercent(animationTrack, percentagePosition)
if not animationTrack.IsPlaying then
-- Play the animation if it is not playing
animationTrack:Play()
end
-- Set the speed to 0 to freeze the animation
animationTrack:AdjustSpeed(0)
-- Jump to the desired TimePosition
animationTrack.TimePosition = (percentagePosition / 100) * animationTrack.Length
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
freezeAnimationAtTime(animationTrack, 0.5)
freezeAnimationAtPercent(animationTrack, 50)

WeightCurrent

読み取り専用
複製されていません
並列読み取り

重量が AnimationTrack に設定されると、すぐに変更されるのではなく、WeightCurrent から AnimationTrack.WeightTarget に移動します。これを行う時間は、アニメーションが再生されたり、重量が調整されたときに渡される fadeTime パラメータによって決まります。

WeightCurrent は、AnimationTrack.WeightTarget に対してチェックして、希望する重量に達したかどうかを確認できます。これらの値は、== 演算子との同等をチェックする必要はありません、これらの値の両方が浮動であるためです。WeightCurrent がターゲット重量に達したかどうかを確認するには、以下のコードサンプルで、それらの値の間の距離が十分に小さいかどうかを確認することをお勧めします (参照してください:下のコードサンプル)。

アニメーション重量システムは、どのように AnimationTracks 同じ優先度で再生されるかを決定するために使用されます。デフォルトの重量は 1 であり、重量ゼロの 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.

WeightCurrent and WeightTarget

local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
while not localPlayer.Character do
task.wait()
end
local character = localPlayer.Character
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation1 = Instance.new("Animation")
animation1.AnimationId = "rbxassetid://507770453"
local animation2 = Instance.new("Animation")
animation2.AnimationId = "rbxassetid://507771019"
task.wait(3) -- 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 に設定されると、すぐに変更されるのではなく、WeightCurrent から AnimationTrack.WeightTarget に移動します。これを行う時間は、アニメーションが再生されたり、重量が調整されたときに渡される fadeTime パラメータによって決まります。

WeightCurrent は、AnimationTrack.WeightTarget に対してチェックして、希望する重量に達したかどうかを確認できます。これらの値は、== 演算子との同等をチェックする必要はありません、これらの値の両方が浮動であるためです。WeightCurrent がターゲット重量に達したかどうかを確認するには、以下のコードサンプルで、それらの値の間の距離が十分に小さいかどうかを確認することをお勧めします (参照してください:下のコードサンプル)。

アニメーション重量システムは、どのように AnimationTracks 同じ優先度で再生されるかを決定するために使用されます。デフォルトの重量は 1 であり、重量ゼロの 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.

WeightCurrent and WeightTarget

local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
while not localPlayer.Character do
task.wait()
end
local character = localPlayer.Character
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation1 = Instance.new("Animation")
animation1.AnimationId = "rbxassetid://507770453"
local animation2 = Instance.new("Animation")
animation2.AnimationId = "rbxassetid://507771019"
task.wait(3) -- 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() でパラメータとして設定されます。しかし、トラックの速度は再生中に調整速度を使用して変更できます。スピードが 1 と同じとき、アニメーションが完了するまでに必要な時間量は AnimationTrack.Length (秒) です。

調整されるとき、トラックが再生する実際の時間は、長さを速度で割って計算できます。速度は単位のない量です。

スピードを使用して、アニメーションの長さを異なるゲームプレイイベント(例えば、能力のリチャージ)にリンクでき、同じアニメーションの異なるバリアントをアップロードする必要はありません。

パラメータ

speed: number

アニメーションが変更される再生速度。

既定値: 1

戻り値

()

コードサンプル

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).

Playing Animation for a Specific Duration

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.

Animation 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.WeightCurrentAnimationTrack.WeightTarget に到達するまでの時間を決定します。

重量が AnimationTrack に設定されると、すぐに変更されるのではなく、WeightCurrent から AnimationTrack.WeightTarget に移動します。これを行う時間は、アニメーションが再生されたり、重量が調整されたときに渡される fadeTime パラメータによって決まります。

WeightCurrent は、AnimationTrack.WeightTarget に対してチェックして、希望する重量に達したかどうかを確認できます。これらの値は、== 演算子との同等をチェックする必要はありません、これらの値の両方が浮動であるためです。WeightCurrent がターゲット重量に達したかどうかを確認するには、以下のコードサンプルで、それらの値の間の距離が十分に小さいかどうかを確認することをお勧めします (参照してください:下のコードサンプル)。

アニメーション重量システムは、どのように AnimationTracks 同じ優先度で再生されるかを決定するために使用されます。デフォルトの重量は 1 であり、重量ゼロの AnimationTrack では動作が表示されません。任意の時点で表示されるポーズは、すべての Poses および各 AnimationTrack の重み平均によって決定されます。以下では、実践でのアニメーションブレンドの例を見てください。ほとんどの場合、ブレンドアニメーションは必要ありません、そして AnimationTrack.Priority を使用する方が適しています。

パラメータ

weight: number

アニメーションが変更される重量。

既定値: 1
fadeTime: number

アニメーションが古い重量と新しい重量の間で消える時間の長さ。

既定値: 0.100000001

戻り値

()

コードサンプル

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).

AnimationTrack Change Weight

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 を返しますが、指定された KeyframeMarkeranimation でヒットしたときにのみ発動します。違いにより、イベントが発射するタイミングをより制御できます。

この機能の使用について詳しくは、 アニメーションイベントアニメーションエディタ 記事で見てください。

キーフレームについてさらに詳しく

Keyframe 名は、アニメーションを作成または編集するときに Roblox アニメーションエディタ で設定できます。しかし、それはプレイする前に既存のアニメーションに Script で設定できません。

Keyframe 名は唯一である必要はありません。たとえば、Animation に「EmitParticles」という3つのキーフレームがある場合、この関数によって返された接続イベントは、これらのキーフレームのうちの1つに到達するたびに発射します。

参照してください:

パラメータ

name: string

シグナルの名前が作成されているのは、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.

Listening to Keyframe Markers

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)

GetTargetInstance

パラメータ

name: string
既定値: ""

戻り値

GetTargetNames


戻り値

GetTimeOfKeyframe

指定された名前の最初の Keyframe の時間位置を AnimationTrack に返します。複数の Keyframes が同じ名前を共有する場合、アニメーションで最も早いものを返します。

この機能は、無効なキーフレーム名 (例えば存在しないもの) で使用したり、基本の Animation がまだロードされていない場合など、エラーを返すことがあります。これを解決するには、正しいキーフレーム名のみが使用され、この関数を呼び出す前にアニメーションがロードされていることを確認してください。

アニメーションがロードされたかどうかを確認するには、AnimationTrack.Length がゼロより大きいかどうかを確認してください。

パラメータ

keyframeName: string

見つかる Keyframe に関連する名前。

既定値: ""

戻り値

時間、秒で、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.

Jump To Keyframe

local function jumpToKeyframe(animationTrack, keyframeName)
local timePosition = animationTrack:GetTimeOfKeyframe(keyframeName)
if not animationTrack.IsPlaying then
animationTrack:Play()
end
animationTrack.TimePosition = timePosition
end
local ANIMATION_ID = 0
local KEYFRAME_NAME = "Test"
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://" .. ANIMATION_ID
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
jumpToKeyframe(animationTrack, KEYFRAME_NAME)

Play

()

When AnimationTrack:Play() が呼び出されると、トラックのアニメーションが再生開始し、アニメーションの重量が 0 から指定された重量に増加します (デフォルトは 1) 指定されたフェードタイム (デフォルトは 0.1) にわたって。

AnimationTrack が再生する速度は、速度パラメータ (デフォルトは 1) によって決まります。スピードが 1 と同じになると、トラックが完了するのにかかる秒数は、トラックの AnimationTrack.Length プロパティと同じです。例えば、2の速度は、トラックが2倍の速さで再生するようになります。

アニメーションの重量と速度も、AnimationTrack:AdjustWeight() および AnimationTrack:AdjustSpeed() メソッドを使用してアニメーションが再生開始後に変更できます。

開発者が AnimationTrack.TimePosition を使用して特定のポイントでアニメーションを開始したい場合、これが行われる前にアニメーションが再生されることが重要です。

パラメータ

fadeTime: number

アニメーションの重量が消える時間の長さ。

既定値: 0.100000001
weight: number

アニメーションが再生される重量。

既定値: 1
speed: number

アニメーションの再生速度。

既定値: 1

戻り値

()

コードサンプル

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).

Playing Animation for a Specific Duration

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.

Freeze Animation at Position

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

()

パラメータ

name: string
既定値: ""
target: Instance
既定値: ""

戻り値

()

Stop

()

停止する AnimationTrack 。呼び出されると、アニメーションの重量は、オプションの fadeTime パラメータによって指定された期間にわたってゼロに移動します。たとえば、Stop()fadeTime2 で呼び出された場合、トラックの重量がゼロに達して効果が完全に終了わるまでには、2秒かかります。これは、アニメーションの最初の重量にかかわらず、発生することに注意してください。

この効果をオーバーライドして、すぐにアニメーションを終了するために の を使用することは推奨されません。これは、関節がそのプレースに凍結するためです。すぐに終了する必要がある場合は、Motor.MaxVelocityMotor6Ds が十分に高いことを確認してください。彼らが適切にスナップできるように。

パラメータ

fadeTime: number

アニメーションの重量が消える時間、秒単位で。

既定値: 0.100000001

戻り値

()

コードサンプル

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).

AnimationTrack Stop

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.

Play AnimationTrack for a Number of Loops

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.

AnimationTrack Ended

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 名によって設定)。これにより、Roblox アニメーションのデフォルト機能が、アニメーションの異なるポイントに Sounds または ParticleEffects を追加することで拡張できます。

Keyframe 名は唯一である必要はありません。たとえば、アニメーションに「パーティクル」と呼ばれる 3つのキーフレームがある場合、KeyframeReached イベントは、これらのキーフレームの1つが達成されるたびに発射します。

Keyframe 名は、アニメーションを作成または編集するときに Roblox アニメーションエディタで設定できます。しかし、それはプレイする前に既存のアニメーションの Script によって設定できません。

パラメータ

keyframeName: string

名前の Keyframe に到達。


Stopped

ファイアは、AnimationTrack が再生を終了するたびに発生します。

このイベントには複数の使用があります。続行する前に、AnimationTrack が停止するまで待つことができます (例えば、連続してアニメーションを再生するようにチェーンする場合)。アニメーション再生中に作成されたすべての Instances をクリーンアップするのにも使用できます。


コードサンプル

The function in this code sample will play an animationTrack and yield until it has stopped, before printing.

AnimationTrack Stopped

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)