- Home /
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:
GameObject can be rotated to the right or left from any point.
If player change rotation direction we need to start measuring a new 360 rotation.
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:
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.
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... :\
Answer by SunnyChow · Jan 24, 2018 at 10:26 AM
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;
Thanks for your detailed reply! It seems i still have two things left:
[1] I Update the gameObject rotation by using acceleration (Added to question):
Vector3 temp = Input.acceleration.normalized;
temp.z = 0;
currentLetterGameObject.transform.up = temp.normalized;
Not sure how to combine it with your code.
[2] I need to have 0 to 1 values while GameObject is rotating to a specific side to change it's color gradually using the Lerp function.
Answer by giantkilleroverunity3d · May 03, 2019 at 06:25 PM
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.
Your answer
Follow this Question
Related Questions
Button Enabling/Disabling using Collision Triggers? 1 Answer
How to Set the Game Speed Based on the Player's Current Score? 1 Answer
How to make the calculated angle respective of the players rotation? 1 Answer
Distribute terrain in zones 3 Answers
How can I limit the rotational speed of a gameObject tracking my mouse? 1 Answer