I searched up ToWorldSpace and read every post regarding the method. I read the Understanding CFrames roblox tutorial. However, I am having trouble understanding its (and other devforum posts’) explanation of ToWorldSpace.
This is its explanation:
The CFrame:ToWorldSpace() function transforms an object’s CFrame — respecting its own local orientation — to a new world orientation. This makes it ideal for offsetting a part relative to itself or another object, regardless of how it’s currently positioned/rotated.
This is the code following that:
local redBlock = game.Workspace.RedBlock
local blueCube = game.Workspace.BlueCube
local offsetCFrame = CFrame.new(0, 2, 0)
redBlock.CFrame = blueCube.CFrame:ToWorldSpace(offsetCFrame)
Here are the parts I don’t understand:
a new world orientation
What does this mean? I’m very confused.
This makes it ideal for offsetting a part relative to itself or another object
What does it mean for a part to be relative to something? (This is probably why I don’t understand ToWorldSpace)
regardless of how it’s currently positioned/rotated.
This is confusing for me because in the video, whenever the red brick transitions to a new CFrame, it goes on top of the blue part even though position doesn’t matter (?)
I personally don’t understand it that well either, but here’s what I got from the wiki.
I think offsetting it relative to another part, basically means instead of positioning using the origin position vector3.new(0,0,0) it uses the given objects CFrame as a world space, for example, Imagine there’s a part called “part1”, and it is positioned vector3.new(0,10,0) This is based of ROBLOX's origin point (0,0,0) then you use toWorldSpace to position another part called “part2” relative to part1
local part1 = part1;
local part2 = part2;
part2.CFrame = p1.CFrame:toWorldSpace(CFrame.new(0, 10, 0));
That will position part210 studs up on the Yaxis relative to part1’s position. Which should be equivalent to part2.CFrame = vector3.new(0, 20, 0) or multiplying both CFrames together I think. but basically all it does is emulates one position as a world space position. (instead of using 0,0,0 it uses the given position, which is 0,10,0 in this case)
Sorry if you still don’t understand, I really can’t explain anything properly.
local a = CFrame.new(1, 2, 1)
local b = CFrame.new(0,1,3)
local product = a:ToWorldSpace(b) -- this is another way of writing (a * b)
-- product equals CFrame.new(1, 3, 4), since multiplying cframes adds their positions.
lets say you want a part to always be floating to the right of a player’s head
part.Position = head.Position + Vector3.new(10, 0, 0) -- ten studs to the right of their head
the problem with this is what happens if the player turns right, towards the part? the part would stay where it was while the player rotated, making it now be in front of the player’s face. To solve this, you have to use cframe:
part.CFrame = head.CFrame * CFrame.new(10, 0, 0) -- same as doing head.CFrame:ToWorldSpace(CFrame.new(10, 0, 0))
this way, no matter how the player is rotated, it will always stay on the right side of them.