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

Using CFrame to rotate doors?

Asked by 4 years ago

I am building a drawbridge, and i watched a youtube video. I am trying to make it so the drawbridge goes down to the ground then pulls back up again. The second part is not working, and instead just resets from a 0 0 0 orientation and then repeats the going down part. Here is my script, anything helps.

local cd = script.Parent.ClickDetector
local ori = script.Parent.Lever.Orientation.X
local click = script.Parent.Sound
local down = false
local lever = script.Parent.Lever
local down = false
local Bridge = workspace.Drawbridge.SoundTwo
local door = workspace:WaitForChild("Drawbridge")
local hinge = workspace:WaitForChild("DrawbridgeHinge")
local hinget = workspace:WaitForChild("DrawbridgeHinge2")
local HingePos = hinge.Position
local HingetPos = hinget.Position
local thump = workspace.Drawbridge.Thump

cd.MouseClick:Connect(function()
    if down == false then
        wait(0.2)
        click:Play()
        lever.Orientation = Vector3.new(30, 180, 180)
        Bridge:Play()
        for i = 1, 90 do
            door.CFrame = CFrame.new(HingePos) * CFrame.Angles(math.rad(i),0,0) * CFrame.new(0,19.87,0)
            wait()
        end
        Bridge:Stop()
        thump:Play()
        wait(2)
        thump:Stop()
        down = true
    else
        wait(0.2)
        click:Play()
        lever.Orientation = Vector3.new(0,180,180)
        Bridge:Play()
        for i = 1, 90 do
            door.CFrame = CFrame.new(HingetPos) * CFrame.Angles(math.rad(-i),0,0) * CFrame.new(0,19.87,0)
            wait()
        end
        Bridge:Stop()
        thump:Play()
        wait(2)
        thump:Stop()
        down = false
    end
end)
0
You could also animate the doors if you want like how you are animating the bridge. DevMaster333 215 — 4y

1 answer

Log in to vote
0
Answered by
Benbebop 1049 Moderation Voter
4 years ago
Edited 4 years ago

I'm not sure what or why a tutorial is telling you to manually interpolate things, sure its possible but completely unnecessary. Use TweenService. It removes the need to interpolate yourself and will fix this problem.

I will assume you know how to set up Services so I will skim over that part.

TweenService works by creating a tween instance that contains all your movement data. It has 3 properties: the object you want to tween (this can be a 3d object, GUI or just any instance with numerical properties), a TweenInfo instance (this contains the information about your tween), and finally the parameters of the object you want to manipulate. TweenInfo has a number of properties: the duration of the tween (in seconds), the interpolation method (Ex. Linear, Quadratic, Exponential, Back, etc.), the directions it should apply the interpolation (should it interpolate on the start to end, just on the start, etc.) and a couple more that you wont really need to worry about.

With that out of the way lets create a tween. For this I will be tweening the instance Part for one second with the interpolation method set to Circular, interpolating only on the way in, to a new position of 1, 0, 0.

Ex.

local Tween = TweenService:Create(Part, TweenInfo.new(1, Enum.EasingStyle.Circular, Enum.EasingDirection.In), {position = Vector3.new(1, 0, 0)})

Addendum: Enums are just dictionaries for numbers, Enum.EasingStyle.Circular is actually just the number 8 but it makes it easier to remember when its written like this.

After the tween is set up you need to actually run it. This step is pretty easy.

Ex.

local Tween = TweenService:Create(Part, TweenInfo.new(1, Enum.EasingStyle.Circular, Enum.EasingDirection.In), {position = Vector3.new(1, 0, 0)})

Tween:Play()

Make sure to set the time and interpolation method to whatever you want.

See if that fixes the issue.

Ad

Answer this question