Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I move a part relative to another part?

Asked by 6 years ago

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.

0
The question marks are parts of the Diamond. The first question mark is the full Diamond, the second is the top half of it, they were censored as they are not actual keys. Witchest 48 — 6y
0
Welds? User#20279 0 — 6y
0
No, CFrame, as mentioned in the post like 3 times. Witchest 48 — 6y
0
View all comments (3 more)
0
^With that, all other parts will move relative to the PrimaryPart ax_gold 360 — 6y
0
Again Witchest, do you want to recreate SetPrimaryPartCFrame with ungrouped parts? I could answer that. Its hard to answer weird thought experiments. cabbler 1942 — 6y
0
Yes, cabbler, that's exactly what I want! Witchest 48 — 6y

2 answers

Log in to vote
0
Answered by
hellmatic 1523 Moderation Voter
6 years ago
Edited 6 years ago
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)
0
Don't know if this will work, wrote it based on my knowledge. Comment if you came across any problems. hellmatic 1523 — 6y
Ad
Log in to vote
0
Answered by
cabbler 1942 Moderation Voter
6 years ago

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

Answer this question