Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a sliding door using Vector3?

Asked by 10 years ago

So I know basic scripting. The thing is, I have 2 scripts. One of the opens and doesn't close, and the other doesn't move. How would I edit the following so that it closes after 2 seconds? Failing this, can you suggest a new script that works in this manner.

local Part = script.Parent
local newPos = Part.Position + Vector3.new(7,0,0)
local Time = 5
local Increment = 0.1
local Debounce = false

local Diff = newPos - Part.Position
local Mag = Diff.magnitude
local Direction = CFrame.new(Part.Position, newPos).lookVector

function MovePart() 
    if Debounce then return end
    Debounce = true
    for n = 0, Mag, Increment do
        Part.CFrame = Part.CFrame + (Direction * Increment)
        wait( (Time/Mag) * Increment )
    end
    Debounce = false
end

script.Parent.Touched:connect(MovePart)

Currently, when you first touch it, it moves to where I want it to. I don't know how to make it go back using the same form of algorithm

Thanks FaultyMatrix

1 answer

Log in to vote
0
Answered by 10 years ago

This script is extra useful because the door will open in the direction it is facing. That means you don't have to configure the Vector3's every time you make a new door. You simply have to rotate it the right way:

local p = script.Parent
local d = false
p.Touched:connect(function()
    if d then
        return
    end
    d = true
    for i=1, 100 do  -- 100 = Distance
        p.CFrame = p.CFrame + p.CFrame.lookVector * .1 -- .1 = Speed
        wait()
    end
    wait(1)
    for i=1, 100 do  -- 100 = Distance
        p.CFrame = p.CFrame + p.CFrame.lookVector * -.1  -- Negative here to go back
        wait()
    end
    d = false
end)
0
This works, but instead of going to the left into the wall, if comes forward towards myself, I have tried rotating but to no prevail. Any ideas? FaultyMatrix 30 — 10y
0
It gets confusing rotating the part, the resizing everything (I've been through it too). The best thing is to completely shrink the door to size 1,1,1, and then rotate it until it's right. Then expand it back to normal. DiamondBladee 135 — 10y
0
Thanks, I got it right as soon as you posted that comment. Thank you so much for your help. :) FaultyMatrix 30 — 10y
0
No problem! I would highly appreciate an upvote on my answer and a click of the "Accept Answer" button c: DiamondBladee 135 — 10y
Ad

Answer this question