单击按钮ROBLOX [LUA]时克隆+拖动模型?

时间:2015-05-30 13:25:35

标签: lua roblox

所以我想这么久,我只是不知道从哪里开始。我是这门语言的新手,我一直在学习,但对我来说很难。但我已经建立了我自己的自定义角色,花了我2​​个星期。无论如何,对于我的问题。一个例子是,如果我有一个按钮,我点击它,一个模型将克隆,我可以拖动该模型,并把它放在附近的任何地方。我可以用什么方法来实现这个目标?

2 个答案:

答案 0 :(得分:1)

首先,我建议您今后提出任何问题,然后转到https://scriptinghelpers.org/

现在,关于你的问题,为了克隆模型,你应该在我的函数库中使用mouse.Target.Parent:Clone()或GetTopParent(mouse.Target)函数(你可以在这里找到它; {{3 }})

然后将模型存入工作区和MakeJoints()

下一步是移动模型,这可能很棘手,但最简单的方法是模型:鼠标上的MoveTo(mouse.Hit.p)。移动(但这有点儿小虫)

另一种移动方法是使用Handles类,但我并不熟悉它,所以你必须自己解决这个问题。

为了使第一种方法减少错误,我建议采用

的方法
model:MoveTo(mouse.Hit.p.X, mouse.Target.Position.Y + (model:GetExtentsSize().Y / 2), mouse.Hit.p.Z) 

但你必须设置鼠标才能忽略模型,我无法真正帮助它。

答案 1 :(得分:1)

一个非常好的起点是在Studio Toolbox中搜索免费模型以获取“拖拉工具”。或者'模型拖动工具'然后使用里面的脚本开始创建自己的脚本。我已经学会了通过这样做创建我自己的自定义拖动器,它比你最初想象的要容易。一旦找到一个好的拖动工具来借用代码,如果你需要增强它,你可以在Roblox Wiki中找到dragger api来帮助你根据你的特定需求进一步定制它。

http://wiki.roblox.com/index.php?title=API:Class/Dragger

编辑:所以这是我搜索时显示的第一个拖动脚本。它将拖动模型和零件,但您必须使用拖动器API编辑它以满足您的要求。在player.BackPack中创建一个工具,然后在工具中创建一个LocalScript,然后将下面的代码复制并粘贴到LocalScript中,这样就可以开始了。

        local Tool = script.Parent

    enabled = true
    local origTexture =     Tool.TextureId
    game:GetService("ContentProvider"):Preload("rbxasset://icons/freemove_sel.png")

    local selectionBox
    local currentSelection
    local currentSelectionColors = {}
    local selectionLasso
    local inGui = false
    local inPalette = false
    local lockTime = 0

    function canSelectObject(part)
        return part and not (part.Locked) and (part.Position - script.Parent.Parent.Head.Position).Magnitude < 60
    end

    function findModel(part)
        while part ~= nil do
            if part.className == "Model" then
                return part
            end
            part = part.Parent
        end

        return nil
    end


    function startDrag(mousePart, hitPoint, collection)
        dragger = Instance.new("Dragger")
        pcall(function() dragger:MouseDown(mousePart, hitPoint, collection) end)
    end

    function collectBaseParts(object, collection)
        if object:IsA("BasePart") then
            collection[#collection+1] = object
        end
        for index,child in pairs(object:GetChildren()) do
            collectBaseParts(child, collection)
        end
    end

    function onMouseDown(mouse) 
        mouse.Icon ="rbxasset://textures\\GrabRotateCursor.png"
        local part = mouse.Target
        if canSelectObject(part) then
            local hitPoint = mouse.Hit:toObjectSpace(part.CFrame).p
            if trySelection(part) then
                local instances = {}
                collectBaseParts(currentSelection, instances)
                startDrag(part, hitPoint, instances)
                return
            end
        end

        --Clear the selection if we weren't able to lock succesfullu
        onMouseUp(mouse)
    end



    function onMouseUp(mouse)
        mouse.Icon ="rbxasset://textures\\GrabCursor.png"
        if dragger ~= nil then
            pcall(function() dragger:MouseUp() end)
            dragger = nil
        end
    end

    function trySelection(part)
        if canSelectObject(part) then
            selectionLasso.Part = part
            local model = findModel(part)
            if model then       
                return setSelection(model)
            else
                return setSelection(part)
            end
        else
            clearSelection()
            return false
        end
    end

    function onKeyDown(key)
        if dragger ~= nil then
            if key == 'R' or key == 'r'  then
                dragger:AxisRotate(Enum.Axis.Y)
            elseif key == 'T' or key == 't' then
                dragger:AxisRotate(Enum.Axis.Z)
            end
        end
    end
    local alreadyMoving
    function onMouseMove(mouse)
        if alreadyMoving then
            return
        end

        alreadyMoving = true
        if dragger ~= nil then
            --Maintain the lock
            if time() - lockTime > 3 then
                Instance.Lock(currentSelection)
                lockTime = time()
            end

            --Then drag
            pcall(function() dragger:MouseMove(mouse.UnitRay) end)
        else
            trySelection(mouse.Target)
        end
        alreadyMoving = false
    end


    function saveSelectionColor(instance)
        if instance:IsA("BasePart") then
            currentSelectionColors[instance] = instance.BrickColor
            if instance.BrickColor == BrickColor.Blue() then
                instance.BrickColor = BrickColor.new("Deep blue")
            else
                instance.BrickColor = BrickColor.Blue()
            end
        end

        local children = instance:GetChildren() 
        if children then
            for pos, child in pairs(children) do
                saveSelectionColor(child)
            end
        end
    end

    function setSelection(partOrModel)
        if partOrModel ~= currentSelection then
            clearSelection()
            if Instance.Lock(partOrModel) then
                lockTime = time()
                currentSelection = partOrModel
                saveSelectionColor(currentSelection)
                selectionBox.Adornee = currentSelection
                return true
            end
        else
            if currentSelection ~= nil then
                if time() - lockTime > 2 then
                    --Maintain the lock
                    if not(Instance.Lock(currentSelection)) then
                        --we lost the lock
                        clearSelection()
                        return false
                    else
                        lockTime = time()
                        return true
                    end
                else
                    return true
                end
            end
        end

        return false
    end

    function clearSelection()
        if currentSelection ~= nil then
            for part, color in pairs(currentSelectionColors) do
                part.BrickColor = color
            end
            selectionBox.Adornee = nil
            Instance.Unlock(currentSelection)
        end
        currentSelectionColors = {}
        currentSelection = nil
        selectionLasso.Part = nil
        selectionBox.Adornee = nil
    end

    function onEquippedLocal(mouse)
        Tool.TextureId = "rbxasset://icons/freemove_sel.png"

        local character = script.Parent.Parent
        local player = game.Players:GetPlayerFromCharacter(character)


        inGui = false
        inPalette = false

        mouse.Icon ="rbxasset://textures\\GrabCursor.png"
        mouse.Button1Down:connect(function() onMouseDown(mouse) end)
        mouse.Button1Up:connect(function() onMouseUp(mouse) end)
        mouse.Move:connect(function() onMouseMove(mouse) end)
        mouse.KeyDown:connect(function(string) onKeyDown(string) end)

        selectionBox = Instance.new("SelectionBox")
        selectionBox.Name = "Model Delete Selection"
        selectionBox.Color = BrickColor.Blue()
        selectionBox.Adornee = nil
        selectionBox.Parent = player.PlayerGui

        selectionLasso = Instance.new("SelectionPartLasso")
        selectionLasso.Name = "Model Drag Lasso"
        selectionLasso.Humanoid = character.Humanoid
        selectionLasso.archivable = false
        selectionLasso.Visible = true
        selectionLasso.Parent = game.workspace
        selectionLasso.Color = BrickColor.Blue()

        alreadyMoving = false

    end

    function onUnequippedLocal()
        Tool.TextureId = origTexture
        clearSelection()
        selectionBox:Remove()
        selectionLasso:Remove()
    end


    Tool.Equipped:connect(onEquippedLocal)
    Tool.Unequipped:connect(onUnequippedLocal)