I'm trying to mimic a third person camera in an old game I used to play, and I'm using math.atan2() to calculate the angle at which the camera should rotate, relative to the head (so it's looking at the head). However, when I move my character via the mouse (mouse is locked to the center of the screen), the rotation goes crazy.
I want to 'clamp' it, or prevent it from going completely bonkers.
Here's the code for the camera:
--> Constantly update the camera.
RS:BindToRenderStep( "Camera", 201, function()
local z = camera.CFrame.Z - character.Head.CFrame.Z
local y = camera.CFrame.Y - character.Head.CFrame.Y
pitch = atan2( y, z )
root.CFrame = cf( root.Position ) * ca( -0, rad( yaw * 5 ), 0 )
camera.CFrame = camera.CFrame:lerp(
root.CFrame * cf( 0, -2.5, 7.5 ) * ca( pitch, 0, 0 ),
0.4
)
end )
root is the character's HumanoidRootPart.
cf and ca are both CFrame.new() and CFrame.Angles() respectively.
atan2 is of course math.atan2().
I apologize if what I'm asking is too vague. Here's a gif to illustrate my problem:
I want the camera to pitch up and down, always pointing at the head as the mouse moves up and down (not yet implemented, but I manipulated the CFrame so you could see the angle) without doing what it does now when I rotate the character.
Any help would be appreciated, thank you!

1 comment
Thank you! It really works a charm and I love your explanation on it - thank you and evaera both, I've learned a couple new things today!