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

Question about CFrame and vector3 values?

Asked by 5 years ago

I use both, but I never understood when to use CFrame or when to use vector3. Is there a pattern for when you should use CFrame and position?

1 answer

Log in to vote
4
Answered by 5 years ago
Edited 5 years ago

CFrame and Vector3

CFrames are actually a combination of position and rotation. If you print a part's CFrame, for example, you will get the part's position followed by its orientation as a 3x3 matrix.

CFrames are not the same as Vector3 values. Vector3 values are matrices, whereas CFrames consist of multiple matrices (Vector3 position, Vector3 rotation/orientation).

But what about when you use CFrame as opposed to when to use a Vector3?

For character teleportation, CFrame does not do anything with the welds currently in place, whereas Vector3 will move the given part without the joints that that one part is welded to. That is why you would do this...

char.HumanoidRootPart.CFrame = CFrame.new(5, 0, 5)

...over this:

char.HumanoidRootPart.Position = Vector3.new(5, 0, 5)

For locating a part not attached to any other parts, use Vector3. This is because there are no welds to "break" on the lone part.

For rotation, a part's Rotation property is good, but CFrame is also good to use. For rotation with CFrame, it does not matter whether you use CFrame.Angles() or CFrame.fromEulerAnglesXYZ() as they both do the same exact thing:

script.Parent.CFrame = script.Parent.CFrame * CFrame.Angles(math.rad(10), 0, 0)

A part's rotation alone can either be done with CFrame or Vector3. Personally, CFrame is easier for rotating a part.

Vector3 should also be used when dealing with any Vector3 value, whether it be a part's Size or a part's Orientation.

Like Vector3, CFrame has different types of itself. There are five types of CFrames:

  1. CFrame.new(Vector3 pos). Creates a CFrame from a given Vector3 position.
  2. CFrame.new(Vector3 pos, Vector3 lookAt). Creates a CFrame with a Vector3 position and a Vector3 to where a part points itself to.
  3. CFrame.new(X, Y, Z). Creates a CFrame from the given coordinates. This is the exact same thing as doing CFrame.new(Vector3 pos).
  4. CFrame.new(X, Y, Z, qX, qY, qZ, qW). Creates a CFrame from X, Y, Z coordinates and quaternion values.
  5. CFrame.new(X, Y, Z, R00, R01, R02, R10, R11, R12, R20, R21, R22). Creates a CFrame with X, Y, Z coordinates and an orientation represented by a 3x3 rotation matrix.

CFrames also have Vector3 properties:

  1. CFrame.LookVector. This is the forward-looking Vector3 within the CFrame's orientation.
  2. CFrame.RightVector. This is the right-direction looking Vector3 within the CFrame's orientation.
  3. CFrame.UpVector. This is the upward-looking Vector3 within the CFrame's orientation.
  4. CFrame.Position. The position Vector3 within the CFrame.

Performing math between CFrames and Vector3's is also possible. Multiplying a CFrame by a Vector3 returns a Vector3 whereas adding or subtracting them returns a CFrame.

The last thing about CFrames is that, unlike Vector3's, they have methods you can use. Here are some important ones:

  1. CFrame:Inverse(). Returns the inverse of the CFrame (the opposite of the CFrame).
  2. CFrame:Lerp(goal, alpha). This method, commonly used with moving things, returns a CFrame interpolated between the CFrame and the goal by the fraction alpha.
  3. CFrame:GetComponents(). Returns a tuple consisting of the components of a 12-parameter CFrame (as mentioned above) with the position and 3x3 rotation matrix.

That's all I have to say. If you have any questions, feel free to ask them.

0
-1 for "Like Vector3, CFrame has different types of itself." User#24403 69 — 5y
Ad

Answer this question