Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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
Use trigonometry to get the angle between two points. Use LookVector "The forward-direction component of the CFrame’s orientation." pidgey 548 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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)
0
Thank you sir laughablehaha 494 — 5y
Ad

Answer this question