How to rotate object over time and oscillate November 29, 2016 /

I wanted to have an object rotate back and fourth between two specific angles on a single axis over time; it’s for a rocking 2D platform.
public Vector3 from = new Vector3(0f, 0f, 135f);
public Vector3 to = new Vector3(0f, 0f, 225f);
public float speed = 1.0f;

void Update() {
float t = Mathf.PingPong(Time.time * speed * 2.0f, 1.0f);
transform.eulerAngles = Vector3.Lerp (from, to, t);

}

 

Source code stackoverflow