Hello. I’m trying to rotate a part using CFrame.new(origin,lookat)
. And, of course, it works fine. However, how can I achieve the same sort of rotation, while at the same time allowing no rotation to occur on the Z axis of the part I am rotating? Fairly simple question, but I can’t figure it out. Thanks
A simple way to do this is by creating a new Vector3 and making Z equal to the origins Z value.
part.CFrame = CFrame.new(part.Position, Vector3.new(lookat.X, lookat.Y, part.Position.Z))
That really just does not work. Moving the Z position of the lookat to the Z position origin doesn’t do anything but create an incorrect position and rotation. I need a way to restrict rotation of the part on the Z axis.
How are you using the CFrame? If you’re multiplying CFrames, just omit the Z from the rotational value. You can also break down a CFrame into its rotational angles via CFrame.ToOrientation, then construct a new CFrame that doesn’t incorporate the Z value and apply that accordingly.
How would I construct a new CFrame without the Z rotational value, after using CFrame.ToOrientation? I’m not multiplying any CFrames, I’m just setting a part’s CFrame using CFrame.new(origin, lookat).
The only way I could think of is to create that new CFrame with CFrame.new(origin, lookat), then get the values from that CFrame’s .ToOrientation. Then I don’t know what to do with those values. Thanks
ToOrientation will return you three values as a tuple: the X, Y and Z representations of approximate angles that can be used to create a CFrame. As such, you only need to get values for the X and Y angles, then ignore the Z. After that, instead of using the LookAt value, multiply the CFrame.
local ACF = Object.CFrame
local CRX, CRY = ACF:ToOrientation()
b.CFrame = CFrame.new(b.CFrame.Position) * CFrame.fromOrientation(CRX, CRY, 0)
Wow. Can’t believe I didn’t think of that. Thanks!