使用动画

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

一旦你创建了一个动画,你需要使用脚本将其包含到你的体验中。你可以从脚本中手动播放动画或为玩家角色替换默认动画

从脚本播放动画

在某些情况下,你需要直接在脚本内部播放动画,例如当用户按下某个键或拾取特殊物品时。

人形

要在包含 Humanoid 对象的模型上播放动画,例如典型的可玩角色,请遵循以下基本模式:

  1. 确保本地玩家的 Humanoid 包含一个 Animator 对象。
  2. 创建一个新的 Animation 实例,并正确赋值 AnimationId
  3. 通过 Animator:LoadAnimation() 加载动画以创建 AnimationTrack
  4. 使用 AnimationTrack:Play() 播放该轨道。

例如,以下 LocalScript,当放置在 StarterPlayerScripts 中时,会在玩家角色上加载一个“踢腿”动画并播放。该脚本还利用了 GetMarkerReachedSignal() 方法来检测特定的 动画事件

LocalScript - 在玩家角色上播放自定义动画

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
-- 确保角色的人形包含一个 "Animator" 对象
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
-- 创建一个新的 "Animation" 实例并分配动画资产 ID
local kickAnimation = Instance.new("Animation")
kickAnimation.AnimationId = "rbxassetid://2515090838"
-- 将动画加载到动画控制器中
local kickAnimationTrack = animator:LoadAnimation(kickAnimation)
-- 如果为该动画定义了命名事件,将其连接到 "GetMarkerReachedSignal()"
kickAnimationTrack:GetMarkerReachedSignal("KickEnd"):Connect(function(paramString)
print(paramString)
end)
task.wait(4)
-- 播放动画轨道
kickAnimationTrack:Play()

非人形

要在不包含 Humanoid 的模型上播放动画,必须创建一个 AnimationController 及其子级 Animator。例如,以下 Script(假定为模型的直接子级)加载一个“踢腿”动画并播放它。

脚本 - 在角色模型上播放自定义动画

local rig = script.Parent
-- 创建一个新的 "Animation" 实例并分配动画资产 ID
local kickAnimation = Instance.new("Animation")
kickAnimation.AnimationId = "rbxassetid://2515090838"
-- 创建一个新的 "AnimationController" 和 "Animator"
local animationController = Instance.new("AnimationController")
animationController.Parent = rig
local animator = Instance.new("Animator")
animator.Parent = animationController
-- 将动画加载到动画控制器中
local kickAnimationTrack = animator:LoadAnimation(kickAnimation)
-- 如果为该动画定义了命名事件,将其连接到 "GetMarkerReachedSignal()"
kickAnimationTrack:GetMarkerReachedSignal("KickEnd"):Connect(function(paramString)
print(paramString)
end)
task.wait(4)
-- 播放动画轨道
kickAnimationTrack:Play()

替换默认动画

默认情况下,Roblox 玩家角色包括常见动画,如奔跑、攀爬、游泳和跳跃。你可以用来自目录的动画或自己的自定义动画替换这些默认动画

  1. 以以下方式获取新动画的 资产 ID

  2. 资源管理器 窗口中,添加一个新的 ScriptServerScriptService

    1. 悬停在 ServerScriptService 上并单击 ⊕ 按钮。
    2. 从上下文菜单中插入一个 脚本
  3. 在新脚本中粘贴以下代码:

    脚本 - 替换默认角色动画

    local Players = game:GetService("Players")
    local function onCharacterAdded(character)
    -- 获取人形上的动画控制器
    local humanoid = character:WaitForChild("Humanoid")
    local animator = humanoid:WaitForChild("Animator")
    -- 停止所有动画轨道
    for _, playingTrack in animator:GetPlayingAnimationTracks() do
    playingTrack:Stop(0)
    end
    local animateScript = character:WaitForChild("Animate")
    --animateScript.run.RunAnim.AnimationId = "rbxassetid://"
    --animateScript.walk.WalkAnim.AnimationId = "rbxassetid://"
    --animateScript.jump.JumpAnim.AnimationId = "rbxassetid://"
    --animateScript.idle.Animation1.AnimationId = "rbxassetid://"
    --animateScript.idle.Animation2.AnimationId = "rbxassetid://"
    --animateScript.fall.FallAnim.AnimationId = "rbxassetid://"
    --animateScript.swim.Swim.AnimationId = "rbxassetid://"
    --animateScript.swimidle.SwimIdle.AnimationId = "rbxassetid://"
    --animateScript.climb.ClimbAnim.AnimationId = "rbxassetid://"
    end
    local function onPlayerAdded(player)
    player.CharacterAppearanceLoaded:Connect(onCharacterAdded)
    end
    Players.PlayerAdded:Connect(onPlayerAdded)
  4. 对于每一行引用 默认角色动画 的代码,取消注释并在 rbxassetid:// 后粘贴替换 ID。例如,要将默认奔跑动画更改为Ninja Run 变体:

    脚本 - 替换默认角色动画

    local Players = game:GetService("Players")
    local function onCharacterAdded(character)
    -- 获取人形上的动画控制器
    local humanoid = character:WaitForChild("Humanoid")
    local animator = humanoid:WaitForChild("Animator")
    -- 停止所有动画轨道
    for _, playingTrack in animator:GetPlayingAnimationTracks() do
    playingTrack:Stop(0)
    end
    local animateScript = character:WaitForChild("Animate")
    animateScript.run.RunAnim.AnimationId = "rbxassetid://656118852"
    --animateScript.walk.WalkAnim.AnimationId = "rbxassetid://"
    --animateScript.jump.JumpAnim.AnimationId = "rbxassetid://"
    --animateScript.idle.Animation1.AnimationId = "rbxassetid://"
    --animateScript.idle.Animation2.AnimationId = "rbxassetid://"
    --animateScript.fall.FallAnim.AnimationId = "rbxassetid://"
    --animateScript.swim.Swim.AnimationId = "rbxassetid://"
    --animateScript.swimidle.SwimIdle.AnimationId = "rbxassetid://"
    --animateScript.climb.ClimbAnim.AnimationId = "rbxassetid://"
    end
    local function onPlayerAdded(player)
    player.CharacterAppearanceLoaded:Connect(onCharacterAdded)
    end
    Players.PlayerAdded:Connect(onPlayerAdded)

设置动画权重

你可以为同一个动作使用多个动画。例如,在替换默认动画的代码示例中,有两个 idle 变体。

当多个动画存在于角色状态中时,Animate 脚本随机选择一个动画进行播放,但你可以通过在以下公式下设置动画的 Weight 值来影响结果:

  • 动画权重 / 所有状态动画的总权重

在以下示例中,idle.Animation1 将在角色静止时播放 ⅓ 的时间,而 idle.Animation2 将播放 ⅔ 的时间。

脚本 - 替换默认角色动画

animateScript.idle.Animation1.AnimationId = "rbxassetid://656117400"
animateScript.idle.Animation2.AnimationId = "rbxassetid://656118341"
animateScript.idle.Animation1.Weight.Value = 5
animateScript.idle.Animation2.Weight.Value = 10

动画参考

默认角色动画

以下表格包含所有可以替换目录动画或你自己的自定义动画的默认角色动画。请注意,Idle 有两个变体,你可以为它们设置权重以使其播放更频繁或更少。

角色动作Animate 脚本引用
奔跑animateScript.run.RunAnim.AnimationId
行走animateScript.walk.WalkAnim.AnimationId
跳跃animateScript.jump.JumpAnim.AnimationId
待机

animateScript.idle.Animation1.AnimationId
animateScript.idle.Animation2.AnimationId

坠落animateScript.fall.FallAnim.AnimationId
游泳

animateScript.swim.Swim.AnimationId

游泳(待机)

animateScript.swimidle.SwimIdle.AnimationId

攀爬animateScript.climb.ClimbAnim.AnimationId

目录动画

使用角色动画包来替换默认动画时,请使用以下各自资产 ID 的引用。例如,如果你想应用Ninja Jump 动画,请使用 656117878。请注意,Idle 有多个变体。

宇航员
奔跑
891636393
行走
891636393
跳跃
891627522
待机
891621366, 891633237, 1047759695
坠落
891617961
游泳
891639666
游泳(待机)
891663592
攀爬
891609353
泡泡
奔跑
910025107
行走
910034870
跳跃
910016857
待机
910004836, 910009958, 1018536639
坠落
910001910
游泳
910028158
游泳(待机)
910030921
攀爬
909997997
卡通
奔跑
742638842
行走
742640026
跳跃
742637942
待机
742637544, 742638445, 885477856
坠落
742637151
游泳
742639220
游泳(待机)
742639812
攀爬
742636889
长者
奔跑
845386501
行走
845403856
跳跃
845398858
待机
845397899, 845400520, 901160519
坠落
845396048
游泳
845401742
游泳(待机)
845403127
攀爬
845392038
骑士
奔跑
657564596
行走
657552124
跳跃
658409194
待机
657595757, 657568135, 885499184
坠落
657600338
游泳
657560551
游泳(待机)
657557095
攀爬
658360781
升空
奔跑
616010382
行走
616013216
跳跃
616008936
待机
616006778, 616008087, 886862142
坠落
616005863
游泳
616011509
游泳(待机)
616012453
攀爬
616003713
法师
奔跑
707861613
行走
707897309
跳跃
707853694
待机
707742142, 707855907, 885508740
坠落
707829716
游泳
707876443
游泳(待机)
707894699
攀爬
707826056
忍者
奔跑
656118852
行走
656121766
跳跃
656117878
待机
656117400, 656118341, 886742569
坠落
656115606
游泳
656119721
游泳(待机)
656121397
攀爬
656114359
海盗
奔跑
750783738
行走
750785693
跳跃
750782230
待机
750781874, 750782770, 885515365
坠落
750780242
游泳
750784579
游泳(待机)
750785176
攀爬
750779899
机器人
奔跑
616091570
行走
616095330
跳跃
616090535
待机
616088211, 616089559, 885531463
坠落
616087089
游泳
616092998
游泳(待机)
616094091
攀爬
616086039
Rthro
奔跑
2510198475
行走
2510202577
跳跃
2510197830
待机
2510197257, 2510196951, 3711062489
坠落
2510195892
游泳
2510199791
游泳(待机)
2510201162
攀爬
2510192778
时尚
奔跑
616140816
行走
616146177
跳跃
616139451
待机
616136790, 616138447, 886888594
坠落
616134815
游泳
616143378
游泳(待机)
616144772
攀爬
616133594
超级英雄
奔跑
616117076
行走
616122287
跳跃
616115533
待机
616111295, 616113536, 885535855
坠落
616108001
游泳
616119360
游泳(待机)
616120861
攀爬
616104706
玩具
奔跑
782842708
行走
782843345
跳跃
782847020
待机
782841498, 782845736, 980952228
坠落
782846423
游泳
782844582
游泳(待机)
782845186
攀爬
782843869
吸血鬼
奔跑
1083462077
行走
1083473930
跳跃
1083455352
待机
1083445855, 1083450166, 1088037547
坠落
1083443587
游泳
1083464683
游泳(待机)
1083467779
攀爬
1083439238
狼人
奔跑
1083216690
行走
1083178339
跳跃
1083218792
待机
1083195517, 1083214717, 1099492820
坠落
1083189019
游泳
1083222527
游泳(待机)
1083225406
攀爬
1083182000
僵尸
奔跑
616163682
行走
616168032
跳跃
616161997
待机
616158929, 616160636, 885545458
坠落
616157476
游泳
616165109
游泳(待机)
616166655
攀爬
616156119