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

Keeping a part centered in another part while moving that part with mouse?

Asked by 4 years ago

Yeah that title is kinda confusing but its what i'm doing, basically if ya want to watch this video: https://www.youtube.com/watch?v=CsZsaR_gXJw then youll see what im doing, im trying to move a part on rotated parts like i did on the first part, or something. Also when you press r it changed the cut direction

I had to remove my cutting function or else this would be too long to post

Heres my code:

--Define Variables
local CAS = game:GetService("ContextActionService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Tool = script.Parent
local mtarget
local omtarget
local down
local newCut
local Side1,Side2,disx1,disx2,pos1,pos2,disz1,disz2
local targ,CH,Part1,Part2
local siX, siZ
local rot = "X"
local waitimer = 0
local ptarg
local equipped

function Rotation()
    if rot == "X" and waitimer == 0 then
        rot = "Z"
        waitimer = 10
    elseif rot == "Z" and waitimer == 0 then
        rot = "X"
        waitimer = 10
    end
    while waitimer ~= 0 do
        waitimer = waitimer - 1
        wait()
    end
end

function PreCut()
    mtarget = mouse.Target
    if ptarg ~= nil then
        if mtarget ~= ptarg then
            if ptarg:FindFirstChild("PreCut") then
                ptarg.PreCut:Destroy()
            end
        end
    end
    while mtarget:FindFirstChild("CanCut") and equipped do
        if mtarget:FindFirstChild("CutPart") == nil then
            if mtarget.CanCut.Value == true then
                if mtarget:FindFirstChild("PreCut") then
                    local sized = mtarget.Size
                    local pcut = mtarget.PreCut
                    if rot == "X" and pcut.Size ~= Vector3.new(sized.X+0.1,sized.Y+0.1,0.1) then
                        if mtarget.Size.Y > mtarget.Size.X and mtarget.Size.Y > mtarget.Size.Z then
                            pcut.Position = Vector3.new(ptarg.Position.X,mouse.Hit.p.Y,ptarg.Position.Z)--Change the position
                        elseif mtarget.Orientation.Z < 0 and mtarget.Orientation.Z < 180 then
                            pcut.Position = Vector3.new(ptarg.Position.X,mouse.Hit.p.Y,ptarg.Position.Z)--Change the position
                        else
                            pcut.Position = Vector3.new(mouse.Hit.p.X,ptarg.Position.Y,ptarg.Position.Z)--Change the position
                        end
                    elseif rot == "Z" and pcut.Size ~= Vector3.new(0.1,sized.Y+0.1,sized.Z+0.1) then
                        pcut.Position = Vector3.new(ptarg.Position.X,ptarg.Position.Y,mouse.Hit.p.Z)--Change the position
                    else
                        pcut:Destroy()
                    end
                else
                    local sized = mtarget.Size
                    local newCut = Instance.new("Part", mtarget) --Create Cut Part
                    local IAD = workspace:GetChildren()
                    for i = 1, #IAD do
                        local DIA = IAD[i]
                        if DIA:FindFirstChild("PreCut") then
                            DIA.PreCut:Destroy()
                        end
                    end
                    ptarg = newCut.Parent
                    newCut.BrickColor = BrickColor.new("Pastel orange") --Change Color of part
                    newCut.Anchored = true --Anchor the part
                    if rot == "X" then
                        newCut.Size = Vector3.new(0.1,sized.Y+0.1,sized.Z+0.1) --Change the size
                        if mtarget.Size.Y > mtarget.Size.X and mtarget.Size.Y > mtarget.Size.Z then
                            newCut.Position = Vector3.new(ptarg.Position.X,mouse.Hit.p.Y,ptarg.Position.Z)--Change the position
                        elseif mtarget.Orientation.Z < 0 and mtarget.Orientation.Z < 180 then
                            newCut.Position = Vector3.new(ptarg.Position.X,mouse.Hit.p.Y,ptarg.Position.Z)--Change the position
                        else
                            newCut.Position = Vector3.new(mouse.Hit.p.X,ptarg.Position.Y,ptarg.Position.Z)--Change the position
                            --print(rot)
                        end
                    elseif rot == "Z" then
                        newCut.Size = Vector3.new(sized.X+0.1,sized.Y+0.1,0.1) --Change the size
                        newCut.Position = Vector3.new(ptarg.Position.X,ptarg.Position.Y,mouse.Hit.p.Z)--Change the position
                        --print(rot)
                    end
                    newCut.Orientation = ptarg.Orientation
                    newCut.Material = "Neon" --Change the material
                    newCut.Name = "PreCut" --Name the part
                    newCut.CanCollide = false
                    mouse.TargetFilter = newCut
                end
            end
        elseif mtarget:FindFirstChild("CutPart") then
            if ptarg:FindFirstChild("PreCut") then
                ptarg.PreCut:Destroy()
            end
        end
        wait()
    end 
end

--Cutting Function, really long thing idk

--Bind actions when equipping tool
function Equip()
    CAS:BindAction("Cutting", Cut, false, Enum.UserInputType.MouseButton1)
    CAS:BindAction("Looking", PreCut, false, Enum.UserInputType.MouseMovement)
    CAS:BindAction("Rotate", Rotation, false, Enum.KeyCode.R)
    equipped = true
end

--Unbind actions when unequipping tool
function Unequip()
    CAS:UnbindAction("Cutting")
    CAS:UnbindAction("Looking")
    CAS:UnbindAction("Rotate")
    equipped = false
    local children = workspace:GetChildren()
    for i = 1, #children do
        local CHH =  children[i]
        if CHH:FindFirstChild("PreCut") then
            CHH.PreCut:Destroy()
        end
    end
end

--Check is tool is equipped or unequipped
Tool.Equipped:Connect(Equip)
Tool.Unequipped:Connect(Unequip)

Answer this question