Rotating on X axis causing the part to flip

I want a part to rotate only on Y axis. The issue is that when I rotated on X axis, the Y axis flipped itself and I didn’t even touch the Y axis at all. How can I fix this issue?


Here’s the code.

local Folder = script.Parent
local Part = Folder.Part
local TargetPart = Folder.TargetPart
local Runservice = game:GetService("RunService")

Runservice.Stepped:Connect(function()

    local TargetPartOrientations = TargetPart.Orientation.Y

    Part.CFrame = CFrame.new(Part.Position) * CFrame.Angles(0,math.rad(TargetPartOrientations),0)

end)

robloxapp-20241103-2144559.wmv (950.4 KB)
If you keep the red part on the top of the screen, you can see that it rotates on the y axis. Just how angles work i guess.

This happened when I rotated on X axis. It flipped just like the video you ashowed me. When I rotated the Z axis, nothing changes. What I want to archived here is when I rotated X axis, the Y axis will not be changed just like the Z axis. So how would I prevent this issue from happening?

if TargetPart.Orientation.X > 180 then
     TargetPartOrientations += 180
end

Can you do this so that when I rotated Y axis, it will not flip. Is it possible?

After I applied your code, the Y axis flipped when I rotated it.

The code is working. Rotating X axis doesn’t make the Y axis flip anymore. The problem is when I rotated the Y axis, it flipped itself. How do you fix this? And here is the code.

RunService.Stepped:Connect(function()

    local TargetPartOrientations = PartA.Orientation.Y
    if PartA.Rotation.X%360 > 90 and PartA.Rotation.X%360 < 270 then
        TargetPartOrientations += 180
    end
    PartB.CFrame = CFrame.new(PartB.Position) * CFrame.Angles(0,math.rad(TargetPartOrientations),0)

end)

I dont know sorry

i’ve finally found a solution for this! hope it helps.

local partA = workspace.PartA

local partB = workspace.PartB

local previousY = partB.Orientation.Y

game:GetService("RunService").PreSimulation:Connect(function()

	if not (partA.Orientation.X >= 25 or partA.Orientation.X <= -25 or
partA.Orientation.Z >= 25 or partA.Orientation.Z <= -25) then

		if not (partB.Orientation.Y == 180 and previousY ~= 180) then
			partB.Orientation = Vector3.new(0, partA.Orientation.Y, 0)
		end

	end

	task.wait(.05)

	previousY = partB.Orientation.Y

end)
1 Like

This is exactly what I wanted. Thanks for the help!

No problem! glad it helped, orientation was so weird to figure out lol

I’m surprised you used PreSimulation instead of stepped. Also I don’t use not on my if statements so I find it a bit hard to understand what the code does. Hopefully it’s not hard.

yeah, i’ve been using the new RunService events (particularly PreRender) for some time now, and i’ve updated the script a bit, here’s how it works now:

the threshold is = to 25,
and if the X or Z Orientation of partA is not above or below the threshold,
and instead in between it then

    if the Y Orientation of partB and partA are less than 180 then
        print("allow partB to rotate.")
    end.

end.

here’s the updated code:

local partA = workspace.PartA

local partB = workspace.PartB

local threshold = 37.5

game:GetService("RunService").PreSimulation:Connect(function()

	if not (partA.Orientation.X >= threshold or partA.Orientation.X <= -threshold or
		partA.Orientation.Z >= threshold or partA.Orientation.Z <= -threshold) then

		if partB.Orientation.Y < 180 and partA.Orientation.Y < 180 then
			partB.Orientation = Vector3.new(0, partA.Orientation.Y, 0)
		end

	end

end)

hopefully you understand it a little more now :slight_smile:

Thanks for your explanation. However I find this part a bit more confusing to me as I am not used to making if statements. Can you explain this part in detail? What does this thing do after that?

Sure, if the threshold is 25

then if the partA.Orientation.X or partA.Orientation.Z is, for example, 25 or lower, then partB won’t rotate at all, the same logic is applied for negative degrees (-25 for example), the reason the threshold is 37.5 in the script is because anything higher (for example, 45) would result in partB rotating weirdly, I’m writing this on my iPad btw so forgive me for any errors.

I think I understand the code now. Thanks.

no problem. (30lim)

What does that mean at the end? 30lim?

i just put that there since replies have to be 30 c.h.a.r.a.c.t.e.r.s long (which is dumb to me lol, also ur not allowed to say that word along with ‘30’ :skull: )

Oh, I see. Why is it not allowed?

idk, they say the limit is because a reply should be meaningful, but sometimes theres not really much else to say, and sometimes they’ll flag me for using (30 c.h.a.r) in my reply, which luckily dosen’t penalize me as far as I’m aware.