Turning vehicle properly

  • What are you attempting to achieve? (Keep it simple and clear)

I’m creating hover car and I have successfully managed to program suspension system and it’s physics, but I have problems with controls of vehicle.

  • What is the issue? (Keep it simple and clear - Include screenshots/videos/GIFs if possible)

I don’t know what body movers should I use.
I tried Angular velocity, but it fails to update front direction of vehicle resulting in vehicle rotating, but continuing moving towards fixed direction.

  • What solutions have you tried so far? (Have you searched for solutions through the Roblox Wiki yet?)

I know I should be using vehicle seat, but I’m not using it now, I am testing this vehicle with Run(Player is not spawning) and I have runService loop which detects keys pressed and on desired ones applies forces of moving forward or backwards and turning. I think that’s where I have trouble. My loop doesn’t update Cframe.LookVector value

here’s the code:

game:GetService("RunService").Stepped:Connect(function()




    local X = part.Velocity.X
    local Y = part.Velocity.Y
    local Z = part.Velocity.Z


        --Resistance and Deaccelaration  
        part.Velocity = part.Velocity - part.Velocity/R 

    local function onKeyPress(inputObject, gameProcessedEvent)


    --Forward
    if inputObject.KeyCode == Enum.KeyCode.T then

        thruster.Force = thruster.Force + part.CFrame.LookVector*10

    end

    --Backward
    if inputObject.KeyCode == Enum.KeyCode.G then
        
        

        thruster.Force = thruster.Force - part.CFrame.LookVector*10
    end

    --Turning Right
    if (inputObject.KeyCode == Enum.KeyCode.T and UIS:IsKeyDown(Enum.KeyCode.H)) or (inputObject.KeyCode == Enum.KeyCode.H and UIS:IsKeyDown(Enum.KeyCode.T)) then
        
        thruster.Force = thruster.Force + part.CFrame.LookVector*10

        rotate1.AngularVelocity = Vector3.new(0,-2,0)


    end
    --Left
    if (inputObject.KeyCode == Enum.KeyCode.T and UIS:IsKeyDown(Enum.KeyCode.F)) or (inputObject.KeyCode == Enum.KeyCode.F and UIS:IsKeyDown(Enum.KeyCode.T)) then
        
    	thruster.Force = thruster.Force + part.CFrame.LookVector*10

        rotate1.AngularVelocity = Vector3.new(0,2,0)

	
        
	end
	--RotLeft
	if inputObject.KeyCode == Enum.KeyCode.F then
		
		rotate1.AngularVelocity = Vector3.new(0,2,0)

		
	end
	--RotRight
	if inputObject.KeyCode == Enum.KeyCode.H  then
		
		rotate1.AngularVelocity = Vector3.new(0,-2,0)

	end
	
		
end






local function onKeyLetgo(inputObject, gameProcessedEvent)
	if (inputObject.KeyCode == Enum.KeyCode.T and inputObject.KeyCode == Enum.KeyCode.F) 
	or (inputObject.KeyCode == Enum.KeyCode.T and inputObject.KeyCode == Enum.KeyCode.H) 
	or (inputObject.KeyCode == Enum.KeyCode.G and inputObject.KeyCode == Enum.KeyCode.F)  
	or (inputObject.KeyCode == Enum.KeyCode.G and inputObject.KeyCode == Enum.KeyCode.H)
	or  inputObject.KeyCode == Enum.KeyCode.T 
	or  inputObject.KeyCode == Enum.KeyCode.G 
	or  inputObject.KeyCode == Enum.KeyCode.H 
	or  inputObject.KeyCode == Enum.KeyCode.F then
		
        thruster.Force = Vector3.new(0, thruster.Force.Y ,0)
        rotate1.AngularVelocity = Vector3.new(0,0,0)

	end
end



UIS.InputBegan:connect(onKeyPress)

UIS.InputEnded:connect(onKeyLetgo)



end)
1 Like

Which motion are you trying to achieve, rotation (BodyAngularVelocity) or sideways movement (BodyThrust) when you turn? If you’re interested, here’s the script that I use to steer a sled that I’m working on at the moment (but it relies on a vehicle seat):

Code
local Throttle = script.Parent.ThrottleForce --(BodyThrust)
local Steer = script.Parent.SteerForce --(Angular Velocity)
--Parented to a vehicle seat.
while true do
	if script.Parent.Throttle == 1 then
		Throttle.Force = Vector3.new(0,0,-150)
	end
	if script.Parent.Throttle == 0 then
		Throttle.Force = Vector3.new(0,0,0)
	end
	if script.Parent.Throttle == -1 then
		Throttle.Force = Vector3.new(0,0,100)
	end
	if script.Parent.Steer == 1 then
		Steer.AngularVelocity = Vector3.new(0,-.5,0)
	end
	if script.Parent.Steer == 0 then
		Steer.AngularVelocity = Vector3.new(0,0,0)
	end
	if script.Parent.Steer == -1 then
		Steer.AngularVelocity = Vector3.new(0,.5,0)
	end
	wait(.01)
end

image

1 Like

I have it working on moving forward and backwards, I can rotate it too but it gets stuck in a loop until keyboard input is changed. I mean if in this part of script I insert print statement it will only print once

if (inputObject.KeyCode == Enum.KeyCode.T and UIS:IsKeyDown(Enum.KeyCode.F)) or 
(inputObject.KeyCode == Enum.KeyCode.F and UIS:IsKeyDown(Enum.KeyCode.T)) then
    print("Hello World")--this will only execute once
	thruster.Force = thruster.Force + part.CFrame.LookVector*10

    rotate1.AngularVelocity = Vector3.new(0,2,0)
end

That’t why I think I should use vehicle seat.

1 Like

I also tried this

       if (inputObject.KeyCode == Enum.KeyCode.T and UIS:IsKeyDown(Enum.KeyCode.F)) or (inputObject.KeyCode == Enum.KeyCode.F and UIS:IsKeyDown(Enum.KeyCode.T)) then
        while (inputObject.KeyCode == Enum.KeyCode.T and UIS:IsKeyDown(Enum.KeyCode.F)) or (inputObject.KeyCode == Enum.KeyCode.F and UIS:IsKeyDown(Enum.KeyCode.T)) do
        	thruster.Force = thruster.Force + part.CFrame.LookVector*10
			
            rotate1.AngularVelocity = Vector3.new(0,2,0)
			wait()
		end

but this way it is executed properly, but doesn’t work as wanted. Maybe there’s problem with how I handle applying force.

1 Like

I implemented it with vehicle seat and this is the result
https://i.gyazo.com/63005eb4a442a75a319f3a766ff9ed85.gif
https://i.gyazo.com/dc219620928cf9760f5ab4cab1baaff9.gif
Not exited at all. I’m not sure what to do.

1 Like