I noticed that when setting part positions and sizes you use two main functions: Vector3, and CFrame. I know the CFrame stands for Coordinate Frame, and that Vector3 is used to set sizes of parts, but what’s the difference?
One of the major impacts of CFrame is that that axis is local and based on where your part is, not the world space/direction of that part.
For example if you want a part to move 3 studs in front of you at all times then CFrame could do that for you, where as Vector3 couldn’t do that easily.
CFrame contains the Coordinate Frame as well as the LookVector.
Ok. I see. But for world space applications, like moving to a specific object, Vector3 will do that for you?
You could use Vector3 to move a part to a certain location, you should keep in mind that the parts orientation wont replicate however.
If you use CFrame the Orientation as well as the Position of it will replicate.
Vector3 is the position in 3D space, whereas CFrame is made up of many components, all boiling down to position and orientation.
You can set a position of a part by changing the Vector3 value of the part. Example:
Part.Position = Vector3.new(X,Y,Z)
CFrame can be used to position a part and give it a direction. Example:
Part.CFrame = CFrame.new(Vector3.new(X,Y,Z),Vector3.new(X,Y,Z))
The first part gives the part a position and the second part makes the part face in the direction of that position.
I recommend checking out @AxisAngle’s CFrame topic posted above
Nice explanation. I am beginning to see the light
CFrames have both
orientation; and these can both be manipulated to produce your desired results. CFrames enable you to work quickly and efficiently within an object’s space via
[LookVector, UpVector, RightVector] and their inverses
[-LookVector, -UpVector, -RightVector].
When first being exposed to these concepts it can be hard to understand. So, let’s look at what vectors actually are - so that we can better understand the
directional vectors listed above.
A vector is a quantity that has both
magnitude. If a vector has the same
direction and the same
magnitude as another; then
those vectors are equal - no matter where they are located. Concepts like this are important to understand as you learn. A video which well introduces this concept can be found here.
It is easy to look at and understand vectors inside of something you are probably a lot more familiar with - being a coordinate plane. Vectors in a coordinate plane are vectors that have two components.
Vector [X, Y] - notice within this array we don’t mention position anywhere.
[2,2] might look like either of the following or any vector equal to them.
This is 2 units to the right (X) and 2 units up (Y)
When thinking about Vector3s - think about a similar array, but instead one that has three components,
[X, Y, Z]
magnitude: The length of the given vector in units (studs in Roblox)
unit: The direction of the given vector
CFrame.[RightVector, UpVector, LookVector, -RightVector, -UpVector, -LookVector] are all directional vectors which face outward from their specified face of the object and have a
magnitude of 1.
local myself = workspace.SummerEquinox local my_lookvector = myself.HumanoidRootPart.CFrame.LookVector
In this example,
my_lookvector is a
directional vector which represents the direction the front of my torso is facing. If you did,
myself.HumanoidRootPart.Position + my_lookvector*5 - this would be a position ~~5 studs in front of the HumanoidRootPart.
There is a lot to learn about Vector3s and CFrames and it all comes with time and practice.
I am by no means super great at any of this - in fact I need help with it all the time; but hopefully this allows you to understand at least enough to get started. As others have mentioned - AxisAngle has great explanations on both Vector3s and CFrames.
Along with the information everyone else has provided, when positioning a part through Vector3 (with
BasePart.Position), overlap checks take place; if the part’s new position causes overlapping with another, the part’s height increases until it is no longer overlapping any parts. (This happens even when the part is anchored.)
BasePart.CFrame to position a part, on the other hand, does not check for overlaps. This means you can position parts much more precisely without worrying about them being moved out of the way of other parts.