Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help getting rotation?

Asked by 9 years ago

The following post includes a set of pictures and explanations of what I want to do.

Picture 1 (Click) This picture shows a brick tilted by 30 degrees on it's Z axis. If I do print(workspace.Part.Rotation.Z), it gives me 150, which is perfectly fine.

Picture 2 (Click) This picture shows the brick after rotating it by 15 degrees on its Y axis and its X axis. Now if I go ahead and do print(workspace.Part.Rotation.Z), it gives me 153.967, which is not what I want.

My question is, how would I go about getting the same Z rotation of a brick even when it's X and Y is rotated at the same time? Thank's for the help.

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

You are asking for the roll around the part's Z-axis.

A way to compute that is this:

local _, _, z = CFrame.new(part.Position, part.Position+part.CFrame.lookVector):toObjectSpace( part.CFrame):toEulerAnglesXYZ()

There are two parts to this:

1) We use the observation that if we get a "normal" X and Y rotation, we can straight out read the Z rotation (using :toEulerAnglesXYZ() which is the same as using the .Rotation as the part)

2) We can "undo" the X and Y spin because it's easy to make a CFrame with 0 roll, but the same X and Y rotation.

local zeroRoll = CFrame.new(part.Position, part.Position + part.CFrame.lookVector)
local justRoll = zeroRoll:toObjectSpace( part.CFrame )
local zero1, zero2, roll = justRoll:toEulerAnglesXYZ()
-- roll is in radians
local rollDegrees = math.deg(roll)

This can be condensed into the first snippet in this answer.

Ad

Answer this question