【问题标题】:A lua script on roblox that moves a model upwards?roblox 上的 lua 脚本可以向上移动模型?
【发布时间】:2014-02-11 17:52:23
【问题描述】:

我有一个模型叫做门 在里面我有一个名为 Open 的 BoolValue 我有一个名为 Top 的模型,其中包含名为 Work Mabey Comeon 和 Proboblynot 的所有门块 而且我有块,当被触摸时应该使顶部向上移动

直接在门内我有这个脚本

door = script.Parent
open = door.Open
Top = door.Top

opener = 18
speed = 100
steps = speed

startl = Top.CFrame

function MoveDoorToCFrame(cfrm,dr)
    dr.Work.CFrame = cfrm
dr.Mabey.CFrame = dr.Work.CFrame * CFrame.new(0,-7.2,0)
dr.Comeon.CFrame = dr.Work.CFrame * CFrame.new(0,10.8,0)
dr.Problynot.CFrame = dr.Work.CFrame * CFrame.new(0,10.8,0)
end

function Update()
if speed/steps < 0.5 then
    calc = 1-math.cos(math.rad((-90/speed)*steps*2))
else
    calc = 1+math.sin(math.rad((90/speed)*((speed/2)-steps)*2))
end
MoveDoorToCFrame(startl * CFrame.new(0,(calc/2)*opener,0),Top)
end

Update()
while true do
wait()
if not open.Value and steps < speed then
    steps = steps + 1
    Update()
elseif open.Value and steps > 0 then
    steps = steps - 1
    Update()
end
end 

在我应该在触摸时激活的按钮内

script.Parent.Touched:connect(function()
script.Parent.Parent.Open.Value = not script.Parent.Parent.Open.Value
end)

script.Parent.Parent.Open.Changed:connect(Update)
Update()

如果您知道如何解决此问题,我们将不胜感激。

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    2015 年 11 月更新:

    使用 PrimaryPart

    自写这篇文章以来,ROBLOX 在 API 方面发生了很大变化。要像请求的那样移动模型,您应该将模型的 PrimaryPart 属性设置为模型内部的中心部分。这将作为模型运动的origin

    然后您可以使用model:SetPrimaryPartCFrame(cframe) 来设置模型的CFrame。您也可以使用model:GetPrimaryPartCFrame() 检索此属性,尽管我认为这只是model.PrimaryPart.CFrame 的一种快捷方式。

    在代码中,它看起来像这样:

    -- Set PrimaryPart:
    MODEL.PrimaryPart = MODEL.SomeCentralPart
    ...
    
    -- CFrame movement:
    local movement = CFrame.new(0, 10, 0)
    
    -- Move the model:
    MODEL:SetPrimaryPartCFrame(MODEL:GetPrimaryPartCFrame() * movement)
    

    选项 A:使用模型的方法

    我认为你让这件事变得比需要的困难得多。每当您遇到此类问题时,请务必检查当前提供的 API。 ROBLOX 模型对象包含一个名为“TranslateBy”的漂亮方法,它采用 Vector3 参数来翻译模型。

    使用MODEL:TranslateBy(Vector3) 类似于通过 CFrame 移动模型,因为它会忽略碰撞。

    另一种选择是MODEL:MoveTo(Vector3),它将整个模型移动到给定的 Vector3 世界位置。这样做的缺点是它确实会发生碰撞。

    使用 TranslateBy 方法可以获得相同的 MoveTo 效果但不发生冲突的一种方法:

    MODEL:TranslateBy(Vector3Position - MODEL:GetModelCFrame().p)
    

    选项 B:编写自定义函数来操作模型的 CFrame

    另一种选择是完全操纵整个模型的 CFrame。为此,您可以编写一个巧妙的函数,将整个模型相对于“原点”移动。这类似于在给定点和原点的情况下在网格上移动形状,除了在三个维度上。使用 ROBLOX 的内置函数,这会容易得多。

    这样做的一个好方法是编写一个函数,让您将 CFrame 值实际分配给整个模型。另一种方法是允许通过 CFrame 进行翻译。

    这是一个例子:

    function ModelCFrameAPI(model)
    
        local parts = {} -- Hold all BasePart objects
        local cf = {} -- API for CFrame manipulation
    
        do
            -- Recurse to get all parts:
            local function Scan(parent)
                for k,v in pairs(parent:GetChildren()) do
                    if (v:IsA("BasePart")) then
                        table.insert(parts, v)
                    end
                    Scan(v)
                end
            end
            Scan(model)
        end
    
        -- Set the model's CFrame
            -- NOTE: 'GetModelCFrame()' will return the model's CFrame
            -- based on the given PrimaryPart. If no PrimaryPart is provided
            -- (which by default is true), ROBLOX will try to determine
            -- the center CFrame of the model and return that.
        function cf:SetCFrame(cf)
            local originInverse = model:GetModelCFrame():inverse()
            for _,v in pairs(parts) do
                v.CFrame = (cf * (originInverse * v.CFrame))
            end
        end
    
        -- Translate the model's CFrame
        function cf:TranslateCFrame(deltaCf)
            local cf = (model:GetModelCFrame() * deltaCf)
            self:SetCFrame(cf)
        end
    
        return cf
    end
    
    
    -- Usage:
    local myModel = game.Workspace.SOME_MODEL
    local myModelCF = ModelCFrameAPI(myModel)
    
    -- Move to 10,10,10 and rotate Y-axis by 180 degrees:
    myModelCF:SetCFrame(CFrame.new(10, 10, 10) * CFrame.Angles(0, math.pi, 0))
    
    -- Translate by 30,0,-10 and rotate Y-axis by 90 degrees
    myModelCF:TranslateCFrame(CFrame.new(30, 0, -10) * CFrame.Angles(0, math.pi/2, 0))
    

    【讨论】:

      【解决方案2】:

      这可能很难。
      除非上面的人让它工作,否则你可能想为这个寻找免费模型。 但是,我确实有一个移动模型的脚本:

      game.Workspace.Model:MoveTo(Vector3.new(0,0,0))
      

      【讨论】:

        【解决方案3】:

        您的代码确实需要修复。

        您不应该使用永无止境的循环来使您的东西正常工作(除非这是唯一的方法)。 您应该将操作基于事件。

        考虑使用这个:

        结构:

        Door [Model]
            DoorScript [Script]
            Button [Part]
            DoorOpen [BoolValue]
            Top [Model]
                Mabey [Part]
                Comeon [Part]
                Problynot [Part]
        

        门脚本:

        local Model = script.Parent
        local Door = Model.Top
        local Button = Model.Button
        local DoorOpen = Model.DoorOpen
        
        local Offset = 0
        local ToOffset = 100
        local Direction = 1
        
        local StepLength = 0.1
        
        local Moving = false
        
        function StartMoving()
            if Moving then return end
            Moving = true
            while (DoorOpen.Value and Offset ~= ToOffset) or (not DoorOpen.Value and Offset ~= 0) do
                local Change = Offset
                Offset = math.max(0,math.min(ToOffset,Offset + StepLength * (DoorOpen.Value and 1 or -1)))
                Change = Offset - Change
                Top:TranslateBy(Vector3.new(0,Change,0))
                wait()
            end
            Moving = false
        end
        StartMoving()
        DoorOpen.Changed:connect(StartMoving)
        
        local Debounce = false
        Button.Touched:connect(function()
            if Debounce then return end
            Debounce = true
            DoorOpen.Value = not DoorOpen.Value
            wait(4)
            Debounce = false
        end)
        

        您可能需要调整速度。

        【讨论】:

        • 我很抱歉我有另一个项目并忘记了这一点,当我看到这些答案时,我很高兴你们都提供了帮助,我使用了你的脚本并尝试了几次但我得到了它工作。
        • 我花了很长时间才在上面说 Top:TranslateBy 它应该是 Door 而不是 top 但感谢您的帮助,这使我的士气提高了 10 倍。竖起大拇指。
        【解决方案4】:

        这可以用来移动模型,试着在你的代码中添加这样的东西。它更有活力。

        a = Workspace.Model
        
        for i=0.1,40 do
            for i,v in pairs(a:getChildren()) do
                if v:IsA("Part") then
                    v.CFrame = CFrame.new(v.CFrame + Vector3.new(0,0.1,0))
                else print("Not a part")
                end
            end
        end
        

        【讨论】:

          猜你喜欢
          • 2023-02-02
          • 2020-07-02
          • 2022-10-20
          • 2016-03-31
          • 1970-01-01
          • 2022-07-12
          • 2022-09-25
          • 2020-06-23
          • 2018-12-28
          相关资源
          最近更新 更多