I’m trying to understand ToObjectSpace. I already know it inverses the CFrame effected by it.
Issue?
I understand how it works. CFrame:Inverse() * cf, but I don’t understand how videos like this use it to to perfectly align a CFrame with another CFrame.
What have I tried?
In studio I tried replicating this process but it ended up, as should be, inversing the CFrame and multiplying it by the CFrame cast in the ToObjectSpace. I don’t know how I would implement this in a game and I don’t know how this relates to “local space”.
At around 18:23 in the video you can see ToObjectSpace being used to lerp an attachment’s CFrame to a part’s CFrame.
This should provide you a hands-on understanding of how local space works and how ToObjectSpace can be used to maintain relative positions
-- Assume cfA and cfB are two CFrames in world space.
local cfA = workspace.PartA.CFrame
local cfB = workspace.PartB.CFrame
-- To find CFrame B's position and orientation relative to CFrame A, you would use ToObjectSpace:
local relativeCFrame = cfA:ToObjectSpace(cfB)
-- Let's say you want PartB to always be a certain distance and orientation from PartA,
-- even as PartA moves around.
local relativeCFrame = cfA:ToObjectSpace(cfB)
-- Then, in a heartbeat or render step loop, you might have:
workspace.PartB.CFrame = workspace.PartA.CFrame * relativeCFrame
local center = workspace.Center
local p1 = workspace.Part1
local p2 = workspace.Part2
local relative = p1.CFrame:ToObjectSpace(p2.CFrame)
center.CFrame = relative
The relative variable makes p2 relative to p1. When I tested this script I figured out that the center part was -24 studs under the origin, just like p2. I thought that the center’s CFrame would be equivalent to p2’s CFrame and not -24 studs under the origin.