# Detect Full 360 rotation of an object when direction changes and change it's color.

Hello Guys,

I hope there is a vector math formula for this.

Rules are simple:

1. GameObject can be rotated to the
right or left from any point.
2. If player change rotation direction
we need to start measuring a new 360
rotation.
3. Color change from white to green
when measuring the 360 rotation.

This is my current code:

`````` private void Update()
{
Vector3 temp = Input.acceleration.normalized;
temp.z = 0;
currentLetterGameObject.transform.up = temp.normalized;

//direction right
if (MyGameObject.transform.eulerAngles.z > previousAngle)
{
if (rotationRight)
{
rotationCount = 0;
startRotationAngle = previousAngle;
rotationRight = false;
}

rotationCount = (Mathf.Abs(MyGameObject.transform.eulerAngles.z) - Mathf.Abs(startRotationAngle)) / 360;
MyGameObject.GetComponent<LetterBase>().LetterText.color = Color.Lerp(gameController.LetterDefaultColor, Color.green, rotationCount);
}
//direction left
if (MyGameObject.transform.eulerAngles.z < previousAngle)
{
if (!rotationRight)
{
rotationCount = 0;
startRotationAngle = previousAngle;
rotationRight = true;
}

rotationCount = (Mathf.Abs(MyGameObject.transform.eulerAngles.z - 360) - Mathf.Abs(startRotationAngle - 360)) / 360;
MyGameObject.GetComponent<LetterBase>().LetterText.color = Color.Lerp(gameController.LetterDefaultColor, Color.green, rotationCount);
}
//full circle
if (rotationCount > 0.925f)
{
MyGameObject.LetterText.color = Color.green;
rotationCount = 0;
Debug.log("END OF GAME");
//Quit
}
}
``````

Issues:

1. It’s not solid code, i look for
rotationCount>0.925f to try to
detect full circle, and it can be
skipped if player moves rotation too
fast.
2. If reaching
MyGameObject.transform.eulerAngles.z
to ZERO angle it reset the color to white and reset
starting point (it jumps between two
“if’s” and reset parameters).

This is a challenge if someone accepts it, i can’t seem to solve it correctly… :\

directly using transform.eulerAngles is a bad idea. Unity doesn’t store this angle value, it’s calculated from transform.rotation which is Quaternion. So some time it goes negative or completely mess up all axis. I would use Quaternion to do calculation and only convert it to Vector3 in last step.

``````// get the rotation between this frame and last frame
Quaternion rotated = Quaternion.Inverse(lastRotation)*transform.rotation;
Vector3 rotatedAngle = rotated.eulerAngles;
lastRotation = transform.rotation;
// make sure the result is between -180 and 180
if (rotatedAngle .y<-180)rotatedAngle.y +=360;
if (rotationAngle.y>180)rotatedAngle.y -=360;
``````

Very sloppy here. Missing or bad variable naming, i.e. rotationAngle declaration missing.
Great description in @SunnyChow comment but code lacking.
But this is the closest answer to ‘Counting revolutions forward or reverse’ that I could find after 3 days searching.
Most elbow benders just substitute revolution with rotation which is abhorrently incorrect.
I cannot find a revolution answer with wading through tons of rotation suggestions. I would bet that there are probably good revolution counting scripts out there but good luck finding them amidst all the rotation expert answers.