Roblox Studio冲刺脚本异常求助:前两次失效+脚部加速异常
Roblox冲刺功能问题修复方案
问题描述
在Roblox Studio开发冲刺功能时遇到两个核心问题:
- 前两次冲刺无法正常生效,第三次才恢复正常;
- 冲刺结束切换至静止或行走状态时,角色脚部会出现大幅加速的情况。
临时调整冲刺时长(duration)和Velocity参数只能掩盖问题,需要根本解决办法。
原代码
--Servicios local player = game:GetService("Players").LocalPlayer local repStorage = game:GetService("ReplicatedStorage") local UIS = game:GetService("UserInputService") local runservice = game:GetService("RunService") local debris = game:GetService("Debris") local character = player.Character or player.CharacterAdded:Wait() local loadedAnimation local fxFolder = repStorage.Fx local dustFx = fxFolder.DustFx local smokeFx = fxFolder.SmokeFx -- Configuracion Dash local key = Enum.KeyCode.Q -- Tecla para hacer el Dash local velocity = 20000 -- velocidad del dash local debounce = false local cooldown = 0.20 -- Cooldown despues del Dash local duration = 0.25 -- duracion del dash local sideDash = false local DashLeft = script:WaitForChild("DashLeft") local DashRight = script:WaitForChild("DashRight") --Variables para DustFX local humanoid = character:WaitForChild("Humanoid") --Partes donde se adjunatara el efecto local lfoot = character:WaitForChild("LeftFoot") --LeftLowerLeg / LeftFoot local rfoot = character:WaitForChild("RightFoot") --RightLowerLeg / RightFoot --Aca las Particulas del efecto local fx1 = game.ReplicatedStorage.Dash.Fx.DusFX.Dust:Clone() --Fx.DusFX.ef:Clone() fx1.Parent = rfoot local fx2 = game.ReplicatedStorage.Dash.Fx.DusFX.Dust:Clone() fx2.Parent = lfoot fx1.Enabled = false fx2.Enabled = false local function UserInputButton (input, Keyboard) --si esta suando el teclado para escribir en el chat salgo if Keyboard == true then return end --si no esta suando el teclado para escribir continuo if input.KeyCode == key then Dash() end end UIS.InputBegan:Connect(UserInputButton) function Dash() if not DashLeft or not DashRight then return end if character and not debounce then debounce = true local humanoid = character.Humanoid local HRP = character.HumanoidRootPart -- 切换左右冲刺状态 sideDash = not sideDash -- 先停止之前的动画,避免冲突 if loadedAnimation then loadedAnimation:Stop() end -- 加载对应方向的冲刺动画 if sideDash then loadedAnimation = humanoid.Animator:LoadAnimation(DashRight) else loadedAnimation = humanoid.Animator:LoadAnimation(DashLeft) end local dashDirection local isOnGround = humanoid.FloorMaterial ~= Enum.Material.Air and humanoid.FloorMaterial ~= Enum.Material.Water if isOnGround then -- 根据冲刺方向计算目标位置(左右冲刺对应角色坐标系的左右方向) if sideDash then dashDirection = HRP.Position + HRP.CFrame.RightVector else dashDirection = HRP.Position - HRP.CFrame.RightVector end -- 创建BodyGyro控制角色朝向冲刺方向 local bodyGyro = Instance.new("BodyGyro") bodyGyro.Parent = HRP bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge) bodyGyro.D = 10 -- 增加阻尼,使旋转更平滑 bodyGyro.P = 200000 -- 降低强度,避免过度旋转 bodyGyro.CFrame = CFrame.lookAt(HRP.Position, dashDirection) -- 创建VectorForce施加冲刺力 local attachment = Instance.new("Attachment") attachment.Parent = HRP local vectorForce = Instance.new("VectorForce") vectorForce.Parent = HRP vectorForce.Attachment0 = attachment vectorForce.RelativeTo = Enum.ActuatorRelativeTo.World -- 采用世界坐标系计算力的方向 local direction = (dashDirection - HRP.Position).Unit vectorForce.Force = direction * velocity -- 朝着冲刺方向施加力 -- 启动冲刺动画与特效 loadedAnimation:Play() humanoid.AutoRotate = false -- 冲刺时禁止自动旋转,保持朝向 DustFx(true, fx1, fx2) task.wait(duration) -- 平滑减速,避免脚部突然加速 local TweenService = game:GetService("TweenService") local tweenInfo = TweenInfo.new(0.1, Enum.EasingStyle.Linear) local tween = TweenService:Create(HRP, tweenInfo, { Velocity = Vector3.new(0, HRP.Velocity.Y, 0) -- 保留Y轴速度,防止角色悬空 }) tween:Play() tween.Completed:Wait() -- 清理物理控件 vectorForce:Destroy() bodyGyro:Destroy() attachment:Destroy() -- 恢复自动旋转与动画、特效 humanoid.AutoRotate = true loadedAnimation:Stop() DustFx(false, fx1, fx2) end task.wait(cooldown) debounce = false end end function DustFx(active, fx1, fx2) fx1.Enabled = active fx2.Enabled = active end -- 绑定UI按钮冲刺事件 local function getImageButton() local ScreenGuiBoton = player:WaitForChild("PlayerGui"):WaitForChild("ScreenGuiBotón") return ScreenGuiBoton:FindFirstChild("ImageButton") end local TextBoton = getImageButton() if TextBoton then TextBoton.MouseButton1Click:Connect(Dash) end
核心修复点
前两次冲刺失效问题
- 修正冲刺力方向逻辑:原代码固定使用本地坐标系Z轴负方向施力,导致冲刺方向与左右动画完全不匹配。现在根据
sideDash状态计算世界坐标系下的左右方向向量,确保冲刺动作与动画一致。 - 优化BodyGyro参数:增加阻尼值、降低旋转强度,避免角色旋转过度或卡顿。
- 增加动画冲突处理:每次加载新动画前停止旧动画,防止多个动画叠加导致异常。
- 修正冲刺力方向逻辑:原代码固定使用本地坐标系Z轴负方向施力,导致冲刺方向与左右动画完全不匹配。现在根据
脚部大幅加速问题
- 替换直接清零Velocity的操作:改用TweenService平滑减速X、Z轴速度,保留Y轴速度避免角色悬空,让冲刺到行走/静止的过渡更自然。
- 调整AutoRotate状态:冲刺时禁用自动旋转,结束后恢复,避免角色朝向混乱。
其他代码优化
- 移除重复定义的
character变量,精简代码结构。 - 修复动画资源检查逻辑:只要任一冲刺动画不存在就终止函数,避免空引用错误。
- 改用
task.wait()替代wait(),提升代码执行效率。 - 增加UI按钮空值判断,防止按钮不存在时触发错误。
- 移除重复定义的
内容的提问来源于stack exchange,提问作者Oscar Charlo
相关产品推荐
相关产品推荐

