How to stop rotation after a certain limit

Fairly new to unity and C#. Tried to make a little game where I can mess around with the physics and build objects and make them interact and stuff. Ran into a little problem when I tried making one of my objects rotate where I couldn’t set a limit to when it can stop.

float rotation = 80;

	public GameObject explosionPrefab;

	void Update()
	{
		transform.Rotate (new Vector3 (0, 0, rotation));
		while (rotation >= 0) {
						rotation = rotation + Time.deltaTime;
				}
	}

I’ve tried using if statements but that hasn’t helped either. I’m fine with the object still rotating a bit when rotation stops changing. When it is rotating, it also jitters. It looks like the entire model is shaking very quickly while it rotates. Also if anyone can show me a way to find or a remade c# version of the character motor script prebuilt into unity that would be appreciated, too.

So you have a while loop in your Update function, but you could probably make this an if statement.

Update is called every frame, so your rotation is very quick and jittery when it is within a loop like this, and it will also display erratically.

Also, you perform the rotation before modifying. While this will technically work, it is more confusing to read, and may have unintended results later on.

In terms of your own code, something like this should do the trick:

 float rotation = 80.0f;
 
 public GameObject explosionPrefab;
 
 void Update()
 {
     if (rotation >= 0) {
                rotation = rotation + Time.deltaTime;
     }
     transform.Rotate (new Vector3 (0.0f, 0.0f, rotation));
 }

In order to limit the rotation, you would have to set a value greater than the starting rotation eg starting rotation is 80 so:

if (rotation >= 260.0f) {

would allow the object to rotate 180 degrees before stopping.

I would go for -

float rotationAngle = 80;
float smoothTime = 1.0f;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
	Quaternion desiredRotation = Quaternion.Euler (0,0,rotationAngle);
	transform.rotation = Quaternion.Lerp (transform.rotation, desiredRotation, smoothTime);
}

@VioKyma could you tell me the full code bcs i didnt understand