I have been trying to make my own CFrame swing doors lately, and I've been running into some problems, like the door swings open, but it doesn't swing back in the right way, is there a way to calculate the correct numbers and angles to put into the CFrames? If not, can you at least answer with some tips on how to make the CFrames work right? Here's what I've got:
01 | local Door = script.Parent |
02 | local DoorFrame = Door.Parent |
03 | local CD 1 = Door.CD |
04 | --I am still not sure about the numbers in here, the dimensions of the door are (4, 6, 0.4) |
05 | function Open 1 () |
06 | if Door.InUse.Value then |
07 | Door.InUse.Value = true |
08 | for i = 1 , 18 do |
09 | Door.CFrame = Door.CFrame * CFrame.new( 0 , 0 , 0.21 ) * CFrame.fromEulerAnglesXYZ( 0 , 0.1 , 0 ) |
10 | wait( 0.01 ) |
11 | end |
12 | wait( 1.5 ) |
13 | for i = 1 , 18 do |
14 | Door.CFrame = Door.CFrame * CFrame.new( 0.002 , 0 , - 0.22 ) * CFrame.fromEulerAnglesXYZ( 0 , - 0.1 , 0 ) |
15 | wait( 0.01 ) |
Here's how you rotate a part around a point (or in your case, a door).
1 | part.CFrame = origin*rotation*(origin:inverse()*part.CFrame) |
origin
is the point (CFrame, not Vector3) you're rotating around, and rotation
is the CFrame you're rotating by.
Added to your code, it will look like this:
01 | local Door = script.Parent |
02 |
03 | local origin = CFrame.new(Door.Position - Vector 3. new(Door.Size.X/ 2 , 0 , 0 )) |
04 | --You're probably going to need to fix the origin yourself |
05 |
06 | local DoorFrame = Door.Parent |
07 | local CD 1 = Door.CD |
08 | function Open 1 () |
09 | if not Door.InUse.Value then |
10 | Door.InUse.Value = true |
11 | for i = 1 , 18 do |
12 | Door.CFrame = origin * CFrame.Angles( 0 ,math.rad( 90 / 18 ), 0 ) * (origin:inverse()*Door.CFrame) |
13 | wait( 0.01 ) |
14 | end |
15 | wait( 1.5 ) |