In what cases should CFrame be used instead of Position and vice versa? Both of them seem so similar, I don’t know what the difference between them is. Can somebody please explain this to me?
Position is just that, a position with no rotation or any other info. On the other hand, CFrames contain both position AND rotation. For any situation in which rotation matters, you can use CFrame. Any situation that DOESNT need rotation, you can use position. These two are interchangeable for many situations, like if you wanted to teleport something somewhere and the rotation of the thing doesn’t matter.
One example for CFrame usage is turning some object to face another object, which wouldn’t be possible only with positions.
A CFrame has 2 Components (Position, Orientation)
A Position has 1 Component (Position)
Example:
You want to make a Part look at another Part, and then Position and Rotate it exactly like that part.
local Part0
local Part1
Part0.CFrame = CFrame.lookAt(Part0.Position, Part1.Position) – this will make Part0 look at the Part1. Basically draws a vector from first position to second position
task.wait(3)
Part0.CFrame = Part1.CFrame – If you only want it to
position to Part1 and not Position and Orientate, then replace here the CFrame to Position
I am on mobile so I mightve spelled something wrong.
As others have said, a CFrame contains positional and rotational data. To be very specific, CFrames store 4 things:
- Position in 3D space
- LookVector
- UpVector
- RightVector
Look, Up, and Right vectors have a length of 1. These just represent the direction of these faces. They are the rotational part of CFrames. You can use them to move an object forward easily:
local part = workspace.Part
part.CFrame += part.CFrame.LookVector * 5
This code will make the part move forward 5 studs, in the direction it’s looking. If you use the UpVector, it will move up 5 studs in the direction of it’s top face. They have a length of 1, so the math is very easy.
If you’re going to change the orientation of something, you should consider changing the CFrame instead. They’re much more reliable! Changing the orientation could result in unwanted behavior, especially when tweening, because angles over 180 degrees are flipped around -180, using -180 + (180 - x)
.
Thank you, all of that is very helpful! One other question regarding the CFrame and Position, I have a part which is cloned into the character model of every player who joins the game. It is welded to the HumanoidRootPart of each character model and is 1.5 studs in front of the HumanoidRootPart.
Here is the line of code which sets its position:
part.Position = character.HumanoidRootPart.Position + Vector3.new(0,0,-1.5)
Now, I also have a part in the game which teleports the player to another location when they step on it.
Here is the code for the teleport position:
hit.Parent:FindFirstChild("HumanoidRootPart").CFrame = destination.CFrame
This all works fine. However, if in this line of code hit.Parent:FindFirstChild("HumanoidRootPart").CFrame = destination.CFrame
, you change both "CFrame"s to “Position”, it will still teleport the player, but the part which is parented to the player when they join the game is no longer in the correct spot. It moves somewhere weird.
Original
After teleport (Position only, does not happen when you use CFrame)
This isn’t a problem that needs solved as I’ve already done so. This issue is fixed by using CFrame instead of Position. I’m just wondering why this issue occurs with Position and not with CFrame, it doesn’t make sense. Like many of you said, the only difference between the two is really just orientation. And I’m not changing the orientation in either script. Does anyone know why this happens?
(This is sort of an extension of my OP, I promise I will mark one of you as the solution, I just don’t want this topic to close yet)
This is just a theory but I’ve had a problem with CFrame, in order to teleport a player you need to get the Player.Character.HumanoidRootPart.CFrame from the players tab and not the workspace. Not sure if this works but hope it helps.