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

How do you rotate a part?

Asked by 9 years ago

I wanted to know how you would rotate a part using scripts. Not sure how its done so I couldn't really put together a LUA script for it.

2 answers

Log in to vote
1
Answered by 9 years ago

The library you're looking for is called 'CFrame'. CFrame holds functions that deal with the 3D positioning and rotation of all physical objects in the game (all parts).

CFrame.new

The 'new' function within the CFrame library returns an entire new CFrame value, that includes both position and rotation. The first 3 arguments (which should be integers) determine the position, and the rest (other 6 arguments) determine rotation. It can be a bit confusing at first, but you can get the hang of it real fast if you mess around with it. Here's an example:

Position

local Part = Instance.new("Part",workspace)
Part.Anchored = true

Part.CFrame = CFrame.new(0,10,0) -- Just like Vector3.new, this will position the part at X: 0, Y: 10, Z: 0

Now, if you've ever noticed that if you print the 'CFrame' property of a part, you get 9 numbers. These numbers correspond to how I've explained it above; first 3 being position, and the rest being rotation.

Rotation

To make things easier with dealing with rotation, there's a special function called 'Angles' that you can index in CFrame. This function messes with rotation directly, and avoids any other positioning in the CFrame. You can combine a newly created CFrame with CFrame.Angles() by multiplying them together (as you would with all CFrame values) like this:

local Part = Instance.new("Part",workspace)
Part.Anchored = true

Part.CFrame = CFrame.new(0,10,0) * CFrame.new(0,0,2) -- Positions the part at 0,10,0, and rotates it about 114 degrees on it's Z axis.

Knowing your rotation

Now, another thing we need to realize here is that rotation deals with radians. One radian is about 57 degrees, and there are about 6.3 radians in a complete rotation (another great way to memorize this is knowing Pi is exactly half a circle, and Pi*2 rounded to the nearest tenth is 6.3)

So saying something like:

local Part = Instance.new("Part",workspace)
Part.Anchored = true

Part.CFrame = CFrame.new(0,10,0) * CFrame.new(0,math.pi,0)

Would rotate the part exactly 180 degrees on it's Y axis (since Pis is half a circle, and there are 360 degrees in a circle, we get 180 degrees).

For any other additional information, I suggest visiting this site:

http://wiki.roblox.com/index.php?title=CFrame

And if you have any other questions, just leave a comment and I'll try and help.

Ad
Log in to vote
0
Answered by 9 years ago

Along with reading the wiki as suggested by ScriptGuider in his answer, I'll add-on by giving some lines of code that would rotate.

First, you should know that both CFrame.Angles() and CFrame.fromEulerAnglesXYZ() are the EXACT same things.

Here is an easy way to rotate something:

game.Workspace.Part.CFrame = CFrame.Angles(math.rad(),math.rad,math.rad())

You may use math.rad or math.deg to rotate (With Degrees/Radians, wiki this if you want to know more) or you can just put in a number which will still rotate the part. I suggest using the link given by ScriptGuider for more information.

Answer this question