Putting a negative number into a CFrame changes it to a positive number

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I’m trying to make a script that rotates the HumanoidRootPart to the camera pivoting to the head.

  1. What is the issue? Include screenshots / videos if possible!

The issue is that for some odd reason, when I put “math.rad(-90) - -80” (note that the 80 is just a placeholder for the camera’s X rotation, and is supposed to be converted to radians), it says “10” instead of “-10” (in degrees).

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

Yeah, I have. I’ve tried to set the Y rotation to 179 instead of 180, I’ve tried to modify the C1 instead, but nothing’s worked so far.

Video:

The CFrame changer:

local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local camera = workspace.CurrentCamera

local x = camera.CFrame:ToOrientation()

char.HumanoidRootPart.RootJoint.C0 = CFrame.new(
			0,
			math.abs(math.deg(x) / (200/3)),
			-math.deg(x) / (800/15)
		) * CFrame.Angles(
			math.rad(-90) - x,
			math.rad(180),
			0
		)

Sorry if it’s horribly written, but I really need an answer as to why this is happening.

1 Like

thats because how Lua handles arithmetic operations with negative numbers and the order of operations.

math.rad(-90) - x

what you are doing is math.rad(-90) - -80, here:

  1. math.rad(-90) converts -90 degrees to radians (which is approximately -1.5708 radians)
  2. Then you are subtracting -80 (which is equivalent to adding 80)
  3. So you get -1.5708 + 80 = 78.4292 (not the -10 you expected)

here this is the fixed one:

local x_deg = math.deg(x) 
char.HumanoidRootPart.RootJoint.C0 = CFrame.new(
    0,
    math.abs(x_deg / (200/3)),
    -x_deg / (800/15)
) * CFrame.Angles(
    math.rad(-90 - x_deg), 
    math.rad(180),
    0
)

Doesn’t :ToOrientation() output radians?

Oh, I worded this badly. The “math.rad(-90) - -80” is supposed to have the 80 converted to radians. My apologies.

looks like it’s gimball lock issue.

Well, how would I fix gimbal lock? I don’t know how to convert Euler to Quaternion.

Sadly it’s impossible to just fix it.
I need to understand what you want to achieve to help you further.
(Not noticed video, looked, will try to help now)

local LookVector = Camera.CFrame.LookVector
local UpVector = Vector3.yAxis
local RightVector = UpVector:Cross(LookVector)
char:PivotTo(CFrame.fromMatrix(char:GetPivot().Position, RightVector, UpVector, LookVector))

This will make character look at the same direction as Camera, and lock it at Y axis.

1 Like

This isn’t what I was looking for, so let me clarify.

I want it so that the character, when in first person, rotates with the camera. (as in, when I look down, the character looks down), for a smoother first person effect (because the arms will be visible in first person).

Erm…
Do you want ENTIRE character rotate like that, or only head?

Well, realistically I only need the arms, it’s just easier to rotate the entire character. In case you’re asking, I’m not using :PivotTo(), I’m just editing the joint that connects the HumanoidRootPart and Torso.

In this case, change UpVector to local UpVector = Camera.CFrame.UpVector
Also, for joints, just change PivotTo() to setting joint CFrame:
char.HumanoidRootPart.RootJoint.C0 = CFrame.fromMatrix(Vector3.zero, RightVector, UpVector, LookVector)

When I use this, the character is rotated 90 degrees.
image

Try this:

local LookVector = Camera.CFrame.UpVector
local UpVector = -Camera.CFrame.LookVector

This will rotate it by 90 degrees, by swapping rotation vectors.

It works, but the rotation is not pivoted to the head, like I said I wanted it to be.

I don’t get what you meant with that.
You want to rotate character using Head as central point?

Yes. Basically, like this.
image

local LookVector = Camera.CFrame.UpVector
local UpVector = -Camera.CFrame.LookVector
local RightVector = UpVector:Cross(LookVector)
local Offset = char.Torso.Neck.C0 * char.Torso.Neck.C1:Inverse()
char.HumanoidRootPart.RootJoint.C0 = Offset:Inverse() * CFrame.fromMatrix(Vector3.zero, RightVector, UpVector, LookVector)

To do this you need to implement offset CFrame.

It seems to have only moved the character downwards, without pivoting on the head either.