2

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

4

2 回答 2

1

首先,我建议对于未来的任何问题,您可以前往https://scriptinghelpers.org/

现在,关于你的问题,为了克隆模型,你应该在我的函数库中使用 mouse.Target.Parent:Clone() 或 GetTopParent(mouse.Target) 函数(你可以在这里找到;http://www. roblox.com/item.aspx?id=244244638

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

下一步是移动模型,这可能很棘手,但最简单的方法是 model:MoveTo(mouse.Hit.p) on mouse.Moved (但这有点麻烦)

另一种移动方法是使用 Handles 类,但我对它不是很熟悉,所以你必须自己弄清楚。

为了使第一种方法不那么容易出错,我建议一些类似的东西

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

但是您必须设置鼠标以忽略模型,我对此无能为力。

于 2015-05-31T02:12:50.717 回答
1

一个非常好的起点是在 Studio Toolbox 中的免费模型中搜索“拖动工具”或“模型拖动工具”,然后使用里面的脚本开始创建您自己的。通过这样做,我学会了创建自己的自定义拖动器,这比您最初想象的要容易得多。一旦你找到了一个好的拖拽工具来借用代码,如果你需要增强它,你可以在 Roblox Wiki 中找到拖拽 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)
于 2016-03-02T18:59:32.477 回答