You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Roblox Studio飞行模式下禁用默认R15坠落动画?

问题解决:Roblox飞行模式下静止时替换坠落动画为Idle动画

核心问题原因

你的脚本未干预Humanoid的状态控制,飞行模式下角色仍处于Falling状态,导致Roblox自动播放默认坠落动画。同时,当前逻辑仅在按键停止时关闭移动动画,未主动播放Idle动画覆盖默认行为。

修改后的完整脚本

local UIS = game:GetService("UserInputService")

local last = tick()
local isFlying = false

local flyUpSpeed = 16
local flyDownSpeed = 16

local char = script.Parent
local humanoid = char:WaitForChild("Humanoid")
local root = char:WaitForChild("HumanoidRootPart")

-- 提前加载所有需要的动画,避免重复创建实例
local anims = {
    W = humanoid:LoadAnimation(Instance.new("Animation", script)),
    A = humanoid:LoadAnimation(Instance.new("Animation", script)),
    S = humanoid:LoadAnimation(Instance.new("Animation", script)),
    D = humanoid:LoadAnimation(Instance.new("Animation", script)),
    Idle = humanoid:LoadAnimation(Instance.new("Animation", script))
}
-- 设置动画ID
anims.W.AnimationId = "rbxassetid://16864466808"
anims.A.AnimationId = "rbxassetid://16864540911"
anims.S.AnimationId = "rbxassetid://16864534139"
anims.D.AnimationId = "rbxassetid://16864446697"
anims.Idle.AnimationId = "rbxassetid://这里替换成你的Idle动画ID" -- 替换为实际的Idle动画ID

local currentPlayingAnim = nil

-- 停止当前播放的动画
local function stopCurrentAnim()
    if currentPlayingAnim then
        currentPlayingAnim:Stop()
        currentPlayingAnim = nil
    end
end

-- 播放指定动画
local function playAnim(anim)
    stopCurrentAnim()
    currentPlayingAnim = anim
    anim:Play()
end

function flight(isFlying)
    if isFlying then
        -- 飞行模式下禁用坠落、跳跃状态,切换到飞行状态
        humanoid:SetStateEnabled(Enum.HumanoidStateType.Falling, false)
        humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
        humanoid:ChangeState(Enum.HumanoidStateType.Flying)
        
        local bv = Instance.new("BodyVelocity")
        bv.MaxForce = Vector3.new(0, 1, 0) * 30000
        bv.Velocity = Vector3.new(0, 0, 0)
        bv.Name = "FlightVelocity"
        bv.Parent = root
        
        -- 进入飞行后默认播放Idle动画
        playAnim(anims.Idle)
    else
        -- 退出飞行恢复Humanoid默认状态
        humanoid:SetStateEnabled(Enum.HumanoidStateType.Falling, true)
        humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
        humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
        
        local bv = root:FindFirstChild("FlightVelocity")
        if bv then 
            bv:Destroy()
        end
        
        -- 停止自定义动画,恢复默认行为
        stopCurrentAnim()
    end
end

UIS.InputBegan:Connect(function(input, gameprocessed)
    if gameprocessed then return end
    
    if input.KeyCode == Enum.KeyCode.Space then    
        if (tick() - last) < 0.5 then
            isFlying = not isFlying
            flight(isFlying)
        elseif isFlying then 
            local bv = root:FindFirstChild("FlightVelocity")
            if bv then 
                bv.Velocity = Vector3.new(0, 1, 0) * flyUpSpeed
            end
        end
        last = tick()
    end
    
    if input.KeyCode == Enum.KeyCode.LeftControl then 
        if isFlying then
            local bv = root:FindFirstChild("FlightVelocity")
            if bv then 
                bv.Velocity = Vector3.new(0, -1, 0) * flyDownSpeed
            end
        end
    end
    
    if isFlying then 
        -- 按下WASD时播放对应移动动画
        if input.KeyCode == Enum.KeyCode.W then 
            playAnim(anims.W)
        elseif input.KeyCode == Enum.KeyCode.A then 
            playAnim(anims.A)
        elseif input.KeyCode == Enum.KeyCode.S then 
            playAnim(anims.S)
        elseif input.KeyCode == Enum.KeyCode.D then 
            playAnim(anims.D)
        end
    end
end)

UIS.InputEnded:Connect(function(input,gameprocessed)
    if gameprocessed then return end

    if isFlying then
        -- 无移动键按下时切换回Idle动画
        local hasMovementKeyDown = UIS:IsKeyDown(Enum.KeyCode.W) or UIS:IsKeyDown(Enum.KeyCode.A) 
            or UIS:IsKeyDown(Enum.KeyCode.S) or UIS:IsKeyDown(Enum.KeyCode.D)
        
        if not hasMovementKeyDown then
            playAnim(anims.Idle)
        end
    end
    
    if input.KeyCode == Enum.KeyCode.Space or input.KeyCode == Enum.KeyCode.LeftControl then
        if isFlying then
            local bv = root:FindFirstChild("FlightVelocity")
            if bv then 
                bv.Velocity = Vector3.new(0,0,0)
            end
        end
    end
end)

关键改动说明

  1. 提前加载动画资源:避免每次按键创建新实例,提升性能并简化管理。
  2. 控制Humanoid状态:飞行模式下禁用Falling状态,切换到Flying状态,从根源阻止默认坠落动画播放。
  3. 主动播放Idle动画:进入飞行模式和松开所有移动键时,主动播放自定义Idle动画覆盖默认行为。
  4. 统一动画切换逻辑:封装停止/播放函数,确保同一时间仅一个动画运行,避免冲突。

内容的提问来源于stack exchange,提问作者Nemesis _343

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 07:20:07