Fix tweening orientation jump

I know this happens because of how orientation works, going from -180 to 180, etc. How can I fix this?

Code if needed:

door.ProximityPrompt.Triggered:Connect(function(player)
	local char = player.Character 
	local doortochar = (char.HumanoidRootPart.Position - door.Position).Unit
	local doorangle = door.CFrame.LookVector
	local dot = doortochar:Dot(doorangle)
	
	if open.Value then
		open.Value = false
		closeSound:Play()
		flipped = false
		local tween = ts:Create(pivot, ti, {Orientation = Vector3.new(0, doorRot, 0)})
		tween:Play()
	else
		open.Value = true
		openSound:Play()
		if dot > 0 then
			flipped = false
			local tween = ts:Create(pivot, ti, {Orientation = Vector3.new(0, doorRot+90, 0)})
			tween:Play()
		else
			flipped = true
			local tween = ts:Create(pivot, ti, {Orientation = Vector3.new(0, doorRot-90, 0)})
			tween:Play()	
		end		
	end
end)
1 Like

I think the easiest way to fix this is to change the 180 to 179 since it will tween to the closest direction so basically make it so its 1 degree closer to where it is supposed to be when closed

I have actually tried this, changing +90 to +89, and it worked. However, when placing the door 90 degrees from what it is now, it flips around still.

have you tried “lerping” it could be a better solution

Lerping fixed some of it, which is good.
However when the starting rotation value is 180, the lerp goes crazy trying to add 89 to 180 since orientation can’t go over 180.

alright, I’ll just make a new place and test your script or maybe give you one I make

can you send me the whole script or a .rbxm file? would be better

I’ve heard using CFrame is probably more reliable in this situation, but I can’t figure out how to use it :smiling_face_with_tear:

door.rbxm (6.7 KB)

1 Like

i used a fixed value for the orientation, used tweening btw, Read The Ending Parts, was way too simple? ;-;

local ts = game:GetService("TweenService")
local rs = game:GetService("RunService")
local door = script.Parent.door
local pivot = script.Parent.pivot
local doorRot = door.Orientation.Y
local open = script.Parent.open
local flipped = false

local openSound = door.opensfx
local closeSound = door.closesfx

local ti = TweenInfo.new(
	0.5 -- Time
)


door.ProximityPrompt.Triggered:Connect(function(player)
	local char = player.Character 
	local doortochar = (char.HumanoidRootPart.Position - door.Position).Unit
	local doorangle = door.CFrame.LookVector
	local dot = doortochar:Dot(doorangle)
	
	if open.Value then
		open.Value = false
		flipped = false
		closeSound:Play()
	else
		open.Value = true
		
		if dot > 0 then
			flipped = false
		else

			flipped = true
		end
		openSound:Play()
	end
	
	
end)

local offset = pivot.CFrame:inverse() * door.CFrame -- offset before rotation
rs.Heartbeat:connect(function(dt)
	if open.Value then
		if flipped then
			ts:Create(pivot , ti , {Orientation = Vector3.new(0 , 90 , 0)}):Play()
		else
			ts:Create(pivot , ti , {Orientation = Vector3.new(0 , -90 , 0)}):Play()
		end
	else
		ts:Create(pivot , ti , {Orientation = Vector3.new(0 , 0 , 0)}):Play()
	end
	door.CFrame = pivot.CFrame * offset -- apply offset to rotated hinge
	print(flipped)
end)

The thing is, that does work if the rotation is set at (0,0,0). However, at some angles it won’t work, and will jump like I described before.

For example, if you rotate the door to (0,180,0), try opening it, it’ll probably jump.
Here’s code describing what I mean.

local offset = pivot.CFrame:inverse() * door.CFrame -- offset before rotation
rs.Heartbeat:connect(function(dt)
	if open.Value then
		if flipped then
			ts:Create(pivot , ti , {Orientation = Vector3.new(0 , 90 , 0)}):Play()
		else
			ts:Create(pivot , ti , {Orientation = Vector3.new(0 , -90 , 0)}):Play()
		end
	else
		ts:Create(pivot , ti , {Orientation = Vector3.new(0 , 180 , 0)}):Play()
	end
	door.CFrame = pivot.CFrame * offset -- apply offset to rotated hinge
	print(flipped)
end)

well idk about that, it’s really weird… my best idea is to use CFrame or an if statement so it never reaches 180 or another solution is using “hings” its an item in roblox but it isnt very practical, I need to go now, if it isn’t solved when I comeback I’ll look more through it! Have a nice day!

and why don’t you try changing the pivot of the door other than having a part and from there you can easily rotate and Hopefully without bugs

Try modulo dividing the orientation by 360.

Also, it’s better if you stored the tween in a variable outside instead of making one each time you press the prompt.

local close = ts:Create(pivot, ti, {Orientation = Vector3.new(0, doorRot%360, 0)})
local open1 = ts:Create(pivot, ti, {Orientation = Vector3.new(0, (doorRot+90)%360, 0)})
local open2 = ts:Create(pivot, ti, {Orientation = Vector3.new(0, (doorRot-90)%360, 0)})
door.ProximityPrompt.Triggered:Connect(function(player)
	local char = player.Character 
	local doortochar = (char.HumanoidRootPart.Position - door.Position).Unit
	local doorangle = door.CFrame.LookVector
	local dot = doortochar:Dot(doorangle)
	
	if open.Value then
		open.Value = false
		closeSound:Play()
		flipped = false
		close:Play()
	else
		open.Value = true
		openSound:Play()
		if dot > 0 then
			flipped = false
			open1:Play()
		else
			flipped = true
			open2:Play()	
		end		
	end
end)