First person camera drifts towards horizon if looking at a part

I’m making a first-person camera script. It’s primitive right now, so don’t tell me to add anything or massively change my code. The issue is that whenever I look at something (player:GetMouse().Target is nil), the camera drifts up towards the horizon (player:GetMouse().Hit.Position moves away from the camera in the direction of the camera). I have tried to isolate the problem but have been unsuccessful. Thanks.

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera

local HEIGHT_OFFSET = 0

local function updateCamera()
	print(player:GetMouse().Target)
	print(player:GetMouse().Hit.Position)
	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	local character = player.Character
	if character then
		local root = character:FindFirstChild("Head")
		if root then
			local rootPosition = root.Position + Vector3.new(0, HEIGHT_OFFSET, 0)
			local cameraPosition = rootPosition
			if player:GetMouse().Target == nil or player:GetMouse().Target.Parent~=character then camera.CFrame = CFrame.lookAt(rootPosition, player:GetMouse().Hit.Position) end --problem area
		end
	end
end

RunService:BindToRenderStep("FPCam", Enum.RenderPriority.Camera.Value + 1, updateCamera)

EDIT: I’ve done some further testing and have discovered that the camera always drifts up, never down, and that it drifts up very slightly still even when looking at the skybox.

1 Like

Can you provide a clip of the issue happening?

it also seems like you are checking if it’s nil and then still trying to set it to that position, so that doesn’t seem to make sense to me

1 Like

i found a better way to do it thanks for the help tho

i checked how much the mouse moved in each direction then rotated the camera accordingly

1 Like

No problem, edit the post with your solution or comment and then mark said solution as the solution so others can be helped.

1 Like

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