Rotating camera gives undesirable results

I use math.atan2 to rotate the camera of my viewport. When rotating using the y-axis it seems to barely move when the mouse is on the right side of the screen, as opposed to moving more fluidly when the mouse is near the furthest left side of the screen. This doesn’t not happen when using other axis’s.

Code:

local Angle = math.atan2(MouseOfPlayer.X - Object.Position.X,  MouseOfPlayer.Hit.Z - Object.Position.Z) -- MouseOfPlayer refers  to the mouse, Object refers to the part in viewport
local RotatedCFrame = CFrame.new(Object.Position) * CFrame.fromEulerAnglesXYZ(0, Angle * math.pi * 2, 0)
Camera.Focus = Object.CFrame
Camera.CFrame = RotatedCFrame:ToWorldSpace(CFrame.new(CameraOffset)) -- Apply some offset
Camera.CFrame = CFrame.lookAt(Camera.CFrame.Position, Object.Position)

Using Y axis:

Using Z axis: Works fine

  • I’m assuming the teddy bear is close to 0,0,0 because you are using viewport frame.

If your mouse position is hitting somewhere behind the direction of the bear it’s going to be turning it’s back on the view port frame camera’s facing direction. Basically it’s getting the world’s mouse position not the screens so it looks at where the vector3 of the mouse would be at. A possible solution could be using Mouse.X and mouse.Y positions

https://developer.roblox.com/en-us/api-reference/property/Mouse/X

I was previously using the mouse x and y coordinates and it worked out perfectly, but I wanted to use math.atan2 in this scenario which as you can see didn’t go so well, well at least not when rotating around y axis. I had assumed this would due to using Mouse.Hit.Z (world position) like you mentioned and would like to ask if there is a way to get the Z coordinate of the mouse in screen position? Nvm lol the Z coordinate only exists in 3D space. Anyways thanks!

New Code:

local RotatedCFrame = CFrame.new(Object.Position) * CFrame.fromAxisAngle(Vector3.new(0, 1, 0), Mouse.X * 1/50)
Camera.Focus = Object.CFrame
Camera.CFrame = RotatedCFrame:ToWorldSpace(CFrame.new(CameraOffset))
Camera.CFrame = CFrame.lookAt(Camera.CFrame.Position, Object.Position)