CFrame rotates fine when going in all directions except one

while scripting a glider I ran into a problem where the glider does not turn correctly when going in one direction but turns fine when going in every other direction. For some reason when the glider faces toward negative on the z-axis this problem starts (it works fine going in any other direction). Below is an example of the glider working as intended and as not.

Glider working properly facing any direction except -z
working.gif

Glider not working facing negative on the z-axis
notworking.gif

script lines used to turn the glider

                        local cx,cy,cz = camera.CFrame:ToOrientation()
			local px,py,pz = cpart.CFrame:ToOrientation()

			
			local mag = (Vector3.new(cz,cx,cy) - Vector3.new(pz,px,py)).Magnitude
			local magrad =  math.rad((Vector3.new(cx,cy,cz) - Vector3.new(px,py,pz)).Magnitude)

			local turn_time = magrad * 10
			local tiltangle = mag
                        
                        local a = CFrame.fromOrientation(px,py,pz)
                        local b = CFrame.fromOrientation(cx,cy,cz)
			

			local relative = a:Inverse()*b    --b’s cframe relative to a's cframe

			local _,ry,_ = relative:ToOrientation()


			if ry > 0 then
				local c = CFrame.fromOrientation(0,py,0) * CFrame.fromOrientation(0,0,magrad)
				local _,_,z = c:ToOrientation()
				tiltangle =  z
				--warn("turn left")
			end
			if ry < 0 then
			local c = CFrame.fromOrientation(0,py,0) * CFrame.fromOrientation(0,0,-magrad)
			local _,_,z = c:ToOrientation()
			tiltangle =  z
			--warn("turn right")
		end	
turn_tween =  game:GetService('TweenService'):Create(oy, TweenInfo.new(turn_time, Enum.EasingStyle.Linear), {Value = CFrame.fromOrientation(0,cy,0)}):Play() --tween turns AlignOrientation on the y axis
			tilt_tween = game:GetService('TweenService'):Create(oz, TweenInfo.new(.1, Enum.EasingStyle.Linear), {Value = CFrame.fromOrientation(0,0,tiltangle*35)}):Play()--tween tilts AlignOrientation on the z axis to, tweens don't interfer with other tweens

I rotate the glider by setting an AlignOrientation’s CFrame to match the camera’s CFrame over time.
I have tried using a BodyGyro object in place of the AlignOrientation object but nothing changed.

1 Like

Is it because of gimbal lock?
I searched that and found a post dealing with this issue. Cool short quaternion vs Euler angles comparison

1 Like

Although gimbal lock wasn’t the problem, thanks for suggesting it @Scottifly. The real problem was the method I was using to get the distance between two rotations didn’t work.

1 Like