How can I tween my door based on its rotation?

Howdy!

I’m making a sci-fi style door that opens in a pretty fancy way.

Pretty cool!

I’ve been trying to figure out how to make the movement of my doors scalable in regards to rotation. Currently if my door is rotated on any axis it will not function properly, and the parts will not tween correctly, as I do not fully understand the maths.

I originally went to tackle this problem using Prismatic Constraints but the documentation is a bit lacking, and I am fairly sure it wasn’t going to work for my anchored door parts. Instead I’ve been trying to use TweenService as my doors will be stationary once placed.

Currently my door opening script is like so:

local function tweenDoor(moving)
	for i, v in pairs (moving.Str:GetChildren()) do
		local newCF = v.CFrame
		if v.Name == "WedgeBR" then
			newCF = v.CFrame + Vector3.new(-6.5, 0, 0)
		elseif v.Name == "WedgeLB" then
			newCF = v.CFrame + Vector3.new(0, 6.5, 0)
		elseif v.Name == "WedgeRT" then
			newCF = v.CFrame + Vector3.new(0, -6.5, 0)
		elseif v.Name == "WedgeTL" then
			newCF = v.CFrame + Vector3.new(6.5, 0, 0)
		end
		ts:Create(v, tweenInfoSlide, {["CFrame"] = newCF}):Play()
	end
	for i, v in pairs (moving.Ang:GetChildren()) do
		local newCF = v.CFrame
		if v.Name == "WedgeBL" then
			newCF = v.CFrame + Vector3.new(-4.875, 3.5, 0)
		elseif v.Name == "WedgeLT" then
			newCF = v.CFrame + Vector3.new(3.5, 4.875, 0)
		elseif v.Name == "WedgeRB" then
			newCF = v.CFrame + Vector3.new(-3.5, -4.875, 0)
		elseif v.Name == "WedgeTR" then
			newCF = v.CFrame + Vector3.new(4.875, -3.5, 0)
		end
		ts:Create(v, tweenInfoSlide, {["CFrame"] = newCF}):Play()
	end
end

Needless to say this is some pretty cursed code that will break with any slight variation. I’m pretty awful with CFrame maths, I know the distances I want to move each part in relation to their starting point (as shown in the code), I just can’t figure out how to then translate that movement depending on how the door is rotated.

Help?

In case you were wondering, this is how my door opens at any rotation:

1 Like

You’re adding vectors to CFrames, but it would be better to combine two CFrames to maintain orientation.

For example, this will tween a part 5 studs up relative to the part’s orientation.

TweenService:Create(part, TweenInfo, {CFrame = part.CFrame * CFrame.new(0, 5, 0)}):Play()

You can see this in this gif where all four parts (top face marked by hinge) are tweened the same, but move in different directions.

In your case you can further simplify the code by simply applying the same tween to all parts, and achieving the same result in a more efficient manner, as well as accounting for the rotation of the door.

2 Likes

Thanks for the speedy response @MayorGnarwhal.

Now I’m going to spend some time figuring out how to handle the protruding triangles :slight_smile:

1 Like