Having trouble rotating camera

So I’m basically just having trouble rotating the camera to a parts orientation + an offset.
This is the code I have:

Camera.CFrame = CFrame.new(CurrentCameraPart.Position) * CFrame.fromEulerAnglesXYZ(math.rad(CurrentCameraPart.Orientation.X), math.rad(CurrentCameraPart.Orientation.Y + InCameraRotation.Value), math.rad(CurrentCameraPart.Orientation.Z))

Now, my problem is that I have a part facing down, specifically -20, 145, -0, but when I use that code to position the camera, it is facing up and rotating incorrectly in general.
The Y rotation is correct, but the X and Z aren’t, and even weirder is that the code works perfectly fine when the X orientation is 0.

The camera orientation is this when X should be -20 and Z should be 0: -6.718, 108.882, 18.882

Did I mess up somewhere?
How do I fix this?

Can’t test right now, but my best guess is that you don’t need to math.rad() the X and Z values since they’re already in radians.

1 Like

That certainly isn’t the solution because this just happens when I remove the math.rad()

What’s the orientation of the camera when you remove math.rad()?

1 Like

18.195, 98.448, -64.56

Also, the code for X and Z use Part.Orientation, not CFrame.Rotation so they aren’t already in radians.

Try using :Inverse() for the Rotation.

1 Like

If you want to use XYZ angles, use fromOrientation instead of fromEulerAnglesXYZ. The latter applies rotations Z first, which is backwards from how orientations are applied.

However, you should probably use fromAxisAngle instead for a number of reasons.
camera.CFrame *= CFrame.fromAxisAngle(Vector3.Y, math.rad(InCameraRotation.Value))

When you have the time, I recommend learning matrices or quaternions and moving away from XYZ rotations. XYZ is here for convenience, but it doesn’t actually contain enough data to handle rotation.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.