Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How Do I Get A Camera's 'Y' Angle in Degrees?

I have tried many methods to get the camera's Y angle, and I have a slight intuition that I can obtain the Y angle using some trigonometry, but I don't know how to do that. How do I get the current camera's y angle in degrees as a full 360° rotation?

I've already tried these methods:

local cam = workspace.CurrentCamera

-- eg #1
local ex, ey, ez = cam.CFrame:ToEulerAnglesXYZ()
local rotation = ey -- this give the values from -2 to 1. ????

-- eg #2
local rotation = cam.CFrame.Angles() -- error

-- eg #3
local rotation = math.asin(cam.CFrame.lookVector.Y) 

1 comment

1

Use trigonometry to get the angle between two points. Use LookVector "The forward-direction component of the CFrame’s orientation."

1 answer

✓ Accepted answer

Indeed, we can use the CFrame.LookVector and trigonometry to get something like what you want.

I figured this out by drawing a right-angle triangle representing the coordinates of the LookVector. I labelled the bottom line as 'x' and the vertical line as 'y'. Trigonometry says that you can get the angle you're interested in with math.atan - but there's a problem: we have to consider the 'z' component. Fortunately we can use the Pythagorean Theorem to combine x & z.

-- Example you can put in the command bar
v = workspace.CurrentCamera.CFrame.LookVector
print(math.deg(math.atan2(v.Y, math.sqrt(v.X^2 + v.Z^2))))
-- math.sqrt is the Pythagorean Theorem part, combining x and z
-- math.atan2 is the inverse tangent (split into two arguments, the 'y' component and the 'x' component)
-- math.deg converts it from radians into degrees
-- the result ranges from -80 when you're looking nearly straight down to +80 when you're looking nearly straight up (Roblox evidently prevents the default camera from looking straight up/down)

1 comment

0

Thank you sir

This discussion is preserved as it was. New answers, comments, and votes are closed.