How to get Vector3 Orientation from a CFrame

So we know that a CFrame consists of 12 components, the first 3 are the position and the others are the rotation values for the CFrame, my question is, how do I take those other values and turn them into a simple Vector3(x,y,z)?

4 Likes

It’s not possible to represent absolute rotation via a 3 component “vector”. You can use CFrame:ToEulerAnglesXYZ to get 3 angles but it does not physically make sense to put them into a vector. Alternatively you can use CFrame:ToAxisAngle which returns the rotation axis as a V3 and the angle.

I also don’t think it’s possible, but I found out you can still use those components to project the X,Y,Z values using lookvectors.

1 Like

The LookVector doesn’t indicate how the CFrame rotates around the LookVector itself, so you’re losing a bit of information, but there are many cases where just using the LookVector is sufficient. For example, it is known that the camera is almost always aligned horizontally, so it’s fine to omit that information. It depends on your use case.

2 Likes

But shouldn’t you in theory be able to get X,Y,Z in BasePart.Orientation = Vector3.new(x,y,z) because there is a correlation from CFrame lookvectors / components to X,Y,Z rotations?

BasePart.Orientation returns the 3 Euler angles I had talked about. You can keep those inside a Vec3 surely, just don’t be surprised if you get unexpected results if you attempt to do arithmetic with that vector.

I’ll give it a try someday. Thanks for your help.

I managed to do it using attachments. You write a CFrame value into the CFrame property of an attachment parented to the part, then read the Orientation value from it. It’s not the most practical, but it works.

1 Like

You can just use CFrame:ToOrientation() or CFrame:toEulerAnglesXYZ() as they said above. Euler angles don’t have enough data to accurately describe rotation though, so it’s better to learn how to use quaternions or CFrames.

What I do in this situation is make a part, set it to the CFrame, use the part’s orientation, then destroy that part.

that is a very hacky way to do it but it works i guess

It’s not very hacky, it doesn’t cost you much to do it like that, and as far as I know, it is the only way to do it.

it doesn’t really take that much time to look at the documentation

CFrame:ToOrientation does not return a Vector3 orientation. It gives 3 separate numbers that are more of a ratio rather than an orientation. Those 3 numbers cannot (easily) be converted into degrees (which the Orientation Vector3 uses).

Hope this clears stuff up!

1 Like

Actually, it’s not a “ratio”, it’s just that angle approximated in radians.

So to answer my question, here is how to get a Vector3 orientation from a CFrame:

function CFrameToOrientation(cf: CFrame)
	local rx, ry, rz = cf:ToOrientation()
	return Vector3.new(math.deg(rx), math.deg(ry), math.deg(rz))
end
21 Likes

Oh! That explains why when I had a part with (0,0,0) orientation, I got very small numbers in return. I thought it was just one of the CFrame things with 3 different numbers.

1 Like