I need help to understand CFrame

well, you can do this!

local Part = workspace.Part
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()


game:GetService("RunService").Heartbeat:Connect(function()
	local Y = 	Character.HumanoidRootPart.CFrame.LookVector -- LookVector Is where it's facing!
	local X = CFrame.new(Character.HumanoidRootPart.Position + (Y * -10)) -- Creates the position, and puts the position BACK according to the LookVector, Multiplying negative to put it in the back!
Part.CFrame = X
end)
1 Like

Hmmm, yes i see im gonna train it thank

You can also use arithmetic operators on CFrames.

Adding a Vector3 to a CFrame will translate the CFrame’s position in world space.
Multiplying a CFrame by another CFrame will translate transform it in object space, and this can be done for rotation as well.

Building on @varjoy’s example:

local Part = workspace.Part
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

game:GetService("RunService").Stepped:Connect(function()
   -- 5 studs behind the HRP
   Part.CFrame = Character.HumanoidRootPart.CFrame * CFrame.new(0, 0, 5)
end)
1 Like

You should use the developer.roblox.com website to check the basics of CFrame. You can even use YouTube to search for tutorials.

The * is used for multiply but why use it in this case?

Part.CFrame = Character.HumanoidRootPart.CFrame *  CFrame.new(0, 0, 5)

You guys should continue this in DMs if you wanna learn/teach more.

I hope that can be helpful for you.

1 Like

CFrames have a position component and an orientation component represented by a 3x3 matrix. Using the * operator on CFrames performs a matrix composition. (That’s because the CFrame data type has a metatable where the __mul method is implemented, but that’s not important here.)

That code will create a new CFrame that has the same orientation as HumanoidRootPart, but offset by 5 studs in the positive Z direction, which in Roblox’s coordinates is backwards relative to the part.

2 Likes

CFrame means basically changing every properties that uses Vector3

Not quite. It is just a single data type that contains data on both a position and an orientation, sort of analogous to the ‘Transform’ object in Unity.
Every BasePart has a CFrame property, but the CFrame object is not specific to just BaseParts.

You can read about it in the links that @Dev_Vegas posted.

Position, orientation ? am i right?

Vector3 is for position, while orientation is rotation.

I know.
CFrame can change these things (position, orientation) whereas vector3 change only position

Vector3 can also apply for orientation. For instance:

part.Orientation = Vector3.new(math.rad(60),0,0)

Correct me if I’m wrong about the radians.

Oh i didnt know that i tought only cframe was able to change orientation

CFrame has more complex stuffs and more powerful compare to Vector3.

The reason you multiply it is because you can’t add 2 CFrames together.

CFrame is a matrix made up of 12 float numbers which consist of rotation and position. Rotation is basically orientation, consisting of 3 numbers; x, y, and z. The position also consists of 3 numbers; x, y, and z.

However, CFrames also include directional vectors, vectors that have a length of 1. Those directional vectors are LookVector, UpVector and RightVector. Also note that directional vectors are unitized and that is t
he reason they have a length of 1.

LookVector is the forward part of a CFrame, RightVector being the right and UpVector being the up. However, it’s different when they are negative:

Remember that -LookVector is back, -RightVector is the left and -UpVector is down [ONLY IF IT’S NEGATIVE].

Whenever you construct a new CFrame, it will always be 0, 0, 0. However, you should provide arguments i.e x, y and z. For e.g:

(Also keep in mind that CFrames are object space while Positions are world space)

CFrame.new(0, 5, 0) --> We position the CFrame 5 studs on the y axis

CFrame.new(1, 5, 0) -- > Contructs a new CFrame 1 stud on the x, 5 on the y and 0 on the z axis

Let’s take some examples from code:

local part = Instance.new("Part") -- create the part
part.CFrame = CFrame.new(0, 5, 0)
part.Parent = workspace

In the code above, we tell the part’s CFrame to be 5 studs on the y axis.

Next example:

local part = Instance.new("Part") -- create the part
part.CFrame = part.CFrame.LookVector * 2 -- look vector is a unit
part.Parent = workspace

Now this code makes use of the directional vectors. Remember that you can set a CFrame to a vector and directional vectors are basically Vector3s. The above code set’s a part’s CFrame to it’s look vector multiplied by 2.

Vector3s are 3D, while Vectors are 2D. Z axis in Vectors don’t exist.

It won’t have the same orientation since CFrame’s have a default orientation of 0, 0, 0. It will only keep the orientation if it’s multiplied.

I was referring to code that I posted earlier:

Part.CFrame = Character.HumanoidRootPart.CFrame * CFrame.new(0, 0, 5)

In that example ‘Part’ would end up with the same orientation as ‘Character.HumanoidRootPart’, but offset by 5 studs due to the transformation.