Need help with improving this hoverboard physics and its rotation

Hello there, So i wanted to make a hoverboard game and my friend gave me a hoverboard script he found online and he added his own small modifications to it, and then i added some more small edits to it like adjusting speed.

But i want its orientation to match with orientation of surface below it so its even more fun to move around with, but i cant figure out how to do it because i am not too good at math related scripting, I’ve tried to look on devforum but when i try to add other people’s script, the movement completely messes up (like Pressing W doesnt reset back orientation so it keeps on pushing me back, etc)

Is there a way to add orientation detection while also keeping the current smooth hoverboard movements?

heres clip of current hoverboard movement:

Picture of what I’ve been trying to achieve:

Script: I’ve added some comments to help

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

wait(5)

local primaryPart = script.Parent.PrimaryPart
local seat = script.Parent:WaitForChild("SeatingPlace")
local vectorForce = script.Parent.VectorForce
local dragVectorForce = script.Parent.DragVectorForce
local alignOrientation = script.Parent.AlignOrientation
local orientation = CFrame.new()
local proportional = 1000
local derivative = 100
local derfall = 170
local drag = 1.5
local idleHeight = 12
local idleCastHeight = 12
local sitHeight = 14
local sitCastHeight = 14

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {script.Parent}

local hold = 0 -- Edits speed, Higher number means higher speed
RunService.Heartbeat:Connect(function(deltaTime)
	local height = if seat.Occupant == nil then idleHeight else sitHeight
	local castHeight = if seat.Occupant == nil then idleCastHeight else sitCastHeight
	
	-- Hover Force
	local localVelocity = primaryPart.CFrame:VectorToWorldSpace(primaryPart.AssemblyLinearVelocity)
	local result = workspace:Blockcast(primaryPart.CFrame, primaryPart.Size, primaryPart.CFrame.UpVector * -castHeight, params)
	if result == nil then
		vectorForce.Force = Vector3.new(0, -localVelocity.Y * derfall, 0)
		print("nothing") -- falls down because its way above surface
	else
		local offset = primaryPart.CFrame:PointToObjectSpace(result.Position)
		vectorForce.Force = Vector3.new(0, (height + offset.Y) * proportional - localVelocity.Y * derivative, 0) 
		
	end
	
	-- Drag
	local velocity = primaryPart.AssemblyLinearVelocity
	if velocity.Magnitude == 0 then
		dragVectorForce.Force = Vector3.zero
	else
		dragVectorForce.Force = -velocity.Unit * (drag * (velocity.Magnitude - hold) ^ 2)
	end
	
	-- Orientation
	if seat.Occupant == nil then
		local groundVelocity = Vector3.new(primaryPart.AssemblyLinearVelocity.X + hold, 0, primaryPart.AssemblyLinearVelocity.Z)
		if groundVelocity.Magnitude > 1 then
			alignOrientation.CFrame = CFrame.lookAt(Vector3.zero, groundVelocity)
		end
	else
		if UserInputService:IsKeyDown(Enum.KeyCode.A) == true then orientation *= CFrame.fromOrientation(0, 0.06, 0) end
		if UserInputService:IsKeyDown(Enum.KeyCode.D) == true then orientation *= CFrame.fromOrientation(0, -0.06, 0) end
		
		local tilt = CFrame.new()
		if UserInputService:IsKeyDown(Enum.KeyCode.W) == true then
			if UserInputService:IsKeyDown(Enum.KeyCode.A) == false and UserInputService:IsKeyDown(Enum.KeyCode.D) == false then
				hold += 0.2
			end
			tilt *= CFrame.fromAxisAngle(orientation.RightVector, 0.4) 
			
		end
		if UserInputService:IsKeyDown(Enum.KeyCode.S) == true then tilt *= CFrame.fromAxisAngle(orientation.RightVector, -0.4) end
		if UserInputService:IsKeyDown(Enum.KeyCode.A) == true then tilt *= CFrame.fromAxisAngle(orientation.LookVector, 0.4) 
			if hold > 3 then
				hold -= 0.3
			end
		end
		if UserInputService:IsKeyDown(Enum.KeyCode.D) == true then tilt *= CFrame.fromAxisAngle(orientation.LookVector, -0.4)
			if hold > 3 then
				hold -= 0.3
			end
		end
		alignOrientation.CFrame = tilt * orientation
	end
end)
1 Like

Would this help

https://devforum.roblox.com/t/how-do-i-make-a-vector-normal-translate-into-a-rotation/964525

1 Like

Check out my pinned topic on my profile, it is pretty similar to this one :heart:

got a bit close to it, is there any way to properly assign that Value to AlignOrientation?

its getting a bit out of control and wonky, and sometimes stops moving at all