Rotate to a set degree then reverse and repeat in Unity
Posted
by
Ryan
on Game Development
See other posts from Game Development
or by Ryan
Published on 2013-11-09T21:04:27Z
Indexed on
2013/11/09
22:12 UTC
Read the original article
Hit count: 614
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
© Game Development or respective owner