I see various issues here
you increase transformZ
by 0.1
every 0.1
seconds => this will count seconds, not the actually applied rotation (see further below)
you rotate about a value transformZ
that is getting bigger each time. Have in mind that Rotate
does not set a final rotation but rather starts add the current rotation and adds to it.
You are rotating like
Iteration | current rotation | transformZ | resulting rotation
1 | 0 + 0 = 0
2 | 0 + 0.1 = 0.1
3 | 0.1 + 0.2 = 0.3
4 | 0.3 + 0.3 = 0.6
5 | 0.6 + 0.4 = 1.0
...
then also in general never use ==
/ ! =
for floating point values
using Rotate
there is always the possibility that you overshoot the target rotation
and then personally I would prefer a continuous rotation instead of 0.1
second jumps.
A solution depends a bit on what you want to control.
For a fixed rotation per second I would do
[SerializeField] private float anglePerSecond;
[SerializeField] private float maxAngle = 90;
IEnumerator Start()
{
...
var initialRotation = transform.rotation;
// precalculate the desired rotation going through Quaternion instead of directly Euler space
var targetRotation = initialRotation * Quaternion.Euler(0, 0, maxAngle);
// here "Quaternion != Quaternion] uses a lower precise check that allows for small rounding errors
while (transform.rotation != targetRotation)
{
// with linear anglePerSecond rotate towards the final rotation
// Without ever overshooting the target rotation
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, anglePerSecond * Time.deltaTime);
yield return null;
}
// to be sure apply final rotation
transform.rotation = targetRotation;
}
Or if you rather want to control the duration in seconds
[SerializeField] private float durationInSeconds = 1f;
[SerializeField] private float maxAngle = 90;
IEnumerator Start()
{
...
var initialRotation = transform.rotation;
var targetRotation = initialRotation * Quaternion.Euler(0, 0, maxAngle);
// similar as before but this time iterate over a certain time
for(var timePassed = 0f; timePassed < durationInSeconds; timePassed += Time.deltaTime)
{
// factor linear growing from 0 to 1
var factor = timePassed / durationInSeconds;
// advantage: you can add ease in/out quote easily e.g.
//factor = Mathf.SmoothStep(0, 1, factor);
// interpolate via given factor
transform.rotation = Quaternion.Slerp(initialRotation, targetRotation, factor);
yield return null;
}
transform.rotation = targetRotation;
}