# Body velocity not moving straight for a second

When I Set the velocity for the Body Velocity it drifts into another direction for a short period of time. I do not know what I could do to fix this

script for the body mover:

``````-- I put this in a loop
script.Parent.Parent.Thrust.Velocity = script.Parent.Parent.CFrame.LookVector * script.Parent.Parent.TopSpeed.Value
``````

I recoment using body force as the moving force and body velocity as a drag force so it stays at a constant speed, try setting the velocity of the body velocity to negative of the spaceshipâ€™s velocity

Is your MaxForce for each axis the same value? Example: `BodyVelocity.MaxForce = Vector3.New(9000, 9000, 9000)`

I might try that I guess[characterlimit]

``````script.Parent.Parent.Thrust.MaxForce = Vector3.new(script.Parent.Parent.Acceleration.Value * 1000, script.Parent.Parent.Acceleration.Value * 1000, script.Parent.Parent.Acceleration.Value * 1000)
``````
1 Like

Actually I donâ€™t fully understand what your saying could you please explain further?

I recommend subtracting the relative X velocity from the actual main part of the spaceship. This results in no sideways velocity.

So basicallyâ€¦

``````local Part = ??? --the main part where bodyvelocity is exerted
Part.AssemblyLinearVelocity += Part.CFrame.RightVector*(0-((Part.CFrame:VectorToObjectSpace(Part.AssemblyLinearVelocity).X)))``````

iâ€™ll try this, thanks Poopmaster

still seems to go sideways from itâ€™s look vector weirdly

Could you show me the code block youâ€™re applying all the velocity?

``````script.Parent.Parent.Thrust.MaxForce = Vector3.new(script.Parent.Parent.Acceleration.Value * 1000, script.Parent.Parent.Acceleration.Value * 1000, script.Parent.Parent.Acceleration.Value * 1000)

script.Parent.OnServerEvent:Connect(function(plr)
script.Parent.Parent.Thrust.On.Value = true
while true do

script.Parent.Parent.Thrust.Velocity = script.Parent.Parent.Parent.Parent.HumanoidRootPart.CFrame.LookVector * script.Parent.Parent.TopSpeed.Value

game:GetService("RunService").Stepped:Wait()
if script.Parent.Parent.Thrust.On.Value == false then
break
end
end

end)
``````

Where are you negating the sideways velocity though?

``````script.Parent.Parent.Thrust.MaxForce = Vector3.new(script.Parent.Parent.Acceleration.Value * 1000, script.Parent.Parent.Acceleration.Value * 1000, script.Parent.Parent.Acceleration.Value * 1000)

script.Parent.OnServerEvent:Connect(function(plr)
script.Parent.Parent.Thrust.On.Value = true
while true do

script.Parent.Parent.Thrust.Velocity = script.Parent.Parent.Parent.Parent.HumanoidRootPart.CFrame.LookVector * script.Parent.Parent.TopSpeed.Value
Part = script.Parent.Parent
Part.AssemblyLinearVelocity += Part.CFrame.RightVector*(0-((Part.CFrame:VectorToObjectSpace(Part.AssemblyLinearVelocity).X)))
game:GetService("RunService").Stepped:Wait()
if script.Parent.Parent.Thrust.On.Value == false then
break
end
end

end)``````

oh i just removed it as it wasnt working

Oh okay I think it may be bodyvelocity cancelling it out.

Even though I donâ€™t think this would fix your problem, here are some code improvements you could make for efficiency.

Also, your BodyForce is applied on the server, so you should be using something on the server to determine the shipâ€™s direction. I think using the HumanoidRootPartâ€™s CFrame is OK 99% of the time, but just in case there is latency on the client or an exploit attempt, I would use something on the server instead just in case. Maybe this is interfering with your direction due to latency somehow?

``````local acceleration = script.Parent.Parent.Acceleration.Value * 1000
script.Parent.Parent.Thrust.MaxForce = Vector3.new(acceleration, acceleration, acceleration)

local IdleVelocity = Vector3.New(0, 0, 0)

local seat = -- your path to the seat/VehicleSeat

local ThrustOn = script.Parent.Parent.Thrust.On

-- Only make changes when you know the value has changed. No need to check it every render.
ThrustOn.Changed:Connect(function(newValue)
if newValue then
script.Parent.Parent.Thrust.Velocity = seat.LookVector * script.Parent.Parent.TopSpeed.Value
else
script.Parent.Parent.Thrust.Velocity = IdleVelocity
end
end)

-- Instead if having two remote events for on and off, just put it in the same one, telling you whether the player wants the thrust on or off
script.Parent.OnServerEvent:Connect(function(plr, thrust)
script.Parent.Parent.Thrust.On.Value = thrust
end)

``````

I havenâ€™t checked this code for errors and you need to put in your own path to the seat, but that should help make things more efficient, more readable for you, and hopefully easier to debug.

1 Like