Hello! I am currently making a fly script and it works fine but whenever I move forward, my character doesn’t stop and continues to keep moving.
What do you want to achieve? I would like for my character to slowly come to a stop whenever I let go of the key.
What solutions have you tried so far? I’ve tried creating an opposite force to counteract the original force, But that just made me go the other way and my character still wouldn’t stop.
Here is my current code:
local FORWARD_KEY = Enum.KeyCode.W
local function onMove1(actionName, inputState)
if inputState == Enum.UserInputState.Begin then
-- If the player presses down the button then they move forward
player.Character.HumanoidRootPart.VectorForce.Force = Vector3.new(0,48,0)
elseif inputState == Enum.UserInputState.End then
-- when the player releases the button then they are supposed to stop moving
player.Character.HumanoidRootPart.VectorForce.Force = Vector3.new(0,0,0)
end
end
ContextActionService:BindAction("Move1", onMove1, false, FORWARD_KEY)
If anyone can help me, That would be greatly appreciated!
You do need a force in the opposite direction of motion to slow the part down, but it should not just be a fixed amount, you probably want to to be proportional to the velocity, otherwise it will reverse direction, like you saw.
Yep as @EmilyBendsSpace said the counteracting force will need to be proportional to velocity.
To do the proportional maths check out @ThanksRoBama tutorial on PID, where the P stands for Proportional control.
In this case for the P_Controller
--kP is a random constant for fine tuning, set at kP = 0.5 for now
--set_point is the goal which is root part Velocity = 0,0,0
--Process_Value is the current velocity, so it would be the HumanoidRootPart.AssemblyLinearVelocity
function p_controller(kP, set_point, process_value)
local e = set_point - process_value
local CO = kP * e
return CO--CO control output = output force applied, just place it into the VectorForce.Force and watch it work.
--Remember to constantly calculate the output force as velocity changes
--Better to put it in a RunService connection PostSimulation since it runs every frame after physics.
end
Ok well, Thanks for the help but all this is a bit complex for me… I still don’t quite understand how its supposed to work. Maybe I should just stick to a bit easier stuff. Thanks anyway tho
Consider working with a BodyVelocity instance instead. It’s older tech, but very straightforward to use and you don’t have to worry about stability of your controller like you do if you’re trying to use forces directly.