Making a Hover Vehicle Tilt Towards the Angle It Is Moving Towards

Hello, I am trying to create a hover vehicle that translates forwards, backwards, up, and down. Whenever the hover vehicle is moving towards these directions I want it to tilt towards the direction it is moving. I want this to be a smooth rotation towards the tilted angle. I have tried to go about this using AlignOrientation, and having the model of the hover vehicle follow a root part that does an instant rotation using CFrame. However, there is inherent jitteriness when AlignOrientation does not have rigidity enabled. So instead I am trying to make a smooth rotation using CFrame, RunService, and by clamping the angle.

So far I have followed the code by dthecoolest as well as implementing my own adjustments:
CFrame clamp on turret?

However, the hover vehicle just seems to rotate towards the corners and does not return to a neutral flat non-rotated position once WASD are released.

local UserInputService = game:GetService("UserInputService")

local translationSpeed = 10
local climbSpeed = 10
local tiltAngle = 25
local tiltRate = 1

local root = script.Parent:WaitForChild("Root")
local vehicleSeat = script.Parent:WaitForChild("VehicleSeat")
local linearVelocity = root:WaitForChild("LinearVelocity")

function controlLoop(deltaTime)
	local climb = 0
	
	if UserInputService:IsKeyDown(Enum.KeyCode.Q) then
		climb = 1
	elseif UserInputService:IsKeyDown(Enum.KeyCode.E) then
		climb = -1
	end
	
	local goalCFrame = root.CFrame * 
		CFrame.Angles(
			math.rad(tiltRate) * deltaTime * vehicleSeat.Throttle,
			0,
			-math.rad(tiltRate) * deltaTime * vehicleSeat.Steer
		)
	
	local rotX, rotY, rotZ = goalCFrame:ToOrientation()

	rotX = math.rad(math.clamp(math.deg(rotX), -tiltAngle, tiltAngle))
	rotZ = math.rad(math.clamp(math.deg(rotZ), -tiltAngle, tiltAngle))
	
	root.CFrame = CFrame.fromOrientation(rotX, rotY, rotZ) + root.Position
	
	linearVelocity.VectorVelocity = Vector3.new(
		translationSpeed * vehicleSeat.Throttle, 
		climbSpeed * climb, 
		translationSpeed * vehicleSeat.Steer
	)
end

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

hovercar

Thank you for any assistance. I am not the most knowledgeable or proficient when it comes to CFrame.

3 Likes

Figured it out using AlignOrientation. The jitteriness was stemming from the fact that I was aligning the orientation of the model to a root part. Instead I used the one attachment mode on an align orientation inside of the root and manually set the CFrame inside of the script. Then I just welded the model to the root.

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