How to get the angle that the player's camera tilts from the x - z axes?

It’s a little hard to explain on its own so here’s a picture of my desired target:

The blue and green lines represent the x and z axis that lie on the flat plane. Given that the camera is the arrow pointing in 3d space and that the pink line lies on the x-z plane, how can I find the angle (in purple) from the camera to the pink line?

So the arrow in that picutre is the camera, which has a position and rotation (lookVector), we can ignore the position. We have all the coordinates so we could approach the problem in many ways.

The cleanest solution is to realize the lookVector is a unitVector (length of 1), which means you could just take the inverse (arc) of a length. Since we have the Y component, it’s straightforward.

sin(ang) gives us a length [-1, 1], so asin(length) gives us the angle.

local camera = workspace.CurrentCamera

while wait() do
	local lookVec = camera.CFrame.LookVector
	local ang = math.asin(lookVec.Y) -- in radians
	print(math.deg(ang))
end
16 Likes

I’m confused with what do you mean by unitVector and how it has a length of 1. Could you elaborate more about that? isn’t the lookVector a Vector3?

Ohh, do you mean that the distance from the LookVector Vector3 value and the camera’s position is 1?

A UnitVector describes a distance with length of 1 (a single unit, may be a little off). This is useful in the sense because the camera is not really “looking” at anything, it’s looking in a direction. CFrame.LookVector returns a unit vector, you don’t have a choice.

To answer your question, a LookVector is a Vector3, with the added property that .Magnitude property is approximately 1.

1 Like

So that would mean that the magnitude between the camera position and the LookVector’s Vector3 value is 1?

No, a camera with the same orientation at a different point in space would have the same LookVector. This problem is independent of camera position, you only care about rotation.