I'm trying to create a model-moving tool that cframes each part separately, but I can't figure out how I move a model in relativity to the primary part.
For example, these are too parts (One above and one below)...
?
but, I move the bottom half downwards...
?
v
I am trying to figure out what I need to do to move the top half to the bottom half to complete the diamond by only using their past positions as a guide.
So far, I've only figured out that I need to use:
Part1.CFrame * Part2.CFrame
But, I can't wrap my head around the math needed. If anyone know how to do this or has any advice, let me know.
repeat wait() until game.Players.LocalPlayer repeat wait() until game.Players.LocalPlayer.Character:IsDescendantOf(workspace) ---------------------------------------------------------------------------------------------------------------- --CONSTANTS; local Player = game.Players:WaitForChild(game.Players.LocalPlayer.Name) local Mouse = Player:GetMouse() local MouseHeld = false local CurrentModel = nil --CLICK FUNCTIONS; function CF_ONCLICK() if not MouseHeld and Mouse.Target then MouseHeld = true if Mouse.Target:IsA('BasePart') then CF_FINDMODEL(Mouse.Target) repeat wait() until CurrentModel ~= nil CF_DRAGMODEL(CurrentModel) end end end function CF_ONRELEASE() if Mouse.Held then Mouse.Held = false CurrentModel = nil end end function CF_FINDMODEL(part) if part then if part.Parent:IsA('Model') then -- assuming that 'part' is only in one model CurrentModel = part.Parent end end end function CF_DRAGMODEL(model) while MouseHeld do if model:IsA('Model') then model:MoveTo(Mouse.Hit.p) end wait() end end Mouse.Button1Down:connect(CF_ONCLICK) Mouse.Button1Up:connect(CF_ONRELEASE)
Anyone else should use model.SetPrimaryPartCFrame
. but apparently you do want to 'recreate' it
You just need to maintain all original offsets from the PrimaryPart. Best way to get offsets is toObjectSpace; originCF:toObjectSpace(targetCF)
. All math is behind the scenes.
With two parts:
local goal = CFrame.new() --change part1.CFrame = goal * part0.CFrame:toObjectSpace(part1.CFrame) part0.CFrame = goal --pseudo primary part
It's possible to move any number of parts by repeating the part1 line except with part2 etc. So, obviously put that in a part-loop, wrap with a function, and you have a generic model-mover!
--take array of parts, the primary part, and a goal cframe local function SetPrimaryPartCFrame(parts,ppart,cf) local ppcf = ppart.CFrame for i=1,#parts do local v = parts[i] v.CFrame = cf * ppcf:toObjectSpace(v.CFrame) end ppart.CFrame=cf end
hope you understand better