How do I make a CFrame’s rotation match a part’s orientation?

I’m trying to position my model at a certain point using SetPrimaryPartCFrame(), and while the position is correct, I can’t seem to get the rotation right. I want its rotation to match the Orientation of another part.

I’ve tried this to no avail (The angle is wrong):

model:SetPrimaryPartCFrame(cframe * CFrame.Angles(part.Orientation.X, part.Orientation.Y, part.Orientation.Z))

I have no idea what I’m doing.

8 Likes

CFrame.Angles applies the angles in XYZ order, which matches the way the Rotation property works.
The Orientation property’s angles are ordered YXZ (which generally makes more sense than XYZ). You should use CFrame.fromOrientation instead, this function also applies the angles in YXZ order.

6 Likes

Okay. I’m away from my computer now but I will try it when I can. Thanks.

(Although I think it’s really counterintuitive to have one property set to YXZ when everything else is XYZ…)

Somewhat recently YXZ ordered properties/functions were added (Part.Orientation, Attachment.Orientation, CFrame.fromOrientation, CFrame.fromEulerAnglesYXZ …), which are semi-replacing the older XYZ stuff (Part.Rotation, Attachment.Rotation, CFrame.Angles, CFrame.fromEulerAnglesXYZ …), I believe.
Probably simply because the YXZ order is much easier to work with in most cases.

It’s too bad I’ve been using XYZ for the past 4 years, I just foolishly expected everything to be that way.

What I do when I only need the rotation of a CFrame is to do myCFrame - myCFrame.Position, which translates the position of the CFrame to (0, 0, 0).

Given that, you can then just do cframe * (part.CFrame - part.Position), in case cframe isn’t rotated already.

29 Likes

Very cool. I will look into that as well when I have the chance :slight_smile:

1 Like

The orientation is still wrong (m is the model, cp is my part, ignore hp):

m:SetPrimaryPartCFrame(CFrame.new(Vector3.new(hp.Position.X, 1, -220.85)) * CFrame.fromOrientation(cp.Orientation.X, cp.Orientation.Y, cp.Orientation.Z))

Am I doing that right?

1 Like

Apparently the Orientation property is in degrees, but all those CFrame functions take radians, so you’d have to convert those degrees to radians first using math.rad.

(Although using Orientation/fromOrientation to copy the rotation of a CFrame works perfectly fine, I’d definitely use the method @As8D described, though.)

3 Likes

As8D’s solution still doesn’t help me. This is really frustrating since I’m trying to get a whole model to move, and only one part is anchored and welded to all the other parts, which are unanchored. If I just do CFrame.new(Vector3.new()), it’ll remove the orientation.

If it’s just an issue of moving a single anchored part, you don’t need to bother with the rest of the model since they’ll adapt the orientation by nature of being welded to the anchored part. Get the rotated CFrame of your target part and apply it as a multiplication to the model root’s current CFrame.

There’s also AlignOrientation.

2 Likes