'Rotating an object between -45 and 45 degrees not behaving as expected
I'm trying to have an object oscillate between -45 and 45 degrees, and the following is the code I am using. Note that direction is initialized to 1, zRotation to 360, and speed to 100. This works perfectly fine. However, when I change speed to 1000, the object does a complete 360 rotation before continuing to oscillate correctly. I'm confused why that happens.
This is my code(inside the Update method):
zRotation += speed * Time.deltaTime * direction;
var newRotation =
Quaternion.Euler(
new Vector3(
0,
0,
zRotation));
if (zRotation < 315)
direction = 1;
if (zRotation > 405)
direction = -1;
transform.rotation = newRotation;
I am using the values 315(-45 degrees) and 405(45 degrees) because it is easier to determine if my object has reached -45 and 45 degrees this way.
I'm aware that this behavior can easily and more cleanly be achieved using Lerp, but this is for an assignment and we haven't covered Lerp yet, so I don't want to use material that hasn't been taught yet.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
