1

我有一个名为 door Inside 的模型 我有一个名为 Open 的 BoolValue 我有一个名为 Top 的模型,其中包含名为 Work Mabey Comeon 和 Proboblynot 的所有门块 我有一个 Block ,当触摸它时应该使 Top 向上移动

直接在门内我有这个脚本

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()

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

4

4 回答 4

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))
于 2014-02-19T19:58:05.450 回答
1

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

game.Workspace.Model:MoveTo(Vector3.new(0,0,0))
于 2017-11-21T18:27:26.953 回答
0

这可用于移动模型,尝试将类似的内容添加到您的代码中。它更有活力。

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
于 2014-01-30T00:54:50.200 回答
0

您的代码确实需要修复。

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

考虑使用这个:

结构:

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)

您可能需要调整速度。

于 2014-02-22T17:08:33.930 回答