How to make a force be applied constantly without it infinitely speeding up?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Make a force be applied constantly without it infinitely speeding up

  2. 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)

  3. 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)

3 Likes

If you have a resultant force acting, you’re naturally going to have acceleration in that direction.

So if you want it to not infinitely speed up, you can:

  • Apply an equal an opposite force (don’t know if you can have two body forces on a part - you definitely can if you use the newer vector forces though)
  • Go back to body velocity (or linear velocity), as this tries to maintain a constant speed.
1 Like

wouldnt applying an opposite force zero out the force though?

2 Likes

zero resultant force → zero acceleration

So you won’t be infinitely speeding up, but you will still be moving at a constant speed (assuming you were moving in the first place)

Maybe I’m misunderstanding something?

1 Like

so what you’re saying is

bf1 and bf2

bf1.Force = root.CFrame.LookVector * moveforce * root.AssemblyMass
bf2.Force = root.CFrame.LookVector * -moveforce * root.AssemblyMass

1 Like

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.

1 Like

i switch the humanoid state to physics midair to retain velocity instead of the player stopping midair

physics state however removes the ability to move midair

so im trying to re-implement that mechanic

I added a video of the problem to the post

I figured it out!

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

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.