How to rotate any part on only one axis using TweenService?

I’ve been trying to make a small puzzle and I reached a small problem.

I made a circle which is going to rotate by clicking on it. Every time I click, the circle will rotate for 90 degrees on X axis from it’s current position, but when it reaches 0 degrees on it’s X axis, it will rotate backwards and forward again.
Here are the pictures of every step of rotating.


When clicked again, it will rotate backwards.

Here is the script I’m currently using for rotating this circle:

--[VARIABLES]--

local Circle_1        = game.Workspace.Circle_1
local TweenService    = game:GetService("TweenService")

--[FUNCTIONS]--

function click_left_1()
	local InfoA = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
	local GoalA = {
	Orientation = Circle_1.Orientation + Vector3.new(90, 0, 0)
	}
	local TweenA = TweenService:Create(Circle_1, InfoA, GoalA)
	TweenA:Play()
	TweenA.Completed:Wait()
end

Circle_1.Click.MouseClick:Connect(click_left_1)

I tried using CFrame.Angles, but TweenService doesn’t support CFrames and I can’t think of any idea how would I rotate this circle constantly by 90 degrees on X axis.

3 Likes

I answered this on the new members discord, but I’ll reiterate here for others to see. The issue here is that when you apply another 45 degrees to X at Orientation == (90, 0, 0), e.g. (90 + 45, 0, 0), Roblox will just convert it to (45, 180, 180); which is, correct me if I’m wrong, mathematically equivalent to 135, 0, 0. From there you get stuck in an infinite loop of going back and forth between (90, 0, 0) and (45, 180, 180). My proposed solution is to use CFrame instead of Orientation with the added benefit of applying the rotation relative to your circle, so it’ll spin correctly no matter how you orientate it originally.

-- Replace your properties table entry with this:
CFrame = Circle_1.CFrame * CFrame.fromAxisAngle(Vector3.new(1, 0, 0), math.rad(90))

What this line does is it first grabs your current CFrame (position vector and a rotation matrix), and applies another CFrame on top as a transformation. Resulting in a new desired CFrame with a new rotation relative to the last.

21 Likes

Dude. Your explanation is dead on as to what I’ve been observing when I try and rotate parts in my game. I saw what it was doing but couldn’t figure out what I needed to fix. CFrame was the key. Thank you!