So, I still have issues understanding CF:ToWorldSpace() and CF:ToObjectSpace() even after watching videos and reading threads, but this is what I understand so far.
So here’s the script, it works but I don’t understand it.
local fish = script.Parent
local t = workspace:WaitForChild("Table")
local relativeCF = t.PrimaryPart.CFrame:ToObjectSpace(fish.CFrame)
while true do
task.wait(0.1)
fish.CFrame = t.PrimaryPart.CFrame:ToWorldSpace(relativeCF)
end
Why do we need to convert the relativeCF back to world space when we can just do :ToObjectSpace()? Like to make the fish relative to the table’s CFrame?
I don’t understand the point of doing :ToWorldSpace() since it just make things relative to the origin? Or like by default objects must be set to World Space CFrame?
And when we do CF:ToObjectSpace(CF2)
CF is the CFrame that CF2 is relative to?
CF2 is the CFrame we wanna make it relative to CF?
Because the toWorldSpace is relative on another object which is the table in your example which is the point of origin for the fish.
Every frame the table CFrame changes and so the point of origin for the fish therefore the CFrame needs to be updated every frame and cannot be done once. (Welds do this automatixally behind the scene)
However the offset remains constant (relativecf) which is on top of the table in the initial frame of the script.
You only know if a CFrame is in object or world space depending on how you define it and how you process it.
For your example on it’s own you cannot really tell (0, 11, 0) is it 11 studs on the global y axis or relative to another CFrames y axis or up vector.
But the moment you do this and apply the function which converts object space CFrame into world space CFrame:
offsetCF becomes an object space CFrame which is being transformed relative to world space.
It’s as if you are telling the computer “Place the fish CFrame at the table CFrame and then add 11 studs on the fish CFrame local y axis or UpVector”. That’s how I personally interpret that line of code.
In the table example yes because you are trying to find the position of the fish relative to the table when the game has started (which is on top by a constant (0,1,0) studs and (0, 90, 0) rotation offset on the local y axis as well).
However if you already know the fish is ontop the table by (0, 1, 0) then you can directly go to world space.
No, I believe the source of the confusion here is that part CFrames are relative to world space by default so most often you are working with world space CFrames (part CFrames).
However if you look at attachment CFrames they are relative to the part the attachment is parented in.
Nvm, I mean if we want to maintain the fishes position but relative to the table, we use :ToObjectSpace()?
I tried doing this and it didn’t work, isn’t it getting the relative CF between the fish’s CFrame and the Table’s CFrame? Like treating the table as the origin and then set the fish CFrame to the relativeCF. And since the relative CF does it once, we put it in a while loop.
What I want to do is maintain the fish position and make it relative to the table using the method below. Is it not possible to do it without :ToObjectSpace()?
local fish = script.Parent
local t = workspace:WaitForChild("Table")
while true do
task.wait(0.1)
local relativeCF = t.PrimaryPart.CFrame:ToWorldSpace(fish.CFrame)
fish.CFrame = relativeCF
end
But like this one, why don’t we put it inside the while loop?