Rotate to a set degree then reverse and repeat in Unity
- by Ryan
and thank you for your time.
I'm making my first project in Unity, a simple game where touching objects adds points to the players score.
I'd like the objects to have a pleasant back and forth swaying animation on the Z axis. Nodding to the right 30 degrees, then to the left 30 degrees, on and on.
Here's what I've got...
public class Rotator : MonoBehaviour
{
void Update ()
{
transform.Rotate(new Vector3(0,0,12)*Time.deltaTime);
}
}
This gives me a nice slow rotation. But I am clueless how to tell Unity to stop at +30 degrees, reverse to -30 degrees, rotate again to +30, stop and repeat, etc, etc.
I'd really appreciate any help. Maybe there is a thread like this that I was not able to find? I assume it will involve some kind of 'if than' function?
Thank you,
Ryan