# Rotate GameObject on click

I'm trying to get a card to flip when clicked but the flipping only works the first flip. While 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");
}
}

• As a quick note, you can do this using coroutines. This helps to keep your Update() function clean, and you can store variables in the coroutine instead of having to put them on your class. Commented Dec 27, 2013 at 23:11

## 2 Answers

The third parameter of the Lerp() method is the interpolation factor between from and to and should be between 0 and 1. Every time you flip a card you want to make this factor go from 0 to 1 over a period of time. You can do this as follows:

1. Add a float member variable t.
2. When the card is clicked set t to 0.
3. In your update function add t += Time.deltaTime * speed; if(t > 1.0f) t = 1.0f;
4. Pass t as the third parameter to the Lerp() method.

Note: if you can flip cards while they're still rotating, the rotation speed will not be constant this way. E.g. if flipping a card takes 1 second, flipping it again half way through will take 1 second as well instead of 0.5 second. You can make the speed constant by not setting t to 0 on click, but interpolating the value based on the current rotation.

try working with the transform.Rotate(...) function. I think it's easier to use and it doesn't require quaternions.

As for your game logic I'd make a boolean which states if the card is flipped or not and according to that use the right transform.Rotate(...) function.