rotate object over time and oscillate

This is part solution part curiosity. 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. Turned out to be one of those problems that should have gone much more smoothly than it did and had me scratching my head. Rotating the object over time was easy enough, trying to have it oscillate between the angles on a loop was where the problems began.

Although there are similar answers on here I’m posting because I couldn’t find any that worked for my situation and hopefully someone out there will find this helpful. I’ve also got a few questions about why certain things did or didn’t work.

Firstly, quaternions will NOT be getting a christmas card this year… or ever. Attempted a quaternion lerp and quickly got myself in a world of hurt. That’ll teach me for trying something I can’t even pronounce properly.

Originally I’d gone for a very inelegant bunch of nested if statements in the update method to determine direction which looked a mess, continually had problems with either the angle boundary or getting past zero and shall not be talked of again in polite society.

When rotating clockwise the editor shows minus numbers yet the object returned only plus in scripting (ie editor would be -20 and script would show 340). Why is that, was I doing something wrong? Anyway, flipped the platform 180 so it wouldn’t go in to negative.

After the nested if’s and failed quaternions I tried this…

	public float negAngle = 135f;
	public float pozAngle = 225f;
	public float speed = 0.5f;
	float currentAngle;
	float transformStep;
	float timePeriod;
	float from;
	float to;

	// Use this for initialization
	void Start () {
		from = transform.eulerAngles.z;
		to = pozAngle;
	}

	// Update is called once per frame
	void Update () 
	{

		currentAngle = transform.eulerAngles.z;
		timePeriod = Time.time * speed;

		//print ("time period " + timePeriod);
		print ("current angle: " + currentAngle + " neg: " + negAngle + " poz: " + pozAngle + " to: " + to);

		//swap direction if positive angle limit reached
		if (currentAngle == pozAngle) 
		{
			print ("poz angle reached");
			from = currentAngle;
			to = negAngle;
		
		//swap direction if negative limit reached
		} else if (currentAngle == negAngle) 
		{
			print ("neg angle reached");
			from = currentAngle;
			to = pozAngle;
		}

		//determine rotation angle and rotate
		transformStep = Mathf.LerpAngle (from, to, timePeriod);
		transform.eulerAngles = new Vector3 (0, 0, transformStep);
				

	}

This would rotate for the initial 180 to 225 but completely fall over trying to go back from 225 to 135. Once it got to 225 it would just alternate between 135 and 225 each frame. Why is that?

This is the solution that works well enough and lets me adjust both speed and rotation boundaries without much agro. Out of curiosity is there a way to have this work over a specific time?

	public float negAngle = 135f;
	public float pozAngle = 225f;
	public float speed;
	float currentAngle;
	float transformStep;
	float to;

	// Use this for initialization
	void Start () {
		to = pozAngle;
	}

	// Update is called once per frame
	void Update () 
	{

		currentAngle = transform.eulerAngles.z;

		//swap direction if positive boundary reached
		if ((int) currentAngle == pozAngle) 
		{
			to = negAngle;
		
		//swap direction if negative boundary reached
		} else if ((int) currentAngle == negAngle) 
		{
			to = pozAngle;
		}

		//determine rotation angle and rotate
		transformStep = Mathf.MoveTowardsAngle(currentAngle, to, speed * Time.deltaTime);
		transform.eulerAngles = new Vector3 (0, 0, transformStep);
         }

So to wrap up,

What’s the deal with the editor showing minus and the code showing only plus (ie editor -20 and code 340 for the same object)?

How comes the mathf.lerpangle produced such odd results when trying to oscillate?

Can the rotation cycle be set to a specific time frame?

update:

I found something odd happening in the ‘working’ code above. After a random number of cycles it will get stuck at one of the boundary positions unless I cast the currentAngle variable to an int (I’ve updated the code to reflect this). Why would it do this? I thought both the transform.eulerAngles.z and angle variables being floats would work ok? Both angles come back as the same number in the console so maybe it’s a type thing??

Try something like this:

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);
	
}

And for a more natural feel, change the calculation of ‘t’ to use a sine function:

float t = (Mathf.Sin (Time.time * speed * Mathf.PI * 2.0f) + 1.0f) / 2.0f;

‘speed’ will be the number of full oscillations per second.