Mouse click for 180º object rotate

Hi guys!
I’m making a memory card game and I don’t find a way to make a C# script to rotate my cards in 180º after my mouse click. Can you help me in this question?
I tried to use a script like that, but don’t work:

using UnityEngine;

using System.Collections;

public class Rotate180OnClick : MonoBehaviour {

public float myRotationSpeed = 100.0f;

public bool isRotateZ = false;	

private bool positiveRotation = false;

private int posOrNeg = 1;

//private float rotation = myRotationSpeed * Time.deltaTime * posOrNeg;

void Start () {

	collider.isTrigger = true;
	if(positiveRotation == false)
		posOrNeg = -1;


void Update () {

	clicou ();

		transform.Rotate(0, 0, myRotationSpeed * Time.deltaTime * posOrNeg);

void clicou(){

	if (Input.GetButton ("MouseRightButton")) {
		isRotateZ = !isRotateZ;


Two possible problems:

  1. You haven’t binded the name MouseRightButton. By default the name is Fire2. You can check this in Project Settings > Input > Axes
  2. You are using GetButton which is called every time key is held down so isRotateZ value is constantly changed. You could use, for example, GetButtonUp instead.


I wrote you an example code, hope it helps.

private bool isRotating = false;
private float rotationAmount = 0.0f;
private float rotationSpeed = 100.0f;

void Update()
    if (Input.GetButtonUp("MouseRightButton") && !isRotating)
        isRotating = true;
    if (isRotating)
        float rotation = rotationSpeed * Time.deltaTime;

        if (rotationAmount + rotation >= 180.0f)
            rotation = 180.0f - rotationAmount;
            isRotating = false;
            rotationAmount = 0.0f;
            rotationAmount += rotation;

        transform.Rotate(0, 0, rotation);

Ok, ejpaari.
I create a Input MouseRightButton just do not change a default input in list. My Fire2 Input is configured with “left alt” in Positive Button field.
About the GetButton I change to GetButtonDown now and it’s working better.
But this isn’t my question… I need to click in my card object and it need to rotate 180º and stop, waiting the next click to rotate again.
Do you understand me?
Thank you for your help!