I want to create a door that rotates around a hinge using tweenService.
I set the ClosedPos as the primary part of the model. The actual door is just a single part.
The ClosedPos
The OpenPos is the same thing but just oriented differently.
And still the door opens like this:
This is my code:
local TweenService = game:GetService("TweenService")
local Model = script.Parent.Parent
local OpenPos = Model.OpenPos
local ClosePos = Model.ClosePos
local Door = Model.Door
local DoorName = Model.DoorName.Value
local TriggerBrick = script.Parent
local IsMoving = false
local ForceOpen = false
local ForceClose = false
local TweenSetting = TweenInfo.new(
5,
Enum.EasingStyle.Sine,
Enum.EasingDirection.InOut,
0,
false,
0
)
local ClosePosCF = {CFrame = CFrame.new(ClosePos.CFrame.Position) * CFrame.Angles(math.rad(ClosePos.Orientation.X), math.rad(ClosePos.Orientation.Y), math.rad(ClosePos.Orientation.Z))}
local OpenPosCF = {CFrame = CFrame.new(OpenPos.CFrame.Position) * CFrame.Angles(math.rad(OpenPos.Orientation.X), math.rad(OpenPos.Orientation.Y), math.rad(OpenPos.Orientation.Z))}
local TweenOpenPos = TweenService:Create(Door, TweenSetting, OpenPosCF)
local TweenClosePos = TweenService:Create(Door, TweenSetting, ClosePosCF)
function openDoor()
if ForceClose ~= true then
IsMoving = true
TweenOpenPos:Play()
wait(6)
if ForceOpen ~= true then
TweenClosePos:Play()
wait(0.1)
IsMoving = false
else
IsMoving = false
end
end
end
TriggerBrick.Touched:Connect(function(part)
local hum = part.Parent:FindFirstChild("Humanoid")
if hum then
openDoor()
end
end)
function closeDoor()
IsMoving = true
if not ForceOpen then
TweenClosePos:Play()
end
end
Honestly I think in most games they position the tween to look like it moves around a hinge. Also, about that part where you set the properties of the door you want to tween. No need for CFrame.Angles. CFrame should account of position and rotation so I think you could remove that part. Maybe i’m missing something sorry.
Oh, I know why that happens. Remove the CFrame.new() part since CFrame.new() takes two arguments, Position of the item and the position you want the item to face. When you do CFrame.new(ClosePos.CFrame) you are trying to give a cframe value to the cframe.
You would want to create essentially a custom lerp that handles your CFraming to get a nice animation effect + a circular motion like a realistic door.
When tweening CFrame you might want to tween essential parts of the motion that you want the door to have if the tween won’t look realistic when you do it all the way. So make it tween halfway closed then make another tween that plays after it is halfway closed that makes it fully closed.
Example: When I am making a walk animation. If I just put the end position at the end without putting any key sequences in between I don’t know how the animation plugin will make the arms move to reach that position.
local door = game.Workspace.Part
wait(3)
local dX2 = door.Size.X / 2
local startCFrame = door.CFrame
function CFrameAtPercentage(p)
return startCFrame
* CFrame.new(-dX2, 0, 0)
* CFrame.Angles(0, math.rad(90) * p, 0)
* CFrame.new(dX2, 0, 0)
end
function LerpN(a, b, t)
return a + (b - a) * t
end
local i = 0
while true do
i = LerpN(i, 90, 0.05)
door.CFrame = CFrameAtPercentage(i/90)
wait()
end
If you make the door a Model, and make the hinge the PrimaryPart of the Model, and all the Parts are Anchored, the script will rotate it around the PrimaryPart automatically.