You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
Make a force be applied constantly without it infinitely speeding up
What is the issue? Include screenshots / videos if possible!
I have a script that changes the state of the player midair to “physics” to retain velocity, however it didn’t let the player move, so I added a section where it applies force to the player midair to move.
This works good but it continuously applies the force and speeds up the player really fast.
(it’s a bit subtle but you can tell it’s accelerating)
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I was able to somewhat fix it by adding a threshold (max walkspeed * 1.5) and a max velocity where force is applied (it wont apply velocity in that direction if it’s more than a certain amount)
I don’t really like this solution, though because at smaller speeds the velocity doesnt carry and the issue is still present and noticeable
This is my code:
local threshold = 50
if not debounce then
if uis:IsKeyDown(Enum.KeyCode.W) then
if forwardvel < threshold and forwardvel > -threshold and forwardvel > (maxWS * 1.5) then
bv.Force = root.CFrame.LookVector * moveforce * root.AssemblyMass
else
bv.Force = Vector3.zero
end
elseif uis:IsKeyDown(Enum.KeyCode.S) then
if forwardvel > -threshold and forwardvel < threshold and forwardvel < -(maxWS * 1.5) then
bv.Force = root.CFrame.LookVector * -moveforce * root.AssemblyMass
else
bv.Force = Vector3.zero
end
elseif uis:IsKeyDown(Enum.KeyCode.D) then
if rightvel < threshold and rightvel > -threshold and rightvel > (maxWS * 1.5) then
bv.Force = root.CFrame.RightVector * moveforce * root.AssemblyMass
else
bv.Force = Vector3.zero
end
elseif uis:IsKeyDown(Enum.KeyCode.A) then
if rightvel > -threshold and rightvel < threshold and rightvel < -(maxWS * 1.5) then
bv.Force = root.CFrame.RightVector * -moveforce * root.AssemblyMass
else
bv.Force = Vector3.zero
end
else
bv.Force = Vector3.zero
end
end
rightvel and forwardvel are the calculated velocities for the right velocity and forward velocity
bv is a bodyforce (it was previously a bodyvelocity but i didnt bother to rename it to “bf”)
(sorry if this code is messy, i’m not really an expert, also this is a parkour game)
What I’m saying is if the forces on the player all cancel each other out, then the player will not speed up at all and remain at a constant speed. If they were not moving, and then the forces cancel, they will still be stationary.
What’s the actual context behind the problem? Is this like a dash system - since you said it was a parkour game.
All i did was instead of using a bodyforce, i just applied the velocity directly and capped it at the max walkspeed
note i had to make the moveforce extremely small due to it being in a “Heartbeat” loop
the prints were just for debugging purposes
code that works:
if hum:GetState() ~= Enum.HumanoidStateType.Physics then --i used this because in certain scenarios the state isn't physics when midair (e.g. when jumping)
return
end
if uis:IsKeyDown(Enum.KeyCode.W) then
if forwardvel < maxWS then
root.Velocity += removeY(cam.CFrame.LookVector * moveforce)
end
print("apply force forwards")
elseif uis:IsKeyDown(Enum.KeyCode.S) then
if forwardvel > -maxWS then
root.Velocity += removeY(cam.CFrame.LookVector * -moveforce)
end
print("apply force backwards")
elseif uis:IsKeyDown(Enum.KeyCode.D) then
if rightvel < maxWS then
root.Velocity += removeY(cam.CFrame.RightVector * moveforce)
end
print("apply force sideways right")
elseif uis:IsKeyDown(Enum.KeyCode.A) then
if rightvel > -maxWS then
root.Velocity += removeY(cam.CFrame.RightVector * -moveforce)
end
print("apply force sideways left")
end