Alright, so this problem may very well be easy to solve, but it’s been giving me a ton of trouble the past two days. I don’t have a code sample, since I haven’t, err, actually had any success whatsoever, but I’ll try to explain what I’m hoping to do to the best of my ability.
Let’s say we have Part A and Part B. Part B is welded to Part A, and Part A is doing god-knows-what somewhere—it’s not stationary. Part B. I want to be able to manipulate Part B through the weld (by CFraming the Weld’s C0 and C1) such that it can do things like turn to face a specific part, regardless of the position & orientation of Part A.
Now, I can do some basic CFraming, but this is beyond me. I’ve scoured wiki pages and spent hours upon hours typing code I don’t understand in order to do this, with no luck. I’m all but certain that the answer to this is fairly simple, but if anyone can really ELI5 the solution to me I’d be super grateful. Thank you
I believe what you’re looking for is CFrame:Inverse().
Try referring to this code:
local tpart = workspace.TargetPart
local cfpart = workspace.CFPart
local w = Instance.new("Weld",tpart)
w.Part0 = tpart
w.Part1 = cfpart
local cf0 = tpart.CFrame
local cf1 = CFrame.new(0,10,0) * CFrame.Angles(0,math.rad(45),0)
w.C0 = cf0:Inverse()
w.C1 = cf1:Inverse()
You’d have to update this weld’s CFrame via RunService because Part1 will always update according to Part0, but if you really need to use welds for something like this, then I believe this is the most straightforward method.
Just to clarify:
C0 refers to the CFrame relative to Part0, so setting it to the inverse of Part0’s CFrame cancels the values out. C1 then refers to the CFrame relative to Part1, but since welds are weird, you need to take the inverse of that CFrame as well after applying any changes in order to convert it to world space. This essentially means that the variable cf1 in the above code will end up being Part1’s CFrame upon applying the weld.
I believe you can achieve the same effect using :ToObjectSpace() and/or :ToWorldSpace(), but using :Inverse() is the method I’m accustomed to.