2014-01-21 199 views
1

我有一個叫做門的模型 Inside我有一個名爲Open的BoolValue 我有一個名爲Top的模型,它有所有名爲Work Mabey Comeon和Proboblynot的門塊 而且我有Block,當被觸摸時應該使Top move高達Roblox上的lua腳本向上移動模型?

直接在門口,我有此腳本

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

如果您知道如何解決這個問題,我們將很樂意爲您解答。

回答

0

你的代碼確實需要修理。

你不應該使用永無止境的循環來讓你的東西工作(除非這是唯一的方式)。 你應該對事件採取行動。

考慮使用這樣的:

結構:

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

DoorScript:

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) 

您可能要調整速度壽。

+0

我很抱歉,我的另一個項目,忘了這一點,當我看到這些問題的答案我很高興的是,大家的幫助下,我去你的腳本,它花了幾次嘗試,但我得到它的工作。 – Crazynerd314

+0

它永遠讓我罰款它說的地方Top:TranslateBy它應該是門沒有頂部,但謝謝你的幫助,這提高了我的士氣10倍。大拇指。 – Crazynerd314

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 
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世界位置。這個缺點是它確實碰撞

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

選項B:得到同樣通過MoveTo的效果,但沒有碰撞

的一種方式可以與TranslateBy方法來完成編寫自定義函數來操縱模型的C框架

另一種選擇將完全操縱整個模型的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)) 
0

這可能很難。
你可能想看看這個免費的模型,除非上面的人得到它的工作。 我,但是,確實有一個腳本來移動一個模型:

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