How can I make the velocity remain the same after the player stops pressing on WASD keys?

I made a ship model and I am having some issues with making the speed remain constant after the player stops pressing on the key. I want the speed of the ship to increase or decrease by a certain amount when I press W or S key, all in relative to the lookvector and have a maximum speed of 25 studs/sec.

As of right now, my ship instantly accelerates to 20 studs per second and the speed drops immediately to 0 after I stop pressing on W key.

I have tried doing
vehicleSeat.F.Velocity = vehicleSeat.F.Velocity + Vector3.new(vehicleSeat.Parent.Facing.CFrame.LookVector.X * 1.1 * -vehicleSeat.Throttle,0,vehicleSeat.Parent.Facing.CFrame.LookVector.Z * 1.1 * -vehicleSeat.Throttle)

But the ship isn’t moving to where it is facing. I suspect that there is a problem in my script but I can’t figure it out myself.

Any help is appreciated and please let me know if you want to know more about the model or the script!!

2 Likes

I would recommend using bodytrust when trying to move the ship, you can set the Z value on a bodythrust to make an object move forwards or backwards relative to their current orientation (I do not believe bodyvelocity does this). To fix the issue questioned you could make the Z value increase after KeyDown is fired and slowly make it decrease after KeyUp is fired.

For some reason when I swap bodyvelocity with bodythrust, the ship doesn’t move towards where it is facing.

are you setting the Force property? I tested this in studio and I had Force set to 0,0,-500 and it went forwards.

I am using a torque to control my turns and a body gyro to stabilize the ship so that it doesn’t rock back and forth. So the problem is that I need to maintain a constant speed while having the ship to move in the direction that it is facing.
I swapped out the body velocity and used body thrust as you suggested and the ship is not moving correctly.
https://drive.google.com/file/d/15ozosBbNVM-z0ZPghvrIafFgAUUxRUJA/view?usp=sharing

which value is being set via your math? You could also try to change the location of the bodythrust to somewhere more center if it is not already

I am changing the x and z value and y is zero.

So far I am only able to move my ship in relative to lookvector with body velocity only. For some reason every other bodymovers don’t move my ship properly. Does anyone know why?

I recreated a movement system in studio and saw the problems you said in the thread, I can only assume that they do not have a real relative system, but a number of different axis (ms paint representation lol) that it snaps to when its near it. I can only recommend just using your old system and adding gradual deceleration instead of flat setting the value to 0 when the key is released

Ohh… So now that I am using body velocity. How do I make it accelerate and decelerate? I am using the throttle and steer inside the vehicle seat to control my velocity so everytime I stop pressing on any one of the WASD keys my ship stops. This is the part that I am stuck on. :C

Oh yeah and this is my current code to control the ship

local vehicleSeat = script.Parent
function updateForces()
	
	vehicleSeat.T.Torque = Vector3.new(0, torque * -vehicleSeat.Steer , 0)
    vehicleSeat.F.Velocity =   Vector3.new(vehicleSeat.Parent.Facing.CFrame.LookVector.X * 20 * -vehicleSeat.Throttle,0,vehicleSeat.Parent.Facing.CFrame.LookVector.Z * 20 * -vehicleSeat.Throttle)
end

while wait(0.05) do
	updateForces()
end

Anyone please help me. 300000000

Ok I got it to work!! I should have known this earlier because this is literally so simple. So basically you can use SteerFloat and ThrottleFloat to control the velocity of the vehicle that you are making. The key is the throttlefloat allows you to have decimals while throttle only allows -1,0, and 1. By having a script to add and subtract the throttlefloat, my ship is able to move in relative it’s lookvector while maintaining speed.

I am using user input service to control my ship sooo yeah…
I will be sharing the script when I press the W key.
For future viewers who needs help like me:
Local Script

local holdingWKey = false

UIS.InputBegan:Connect(function(inputObject) 
    if(inputObject.KeyCode==Enum.KeyCode.W)then
        holdingWKey = true

		    while holdingWKey do
			Wevent:FireServer()
				wait(0.2)
			end
        end
end

Server Script

local replicatedStorage = game:GetService("ReplicatedStorage")
local Wevent = replicatedStorage:WaitForChild("WKey")

Wevent.OnServerEvent:Connect(function(player)
	local shipselected = player.shipselected.Value
	
	
	if shipselected == "Yamato" then --This is just for me to check which ship the player is in. You can delete this
		
		if game.Workspace["Cloned"..shipselected..player.Name].VehicleSeat.ThrottleFloat < 1 then
			
			game.Workspace["Cloned"..shipselected..player.Name].VehicleSeat.ThrottleFloat = game.Workspace["Cloned"..shipselected..player.Name].VehicleSeat.ThrottleFloat + 0.1
		end  --0.1 in this script determines how rapidly the speed to increase. ThrottleFloat is a percentage, so 0.1 of 20 (my max speed) is 2. AKA everytime the player press W the speed increases by 2 studs per second.
	end
end)

Server Script for the vehicle seat of my ship

local torque = 25000000
function updateForces()
	
	vehicleSeat.T.Torque = Vector3.new(0, torque * -vehicleSeat.SteerFloat , 0)
    vehicleSeat.F.Velocity =   Vector3.new(vehicleSeat.Parent.Facing.CFrame.LookVector.X * 20 * - vehicleSeat.ThrottleFloat,0,vehicleSeat.Parent.Facing.CFrame.LookVector.Z * 20 * -vehicleSeat.ThrottleFloat)
end
--The 20 in this script determines the max speed.
while wait(0.05) do
     updateForces()
end
1 Like