蹲伏动画Bug修复问询:蹲伏时按Shift调速异常问题
蹲伏奔跑Bug修复方案
问题现象
- 按下Left Ctrl进入蹲伏状态后,再按Left Shift,角色依然会获得奔跑速度
- 预期行为:蹲伏状态下按Shift,角色保持蹲伏速度(蹲伏动画无调速功能)
现有代码
local UIS = game:GetService('UserInputService'); local key = Enum.KeyCode.LeftShift local ctrl = Enum.KeyCode.LeftControl local pl = game.Players.LocalPlayer; local char = script.Parent or pl.CharacterAdded:Wait(); local hum = char:WaitForChild('Humanoid'); local Move = 'rbxassetid://14908666655' local Idle = 'rbxassetid://14908644962' local Crouching = false; local Holding = false; local HoldCrouching = false; local function ResetAnimation(boolean) if boolean then script.CrouchAnimation.AnimationId = Move else script.CrouchAnimation.AnimationId = Idle end end local function ChangeState(boolean) if boolean then hum.WalkSpeed = 8 hum:SetStateEnabled(Enum.HumanoidStateType.Jumping, false) local Anim = hum:LoadAnimation(script.CrouchAnimation) Anim:Play() else hum.WalkSpeed = 16 hum:SetStateEnabled(Enum.HumanoidStateType.Jumping, true) for _,i in ipairs(hum.Animator:GetPlayingAnimationTracks()) do if i and i.Name == 'CrouchAnimation' then i:Stop() end end end end script:WaitForChild('CrouchAnimation').Changed:Connect(function() if Crouching then for _,i in ipairs(hum.Animator:GetPlayingAnimationTracks()) do if i and i.Name == 'CrouchAnimation' then i:Stop() end end local Anim = hum:LoadAnimation(script.CrouchAnimation) Anim:Play() else for _,i in ipairs(hum.Animator:GetPlayingAnimationTracks()) do if i and i.Name == 'CrouchAnimation' then i:Stop() end end end end) UIS.InputBegan:Connect(function(Input, proc) if proc then return; end if Input then if Input.KeyCode == ctrl then if not Crouching then Crouching = true; ChangeState(true) elseif Crouching and not Holding then Crouching = false; ChangeState(false) end end end end) UIS.InputEnded:Connect(function(Input, proc) if proc then return; end if Input then if Input.KeyCode == ctrl then if Crouching and Holding then Crouching = false; ChangeState(false) end end end end) game:GetService('RunService').RenderStepped:Connect(function() if Crouching then if hum.MoveDirection.Magnitude > 0 then ResetAnimation(true) else ResetAnimation(false) end end end)
修复思路与修改代码
问题核心是未在蹲伏状态下屏蔽Shift的加速效果,需添加Shift状态跟踪,并在蹲伏时强制锁定行走速度。
修改后的完整代码:
local UIS = game:GetService('UserInputService'); local shiftKey = Enum.KeyCode.LeftShift local ctrlKey = Enum.KeyCode.LeftControl local pl = game.Players.LocalPlayer; local char = script.Parent or pl.CharacterAdded:Wait(); local hum = char:WaitForChild('Humanoid'); local MoveAnim = 'rbxassetid://14908666655' local IdleAnim = 'rbxassetid://14908644962' local isCrouching = false; local isSprinting = false; local baseWalkSpeed = 16; local crouchWalkSpeed = 8; local sprintWalkSpeed = 24; -- 原奔跑速度,可按需调整 local function ResetAnimation(isMoving) script.CrouchAnimation.AnimationId = isMoving and MoveAnim or IdleAnim end local function UpdateWalkSpeed() if isCrouching then hum.WalkSpeed = crouchWalkSpeed else hum.WalkSpeed = isSprinting and sprintWalkSpeed or baseWalkSpeed end end local function ChangeCrouchState(enableCrouch) isCrouching = enableCrouch if enableCrouch then hum:SetStateEnabled(Enum.HumanoidStateType.Jumping, false) local Anim = hum:LoadAnimation(script.CrouchAnimation) Anim:Play() else hum:SetStateEnabled(Enum.HumanoidStateType.Jumping, true) for _, track in ipairs(hum.Animator:GetPlayingAnimationTracks()) do if track.Name == 'CrouchAnimation' then track:Stop() end end end UpdateWalkSpeed() end script.CrouchAnimation.Changed:Connect(function() if isCrouching then for _, track in ipairs(hum.Animator:GetPlayingAnimationTracks()) do if track.Name == 'CrouchAnimation' then track:Stop() end end local Anim = hum:LoadAnimation(script.CrouchAnimation) Anim:Play() else for _, track in ipairs(hum.Animator:GetPlayingAnimationTracks()) do if track.Name == 'CrouchAnimation' then track:Stop() end end end end) UIS.InputBegan:Connect(function(Input, isProcessed) if isProcessed then return end if Input.KeyCode == ctrlKey then if not isCrouching then ChangeCrouchState(true) else ChangeCrouchState(false) end elseif Input.KeyCode == shiftKey then isSprinting = true UpdateWalkSpeed() end end) UIS.InputEnded:Connect(function(Input, isProcessed) if isProcessed then return end if Input.KeyCode == ctrlKey then ChangeCrouchState(false) elseif Input.KeyCode == shiftKey then isSprinting = false UpdateWalkSpeed() end end) game:GetService('RunService').RenderStepped:Connect(function() if isCrouching then local isMoving = hum.MoveDirection.Magnitude > 0 ResetAnimation(isMoving) -- 强制锁定蹲伏速度,避免其他逻辑意外修改 if hum.WalkSpeed ~= crouchWalkSpeed then hum.WalkSpeed = crouchWalkSpeed end end end)
关键修改点
- 新增状态变量:添加
isSprinting跟踪Shift按键状态,拆分速度常量便于维护 - 统一速度更新逻辑:创建
UpdateWalkSpeed函数,根据蹲伏和奔跑状态动态设置速度,确保蹲伏时优先使用蹲伏速度 - 屏蔽蹲伏时的奔跑效果:在
RenderStepped中强制检查并锁定蹲伏状态下的行走速度,彻底避免Shift影响 - 优化代码可读性:重命名变量(如
Crouching→isCrouching),拆分常量,逻辑更清晰
内容的提问来源于stack exchange,提问作者arthur
相关产品推荐
相关产品推荐

