smooth 180 rotation

Hello
On a Tap I am trying to rotate a game object smoothly 180º

	public bool rotateMe = false;
	public float amount=180f;
        public float time = 10f;
	float t = 0;

	
	void Update() {
		if (rotateMe == true) { 
			while(t<=time)
			{
			t++;
				transform.Rotate (0f, (Time.deltaTime*amount) , 0f);
			}
		}
	}

	void OnMouseUp() {
		Debug.Log("MouseHit");
			rotateMe = true;
}

Update is a function called at each frame, so what you are doing is completly wrong. Having a while loop in this context is not a good practice. What you want is at each frame, so each update, your character rotates a little until the frame where it reaches 180°.

float speed = 10f;
float currentRotation = 0f;
float amount = 180;

void Start()
{
    // this disable the Update function
     enabled = false;
}

public void RotateCharater()
{
     currentRotation = transform.eulerAngles.y;
    enabled = true;
} 
     
void Update() 
{
    transform.Rotate (0f, Time.deltaTime*speed , 0f);
   if(transform.eulerAngles.y >=  currentRotation+amount)
  {
       enabled = false;
  }
}

I wrote this in blind, so I am sorry if there are some mistakes, but this is the idea.
Please let me know me if something is not clear to you, I’ll explain.

You should know, that “Update” is called every frame. Take a look at Unity’s scripting beginner tutorials . In every Frame (so every Update-Call) you should rotate the object by Time.DeltaTime. Actually your code does the following:

  • if the Mousebutton is getting released count t from 0 to 10
  • On every increase rotate the object by Time.DeltaTime (~0.02s)*180 degrees, which makes 3.6 degrees.
  • SInce this is done 11 times (0-10), your object gets rotated ~39.6 degrees

The following code should do the trick:

bool RotateMe=false;
public float amount=180f;
public float time=10f;

//The amount of degrees, which is left of the original amount (180 degs)
float DegreesLeft;

void Update(){
	if(RotateMe){
		//Calculate the amount of degrees, which the object should turn this frame
		float DegreesThisFrame = (amount*Time.DeltaTime)/time;
		if(DegreesThisFrame>DegreesLeft){
			//If the amount to turn this frame is bigger than the amount,
			//which is left to 180degs, set the amount of degrees to turn to the amount left.
			//Otherwise the complete turn will be a bit above 180 degrees.
			DegreesThisFrame=DegreesLeft;
			//Since the turn is done, set "RotateMe" to false again
			RotateMe=false;
		}
		//Now actualy rotate the object
		transform.Rotate (0f, DegreesThisFrame , 0f);
		//And subtract the rotated amount from the amount left
		DegreesLeft-=DegreesThisFrame;
	}
}

void OnMouseUp(){
	RotateMe=true;
	DegreesLeft=amount;
}

Thank You @Murkas Thank you
~be