Rotating the camera without disturbing the direction the player walks in

I am trying to make a vehicles system by creating a seperate, static, version of the vehicle which has the player on. However, with this setup, things break with rotating parts. So, I am wondering if there would be a way to rotate the camera without affecting the direction the player would walk in. Here’s the code (excuse the mess, I’m yet to clean it up):

RS = game:GetService("RunService")
PL = game:GetService("Players")

player = PL.LocalPlayer
while not player.Character do wait() end
local VehicleClone:Model
local OriginalPlatform
local StandingOnPlatform = false
local offset
local LastAngle = CFrame.new()

local FalseCam = Instance.new("Part")
FalseCam.Size = Vector3.new(1,1,1)
FalseCam.Transparency = 1
FalseCam.CanCollide = false

local hum:Humanoid

Func = RS.Heartbeat:Connect(function()
	local RaycastParam = RaycastParams.new()
	local character = player.Character
	local RootPart:BasePart = character.HumanoidRootPart
	local CharacterPivot = character.PrimaryPart.CFrame
	RaycastParam.FilterType = Enum.RaycastFilterType.Exclude
	RaycastParam.FilterDescendantsInstances = {character}
	local RaycastResults = workspace:Raycast(RootPart.CFrame.Position,Vector3.new(0,-50,0),RaycastParam)

	if StandingOnPlatform then
		if not (RaycastResults and RaycastResults.Instance:IsDescendantOf(VehicleClone)) then
			print("hello")
			StandingOnPlatform = false
			offset = VehicleClone.PrimaryPart.CFrame:ToObjectSpace(CharacterPivot)
			VehicleClone:Destroy()
			character:PivotTo(OriginalPlatform.CFrame:ToWorldSpace(offset))
			game.Workspace.CurrentCamera.CameraSubject = character.Humanoid
			LastAngle = CFrame.new()
			return
		end
		local FalseCamClone = FalseCam:Clone()
		game.Workspace.CurrentCamera.CameraSubject = FalseCamClone
		FalseCamClone.CFrame = CFrame.new(character.HumanoidRootPart.CFrame.Position + character.Humanoid.CameraOffset) * VehicleClone.PrimaryPart.CFrame:ToObjectSpace(OriginalPlatform.CFrame)
		game.Workspace.CurrentCamera.CFrame = game.Workspace.CurrentCamera.CFrame * LastAngle:ToObjectSpace(OriginalPlatform.CFrame.Rotation)
		LastAngle = OriginalPlatform.CFrame.Rotation
		FalseCamClone:Destroy()
	else
		if RaycastResults and RaycastResults.Instance then
			if RaycastResults.Instance:HasTag("VehicleBase") then
				if VehicleClone and RaycastResults.Instance:IsDescendantOf(VehicleClone) then return end
				if VehicleClone then VehicleClone:Destroy() end
				VehicleClone = RaycastResults.Instance.Parent:Clone()
				VehicleClone:PivotTo(CFrame.new(0,0,1000))
				VehicleClone.Parent = workspace.VehicleModels

				OriginalPlatform = RaycastResults.Instance
				offset = RaycastResults.Instance.Parent.PrimaryPart.CFrame:ToObjectSpace(CharacterPivot)
				character:PivotTo(VehicleClone.PrimaryPart.CFrame:ToWorldSpace(offset))
				StandingOnPlatform = true
			else
				--[[game.Workspace.CurrentCamera.CameraSubject = character.Humanoid]]
			end
		end
	end

end)

And here’s the place file so it’s easier to see the issue:
Platform Testing Two.rbxl (48.0 KB)
(Notice the new platform that appears in the background when you walk onto the original one)

Help much appreciated!
Thanks

1 Like

Updated Code w/ small bug fix:

RS = game:GetService("RunService")
PL = game:GetService("Players")

player = PL.LocalPlayer
while not player.Character do wait() end
local VehicleClone:Model
local OriginalPlatform
local StandingOnPlatform = false
local offset
local LastAngle = CFrame.new()

local FalseCam = Instance.new("Part")
FalseCam.Size = Vector3.new(1,1,1)
FalseCam.Transparency = 0
FalseCam.BrickColor = BrickColor.new("Bright red")
FalseCam.Anchored = true
FalseCam.CanCollide = false

local hum:Humanoid

Func = RS.Heartbeat:Connect(function()
	local RaycastParam = RaycastParams.new()
	local character = player.Character
	local RootPart:BasePart = character.HumanoidRootPart
	local CharacterPivot = character.PrimaryPart.CFrame
	RaycastParam.FilterType = Enum.RaycastFilterType.Exclude
	RaycastParam.FilterDescendantsInstances = {character}
	local RaycastResults = workspace:Raycast(RootPart.CFrame.Position,Vector3.new(0,-50,0),RaycastParam)

	if StandingOnPlatform then
		if not (RaycastResults and RaycastResults.Instance:IsDescendantOf(VehicleClone)) then
			print("hello")
			StandingOnPlatform = false
			offset = VehicleClone.PrimaryPart.CFrame:ToObjectSpace(CharacterPivot)
			VehicleClone:Destroy()
			character:PivotTo(OriginalPlatform.CFrame:ToWorldSpace(offset))
			game.Workspace.CurrentCamera.CameraSubject = character.Humanoid
			LastAngle = CFrame.new()
			return
		end
		local FalseCamClone = FalseCam:Clone()
		FalseCamClone.Parent = workspace
		game.Workspace.CurrentCamera.CameraSubject = FalseCamClone
		offset = VehicleClone.PrimaryPart.CFrame:ToObjectSpace(character.PrimaryPart.CFrame)
		FalseCamClone.CFrame = OriginalPlatform.CFrame:ToWorldSpace(offset)
		game.Workspace.CurrentCamera.CFrame = game.Workspace.CurrentCamera.CFrame * LastAngle:ToObjectSpace(OriginalPlatform.CFrame.Rotation)
		LastAngle = OriginalPlatform.CFrame.Rotation
	else
		if RaycastResults and RaycastResults.Instance then
			if RaycastResults.Instance:HasTag("VehicleBase") then
				if VehicleClone and RaycastResults.Instance:IsDescendantOf(VehicleClone) then return end
				if VehicleClone then VehicleClone:Destroy() end
				VehicleClone = RaycastResults.Instance.Parent:Clone()
				VehicleClone:PivotTo(CFrame.new(0,0,1000))
				VehicleClone.Parent = workspace.VehicleModels

				OriginalPlatform = RaycastResults.Instance
				offset = RaycastResults.Instance.Parent.PrimaryPart.CFrame:ToObjectSpace(CharacterPivot)
				character:PivotTo(VehicleClone.PrimaryPart.CFrame:ToWorldSpace(offset))
				StandingOnPlatform = true
			else
				--[[game.Workspace.CurrentCamera.CameraSubject = character.Humanoid]]
			end
		end
	end

end)