CFraming Guide

From Legacy Roblox Wiki
Revision as of 17:13, 22 April 2012 by >SoulStealer9875 (fix name)
Jump to navigationJump to search

Intro

This guide was written by theopfor, read the forum thread here.

I heavily encourage the use of command bar. People may say that plugins, tools, and CmdUtl is faster and more efficient but honestly you gotta CFrame like a pro and use command bar.

Edited a few items and tried to clear up a few parts of this guide for everyone.

Preparation

First things first open Studio mode or Studio. Next View>Toolbars>Command. That is the command bar. Pretty small object but is powerful if you use it right. This is where you put the code.

Also the brick you are CFrame MUST BE ANCHORED!!!! I learned this the hard way and I do not want anyone to be discouraged from command bar by this one problem. Another thing is that with the command that will soon be shown all you have to do is select the brick. Also bricks cannot be welded when you CFrame them. Weld them afterwards. After rotating a brick if you drag it the brick will flatten which is bad. I suggest using the Move On Axis tool when moving CFramed parts.

USE THE WHOLE CODE GIVEN, OTHERWISE THIS WILL NOT WORK!

Tutorial

The command looks very complex but I will break it down into the chunks you need to know. Here is the command:

for i = 1,#game.Selection:Get() do game.Selection:Get()[i].CFrame = game.Selection:Get()[i].CFrame * CFrame.new(0,0,0)* CFrame.Angles(math.rad(0), math.rad(0), math.rad(0)) end

To use the command like I said looks complex but give it a chance! There are two things we need to pay attention to here. The first one is CFrame.new(0,0,0). CFrame.new is CFraming the brick. This doesn't rotate it just translates on the axis. The unit of measurement is studs. With the command bar you can go into the hundred thousandths of a stud. Might not ever need to go down that far but you never know. So the first 0 is the X axis. If you have learned about grids in school then you should know what it is but I will explain it anyways. The X axis is side to side or think of it as left to right. The next 0 is the Y axis. Y is very easy to keep track of because it only controls the up and down position. Next we have Z. The Z axis controls depth. Depth you can think of as forwards or backwards. You can also think of the Z axis as distance. Please note that these axises are not the adjusted to the brick but to the world grid. People are discouraged by this part sometimes because they get confused but you gotta try this. Real quick to achieve!

Next we have rotation! Rotation is very very useful. The line for rotation is:

CFrame.Angles(math.rad(0),math.rad(0),math.rad(0))

This looks a little confusing but when I explain it you will understand it more. Now rotation is a little different for rotation so you have to experiment a little. The wiki teaches you to use radians and most people are better with angles. Lets break this down a little. CFrame.Angles is identifying the type of CFrame. math.rad() is converting the number to a radian for CFrame to use. Kinda quick to explain. Studio does have a built-in rotate tool. You might say "YAY!!!" but it is ever so inaccurate you can't rotate anything with it.

Tutorial Explanation

CFraming does take practice too so don't seem discouraged at first! Also for these commands forward will require some simple scripting knowledge of how to locate bricks. Now the wiki teaches you two things that you cannot do with the command provided so it is a little lengthy. You need to locate the brick then put in in some other stuff and is easier to explain through an example.

workspace.brick.CFrame=workspace.brick2.CFrame

That right there will position brick inside brick2. Can be handy in many cases but once again can be lengthy. The other thing that the wiki shows you that you cannot do with the code provided is CFrame.new(). CFrame.new moves the part to the exact coordinates provided.

workspace.brick.CFrame=CFrame.new(12,6,54)

That will position the brick at the exact grid coordinates specified. Here is the what the wiki version teaches you for CFraming like the code above. This code doesn't really need any breaking down for it is fairly simple but I will explain it anyways for the benefit of others.

workspace.Brick.CFrame=workspace.Brick.CFrame+Vector3.new(1,1,1)

That command moves the brick 1 stud to the right, 1 stud up, and 1 stud forward. Now we are locating the brick with this part: "workspace.Brick". Next we are changing the CFrame property. We take the current CFrame value then add the Vector3 value of 1,1,1 to the brick that moves it.

Some More Stuff

Some people really like to make vehicles but to make some nice ones CFrame really helps. Well CFrame must be Anchored so this can create problems. Well lucky you this guide explains how! To have unAnchored CFrame you must have an Instance Weld script. The Weld script puts in Instance Welds. An Instance Weld keeps the welded brick at the same distance so they move together. This is very useful! Some people also have the problem of planes or vehicles lag moving and this might also solve it.

CFrame Constructors, Properties, and Matrix

CFrame also has some constructors and properties. Let's just start with the matrix. Here is what a matrix can look like:

1,23,14,3,4,7,1,5,8,1,4,5

The first three numbers (1,23,14) are the position of the object. Pretty simple. The next 9 (3,4,7,1,5,8,1,4,5) describes the way the object is rotated. I can't think of a use for a matrix but maybe you can. Next we have the constructors:

CFrame.new(v) This creates a CFrame from the Vector3 value of v. CFrame.new(v,l) This creates a CFrame from the Vector3 of v that looks at the l (a Vector3 value).

Those are the only two that aren't explained during this tutorial. Next for the properties. These can be useful for many things but I will let you figure out what you can use them for. Properties:

CFrame.p Is the 3D position of the CFrame CFrame.x(CFrame.y and CFrame.z are the same as this) is the value of the X axis in the CFrame CFrame.lookVector Is where the brick is facing. This is very useful in scripting!

lookVector

Now I want to dedicate a section to lookVector because of how useful it is. lookVector is used mainly for scripters. What lookVector does is returns the rotation matrix of the brick's CFrame (see "CFrame Constructors Properties, and Matrix"). So if I wanted a block to move forward but it was rotated at an unkown angle I would use lookVector. There are only four operations you can do with lookVector: add, subtract, multiply, and divide. Each one moves the brick in a direction. Add and subtract move the object to it's side while multiply and divide move it forward and backward.

Example
workspace.Part.CFrame=workspace.Part.CFrame.lookVector * 15 (Moves forward)
workspace.Part.CFrame=workspace.Part.CFrame.lookVector / 15 (Moves backward)
workspace.Part.CFrame=workspace.Part.CFrame.lookVector + 15 (Moves to one side)
workspace.Part.CFrame=workspace.Part.CFrame.lookVector - 15 (Moves to the opposite side of +)