使用动画

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

一旦你创建了动画,你需要使用脚本将其包含在你的游戏中。你可以选择从脚本手动播放动画替换玩家角色的默认动画

从脚本播放动画

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

人形

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

  1. 确保本地玩家的 Humanoid 包含一个 Animator 对象。
  2. 创建一个具有正确 AnimationId 的新 Animation 实例。
  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()
-- 确保角色的 humanoid 包含 "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(假定为模型的直接子项)加载“踢腿”动画并播放。

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. 资源管理器 窗口中,向 ServerScriptService 添加一个新的 Script

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

    Script - 替换默认角色动画

    local Players = game:GetService("Players")
    local function onCharacterAdded(character)
    -- 获取 humanoid 上的动画器
    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。例如,要将默认跑步动画改为 忍者 跑 变体:

    Script - 替换默认角色动画

    local Players = game:GetService("Players")
    local function onCharacterAdded(character)
    -- 获取 humanoid 上的动画器
    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 会播放 ⅔ 的时间。

Script - 替换默认角色动画

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。例如,如果您想应用 忍者 跳跃 动画,请使用 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
©2026 Roblox Corporation、Roblox、Roblox 标志及 Powering Imagination 是我们在美国及其他国家或地区的注册与未注册商标。