- Home /
Question by
Lautaro-Arino · Dec 27, 2013 at 08:00 PM ·
quaternionlerp
Rotate GameObject on click
Im trying to get a card to flip when clicked but the flipping only works the first flip. Debugging i can see that OnMouse is called every time i click.
public class CardController : MonoBehaviour
{
bool isShowingFront = true;
bool isFlipping = false;
float speed = 0.005F;
Quaternion flipRotation = new Quaternion (0, 0, 0, 0);
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (transform.rotation.y != flipRotation.y) {
transform.rotation = Quaternion.Lerp (transform.rotation, flipRotation, Time.deltaTime * speed);
isFlipping = true;
} else {
isFlipping = false;
}
}
void OnMouseUp ()
{
if (isShowingFront) {
flipRotation.y = 180;
} else {
flipRotation.y = 0;
}
isShowingFront = !isShowingFront;
Debug.Log ("I was clicked");
}
}
Comment
Best Answer
Answer by TowerOfBricks · Dec 27, 2013 at 10:06 PM
What lerp does is that it moves some percentage closer to the desired rotation. Mathematically, you will never reach the target value, instead it would asymptotically approach the value ( like this curve ). However since we are using computers, and computers use representations of numbers which are limited to some precision, it will reach that value eventually, could take some time though.
That means that the IF statement in your Update method will never be true. What you should try instead is something like
if ( Mathf.Abs ( transform.rotation.y - flipRotation.y ) < 0.01 )
or
if ( Mathf.Approximately ( transform.rotation.y, flipRotation.y ) )