1

ドアというモデルがあります 内部に Open という名前の BoolValue があります Work Mabey Comeon と Proboblynot という名前のすべてのドア ブロックを持つ Top というモデルがあります そして、触れると Top が上に移動することになっている Block があります

ドアのすぐ内側にこのスクリプトがあります

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 Model オブジェクトには、モデルを変換するために Vector3 引数を取る「TranslateBy」と呼ばれる気の利いたメソッドが含まれています。

使用MODEL:TranslateBy(Vector3)は、衝突を無視するため、CFrame を介してモデルを移動するのと似ています。

もう 1 つの方法はMODEL:MoveTo(Vector3)、モデル全体を特定の Vector3 ワールド位置に移動することです。これの欠点は、衝突することです

同じ MoveTo 効果を衝突なしで取得する 1 つの方法は、TranslateBy メソッドを使用して実行できます。

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

オプション B: モデルの CFrame を操作するカスタム関数を作成する

もう 1 つの方法は、モデル全体の CFrame を完全に操作することです。これを行うには、モデル全体を「原点」に対して相対的に移動させる巧妙な関数を作成できます。これは、3 次元を除いて、点と原点を指定してグリッド上で形状を移動するのと似ています。ただし、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

あなたのコードは確かに修正が必要です。

終わりのないループを使用して作業を行うべきではありません (それが唯一の方法でない限り)。イベントに基づいてアクションを実行する必要があります。

これを使用することを検討してください:

構造:

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 に答える
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 に答える