How to make bike transition smoothly from a rotation to another

Hello everybody, i’m trying to make a bike stabalize using align orientation. To achieve this i cast a raycast from the center of the bike to the ground and then calculate the correct orientation that the bike should adapt however since i’m using one raycast from the center this change happens suddenly but i want it to be a smooth transition.

Here’s what i have :

Here’s what i want :

Here’s the code :

-- Services
local RunService = game:GetService("RunService")

-- Bike Variables
local bike = game.Workspace:WaitForChild("Bike")
local humanoidRootPartBike = bike:WaitForChild("HumanoidRootPart")

-- Constraints
local alignOrientation = mainAttachment:WaitForChild("AlignOrientation")
local vectorForce = mainAttachment:WaitForChild("VectorForce")

-- Attachments
local mainAttachment = humanoidRootPartBike:WaitForChild("MainAttachment")

-- Varaibles
local force = 400
local drag = 0.1

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {bike,character,humanoidRootPartBike}

RunService.Heartbeat:Connect(function(deltaTime)
	local raycastMiddle = game.Workspace:Raycast(mainAttachment.WorldPosition,Vector3.new(0,-3,0),raycastParams)

	vectorForce.Enabled = true
	vectorForce.Force = humanoidRootPartBike.CFrame.LookVector * humanoidRootPartBike.AssemblyMass * (force * bike:GetAttribute("SpeedMultipiler")) * vehicleSeat.Throttle

	if humanoidRootPartBike.AssemblyLinearVelocity.Magnitude > 0 then
		local dragVector = -humanoidRootPartBike.AssemblyLinearVelocity.Unit
		local v2 = humanoidRootPartBike.AssemblyLinearVelocity.Magnitude ^ 2 
		vectorForce.Force += dragVector * v2 * drag  * humanoidRootPartBike.AssemblyMass
	end 

	if raycastMiddle then
		local lookVector = bikeMesh.CFrame.LookVector:Cross(raycastMiddle.Normal).Unit

		alignOrientation.CFrame = CFrame.fromMatrix(Vector3.zero,Vector3.zAxis,raycastMiddle.Normal,lookVector):Orthonormalize() * CFrame.Angles(0,math.rad(90),0)

		frontResult.WorldPosition = raycastMiddle.Position
	end
end)
1 Like