Rotating Object: Axis flip

I’ve got a Game Object, that ist, basically a big ring. The Game Object itself rotates continouusly negative around it’s x-axis, and I have to know, where it is currently.
At least, the Object is normally, without the x-Rotation, not rotated in any way…
Around the Object are 40 “Sections” and a My script has to know wich of these Sections is at top.

So I use the rotation.eulerAngles.x value and diviedy it by 9. With Math.RoundToInt I finally get the Section that is at top. Works all very fine, but it never gets to values from 270° over 180° to 90°. The ring rotates as it has to, but at 270° the x-Value instead of going on to 180° starts growing again to 360°, then counting upwards to 90° and there it turns back again and so on and so on… so my Problem now is that I get the “upper” Sections all twice like a Ping-Pong-Animation, and never the “lower” sections.
I know where the actual problem is: at x270° the y and z values flip from 0° to 180°, and at x90° the flip back to 0.

How can I get the real active Section now?

Oh and: Here Is my code:

var CurrentSection : int;
function Update () 
{
	Debug.Log(transform.rotation.eulerAngles);
	CurrentSection = Mathf.RoundToInt(transform.rotation.eulerAngles.x / 9.0);//Here I calculate the active Section
	if(CurrentSection == 40)
	{
		CurrentSection = 0;//Section 40 is the same as Section 0, So I save them as the same value...
	}
}

How are you rotating the ring? I suspect that the cause of the y/z value flip you are seeing is contained there.

It might be easier to track the current section number in the update function that also rotates the ring, like so:

var kNumSegments : int = 9;
private var kSegmentSize : float = 360.0 / kNumSegments;

var speed : float = 5.0;
var segmentDelta : float = 0.0;
var currentSection : int = 0;

function Update()
{
   var delta = Time.deltaTime * speed;

   // Rotate ring (= this object)
   transform.Rotate(delta, 0, 0);

   // Update segment index
   segmentDelta += delta;
   if (segmentDelta > kSegmentSize)
   {
      segmentDelta -= kSegmentSize;
      currentSection++;
      if (currentSection > kNumSegments)
         currentSection -= kNumSegments;
   }
}

It works just fine with your idea!
I just had to tweak one or two things to fit into my Script, Because I use an enum to store the sections, and the Sections are Counted the way back the ring is rotating, but it simply works!

Sometimes I think, I work too long on the same problem… I should make some more breaks :wink:

Thanks for the help, it came to the right time!

going-off-to-read-a-book