Compass only works when camera is not looking down or up

I am trying to make this simple compass but I ran into a problem. When my camera is looking down or up the angles are messed up and I can’t figure out how to fix it.

Here is a video of the problem:
Watch 2024-04-10 15-46-47 | Streamable

The angle shouldn’t change when I move the camera to face up or down. Can anyone help me? I don’t know what to do.

Here’s how I got the angle:

local orientation = math.deg(camera.CFrame.LookVector:Angle(Vector3.new(0, 0, -1), Vector3.new(0, 1, 0)))
	
if orientation < 0 then
	orientation = orientation + 360
end
1 Like

I’m not sure what the reason is, but I think I found a solution. You could simply use only the X and Z of the Camera’s LookVector and it should work just fine.

Modified version of your code:

local yLessLookVector = camera.CFrame.LookVector * Vector3.new(1, 0, 1)
local orientation = math.deg(yLessLookVector:Angle(Vector3.new(0, 0, -1), Vector3.new(0, 1, 0)))
	
if orientation < 0 then
	orientation = orientation + 360
end
2 Likes

I may be a little dumb but can’t you simplify this by doing:
local orientation = camera.CFrame.Rotation.X

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