How do I Detect if an object has made a full rotation and gradually change its color
Hello,
Has topic suggests I need to detect full rotation of an object. While doing this i want that the color of the object will change from white (starts rotation at 0 degrees) to to green (at full rotation, return back to starting position)
For change color i am usings Lerp:
LetterText.color = Color.Lerp(Color.white, Color.green, Mathf.Abs(currentLetterGameObject.transform.rotation.z));
Which only works half way since it's goes from 0 to 1 (180 degree) and 1 to 0 (other 180 degree)
Object is rotated inside Update() function.
Answer by Lairinus · Jan 23, 2018 at 02:18 AM
You want transform.eulerAngles, not transform.rotation
https://docs.unity3d.com/ScriptReference/Transform-eulerAngles.html https://docs.unity3d.com/ScriptReference/Transform-rotation.html
Transform.rotation is the Quaternion representation of the object's rotation, and Z is a number between 0 and 1 and does not correspond to the actual 360 rotation (by itself). What you want to use is transform.eulerAngles.
The final code is as follows (notice the division):
LetterText.color = Color.Lerp(Color.white, Color.green, Mathf.Abs(currentLetterGameObject.transform.eulerAngles.z / 360));
For what it's worth, you were close :)
Working! thanks. Note that to change color correctly for the opposite rotation side we need to do subtraction of 1
$$anonymous$$athf.Abs(currentLetterGameObject.transform.eulerAngles.z / 360-1)
One issue i encounter with this, is that if i want to start detecting full circle rotation from a specific point. like Start at angle 60.. and target is 60 again.. issue that when moving over the angel 0, it reset the color back to white... Even when doing the following:
LetterText.color = Color.Lerp(Color.white, Color.green, ($$anonymous$$athf.Abs(currentLetterGameObject.transform.eulerAngles.z) - $$anonymous$$athf.Abs(startRotationAngle)) / 360);
Your answer
Follow this Question
Related Questions
Help me with joystick setting 1 Answer
I need help with AI,Force not working 0 Answers
How do I make a ball bounce in a circle? 0 Answers
Game progress restart / Prestige in Unity 0 Answers
RayCast 2d change start position ? 0 Answers