Trying to get orientation values from a CFrame

I’m trying to get what would be x, y, and z orientation values from a CFrame, using the “convert” function. I used the code to point a part towards another one, then compare what I got from “convert” to the actual part’s orientation. The x and y values seem to be fine, but it looks like z is nonsense.

Sometimes z would print as 0, as it should, but sometimes z would print as “9.6435354297272e-07” or something similar.

It also seems a little . . inaccurate . .
I have no idea what else to do.

``````convert = function(cf)
local x, y, z = cf:ToOrientation()
x = x * (180 / math.pi)
y = y * (180 / math.pi)
z = z * (180 / math.pi)

return x, y, z
end

lookat = CFrame.new(workspace.fora.Position, workspace.at.Position)
x, y, z = convert(lookat)
workspace.fora.CFrame = lookat
print(x, y, z)
``````
6 Likes

I think you’re making this a bit complicated lol
I’m not sure if this will work but to get the rotational values you can do:

``````local newCFrame = CFrame.new(workspace.fora.Position, workspace.at.Position)
local x, y, z = newCFrame:ToOrientation()
workspace.fora.CFrame = newCFrame
print(x, y, z)
``````

Have you tried using CFrame.LookVector?

Then the values for x, y, and z would only be going up to math.pi instead of 180 or 90.

If you’re just trying to get a CFrame that has the orientation without the position, try this:

``````local partCFrame = workspace.Part.CFrame
local orientation = partCFrame - partCFrame.p
``````

This will set the `orientation` CFrame’s position to 0,0,0 with maintaining the orientation.

6 Likes

I’m trying to get the values with a CFrame and only a CFrame.

This is probably caused by rounding and/or dividing by zero somewhere.

The wiki even says approximate

This isn’t the most efficient way to do it, but you could always try something like

``````local Part = Instance.new('Part')
function getOrientation(cf)
Part.CFrame = cf
return Part.Orientation
end

``````

I don’t know why they have to approximate it if the above function shows that it’s possible to not have to approximate it…

2 Likes

The developer hub does mention that `CFrame:ToOrientation()` returns approxmiate angles to regenerate the CFrame, but 9.6435354297272e-07 is a really small number – this is in scientific / index notation to represent 0.00000096435354297272

An alternative is by using some trigonometric functions:

``````local sx, sy, sz, m00, m01, m02, m10, m11, m12, m20, m21, m22 = CFrame:GetComponents()

local X = math.atan2(-m12, m22)

local Y = math.asin(m02)

local Z = math.atan2(-m01, m00)
``````

You of course have to convert it to degrees because all trigonometric functions in the math library use and / or return radians by using math.deg or the same math you used to convert it

42 Likes