Calculate / find out camera's CFrame.Angles

  1. What do you want to achieve? Keep it simple and clear!
  • Find out camera’s CFrame.Angles.Y to fill in the “currentY” variable
  • Convert / Calculate camera’s lookVector.Y to CFrame.Angles.Y (I don’
    t know if this would work)
  • I want to set the currentY to camera’s CFrame.Angles.Y but i have no idea how to find it. So i used lookVector.Y to calculate the CFrame.Angles.Y (Code below)
  1. What is the issue? Include screenshots / videos if possible!

I found out that value between lookVector.Y and CFrame.Angles.Y aren’t same

 -- calculation to find rotation X
local currentX = math.atan2(-camera.CFrame.LookVector.X, -camera.CFrame.LookVector.Z) / math.pi * 180

 -- Need help here to find rotation Y
local currentY = camera.CFrame.LookVector.Y * 80

-- Set current X and Y for the variables below
local angleX, targetAngleX = currentX, currentX
local angleY, targetAngleY = currentY, currentY

local function onUpdateCamera(actionName, inputState, inputObject)
	userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	local sensitivity = 0.4
	local delta = Vector2.new(inputObject.Delta.X * sensitivity, inputObject.Delta.Y * sensitivity)
	local x = targetAngleX - delta.X
	local y = targetAngleY - delta.Y
	
	targetAngleX = x
	targetAngleY = math.clamp(y, -80, 80)

	angleX = angleX + (targetAngleX - angleX) * 0.35
	angleY = angleY + (targetAngleY - angleY) * 0.35

	camera.CFrame = CFrame.new(camera.CFrame.p)
	* CFrame.Angles(0, math.rad(angleX), 0)
	* CFrame.Angles(math.rad(angleY), 0, 0)

	local lookVectorY = camera.CFrame.LookVector.Y * 80
	print(angleY, lookVectorY)
end

Output

 7.2999205150142 10.165059566498
 36.805788673335 47.9283618927

LookVector.Y between X and Y axis should be 0.5 but it doesn’t

In my mind,

  • LookVector.Y = 0; CFrame.Angles.Y = 0

  • LookVector.Y = 0.5; CFrame.Angles.Y = 45

  • LookVector.Y = 1; CFrame.Angles.Y = 90

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I searched up in forum and doesn’t see any question related to my problem.
I tried to do the calculation between lookVector and CFrame.Angles and doesn’t achieve my goal.

  1. Addition

Some of the reply i might not understand because the calculation confused me or i wrongy used lookVector to calculate CFrame.Angles.
If theres any coding mistake please correct me to improve my coding skill. Thanks :smile:

1 Like

CFrame:ToEulerAnglesXYZ(): Returns approximate angles that could be used to generate CFrame, if angles were applied in Z, Y, X order

Found on the CFrame wiki page.

1 Like

Thanks for your reply but i have no idea how can i use CFrame:ToEulerAnglesXYZ ( ) to find the camera CFrame because the wiki page just shown the format. Can you give some example and explanation? If can, I’ll be very grateful.

I tried use CFrame:ToEulerAnglesXYZ ( ) and here come the result

script.Parent.Changed:Connect(function()
    local x, y, z = script.Parent.CFrame:ToEulerAnglesXYZ()
    print("Orientation.X = "..script.Parent.Orientation.X.."; CFrame.Angles.Y = "..x * 60)
end)

Output

Orientation.X = 5; CFrame.Angles.Y = 5.2359871566296
Orientation.X = 45; CFrame.Angles.Y = 47.123891115189
Orientation.X = 90; CFrame.Angles.Y = 94.247782230377

Thanks alot @ee0w :grinning: I learnt that from your valuable reply. Is there any way more accurate than this method or i used the CFrame:ToEulerAnglesXYZ ( ) wrongy?

My mistake! I meant CFrame:ToEulerAnglesYXZ(). Very similar, yet very different.
BasePart.Orientation uses this to generate the human-readable angles.

4 Likes

Just one thing to point out;
.Changed will fire when any property changes, if you want it so only when their CFrame then use :GetPropertyChangedSignal("CFrame")

2 Likes

Thanks @ee0w and @return_end1’s remind. After i calculated the multiply value for CFrame:ToEulerAnglesXYZ ( ) and is it good using 57.29578607161983 instead of 57.3?

Finished product

local currentX = math.atan2(-camera.CFrame.LookVector.X, -camera.CFrame.LookVector.Z) / math.pi * 180
local currentY = camera.CFrame:ToEulerAnglesYXZ() * 57.29578607161983

local angleX, targetAngleX = currentX, currentX
local angleY, targetAngleY = currentY, currentY

local function onUpdateCamera(actionName, inputState, inputObject)
	userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	local sensitivity = 0.4
	local delta = Vector2.new(inputObject.Delta.X * sensitivity, inputObject.Delta.Y * sensitivity)
	local x = targetAngleX - delta.X
	local y = targetAngleY - delta.Y
	
	targetAngleX = x
	targetAngleY = math.clamp(y, -80, 80)
	angleX = angleX + (targetAngleX - angleX) * 0.35
	angleY = angleY + (targetAngleY - angleY) * 0.35
	camera.CFrame = CFrame.new(camera.CFrame.p)
	* CFrame.Angles(0, math.rad(angleX), 0)
	* CFrame.Angles(math.rad(angleY), 0, 0)
	local currentAngleY = camera.CFrame:ToEulerAnglesYXZ() * 57.29578607161983
	print(angleY, currentAngleY)
end

contextActionService:BindAction("UpdateCamera", onUpdateCamera, false, Enum.UserInputType.MouseMovement)

Output

0.76822881139907 0.76822892320774
0.03392139469494 0.033921398013951
-0.49425539898708 -0.49425543672447
2 Likes