Setting CFrame UpVector

Is there a way that I can create a CFrame with the orientation of one part, but with a different UpVector?

For a BodyGyro, I would like to create a CFrame that has the orientation of a character’s HumanoidRootPart, except I want use use a different UpVector.

2 Likes

All the axes of a cframe (look vector, up vector, right vector) are perpendicular to each other, so yes you can do this, but the up vector would need to be perpendicular to the original look vector. (if not, then the up vector and look vector cannot be part of the same orientation without correcting the up vector, since they are not perpendicular)

You’d take the cross product of the look vector of the original CFrame and the new up vector. This gives you the new right vector. You normalize this one (in case up vector and look vector weren’t entirely perpendicular), and then you cross this right vector to the look vector to create the corrected up vector (again, in case it wasn’t entirely perpendicular). Take the unit of that too.

You then have the three vectors you need to construct the CFrame: the computed right vector, the corrected new up vector, and the look vector of your original CFrame.

14 Likes

Is there a CFrame constructor that uses these three vectors? If not, how would I construct the CFrame?

2 Likes

You can also take the position from the original vector and then use this:

CFrame.fromMatrix ( Vector3 pos, Vector3 vX, Vector3 vY, Vector3 vZ )

(wow copying from the Developer Hub gives a nice syntax here)

By the way, if what I mentioned in the previous post doesn’t work, try swapping the placement of the vectors in the cross product. (I’m fairly sure I wrote it the correct way around, but I’m never 100% on if you do A cross B or B cross A.)

14 Likes