Trying to rotate character 180 degrees slowly - having issues

Hi Unity,

I am trying to make my character model rotate at a set speed for 180 degrees, while still moving forward the same direction ( because he moves forward at all times) but running into some issues. I have looked through a few scripts on here and this is what I have come up with:

(There is some code in here that also checks if you “Double Tapped” the A button instead of just pressing it.)

			if(Input.GetKeyDown(KeyCode.A))
			{
				if(!oneTap)  //check to see if this is the first time we pressed A
				{
					oneTap = true;
					timer_for_double_tap = Time.time;  //save the current time
					
					//Add any functionality for just pressing "A" here... if you wanted
				}
				else
				{
					Debug.Log("Double Tap");
					oneTap = false;  // found a double tap
					Transform boarderModel = GameObject.Find("SpaceBoarderModel").transform;
					
					//Do a 180 rotation Trick!

				   if (!rotating) 
					{
				
				      rotating = true;
				
				      float curRot = 0;
				
				      float startRot = boarderModel.eulerAngles.y;
				
				      while (curRot < 180) {
				
				         curRot += rotateSpeed * Time.deltaTime;
				
				         startRot = startRot + curRot;
				
				         break;
				
				      }
				
				      startRot = Mathf.Round(startRot + 180);
				
				      rotating = false;
				
				   }
				}

First I check to see if the user has tapped “A” twice, if they have, I want to start the rotation and rotate by “rotateSpeed” to 180 degrees. I use boarder model to get the transform of the actual model of my character, which is housed inside an empty gameObject named something else. The idea behind this was that the parent gameobject would continue to move forward while the child that I reference here will do the 180.

I hope that makes sense.

There is no errors, but my character does not receive any updates to their transform and of course does not rotate. Any ideas what I might have done wrong?

In Unity you accomplish a little bit of motion every frame. The primary place to make this change is in Update() or some method called from inside Update(). You first problem is that you have a while loop that does the rotation all at once rather than a little bit at a time. You want a rotation of rotateSpeed * Time.deltaTime at every call to Update.

Your second problem I think (since I’m not seeing all your code), is that you are not doing anything to make the object rotate. That is there are many ways to rotate an object in Unity. For example, you can call transform.Rotate() to incrementally rotate the object, or you can directly assign a specific rotation to transform.eulerAngles.

Here is a simple script that will rotate the object it is attached to around the Y axis by the number of degrees per second specified.

var degreesPerSecond = 30.0;

function Update () {
	transform.Rotate (Vector3.up * degreesPerSecond * Time.deltaTime);
}