Negative Eular angles.

I have a turret that I am rotating using transform.eulerAngles = Vector3.Lerp(…);
I am using a timer that tells it when to rotate the opposite way.

This is the Rotate Code.

mainAssembly.eulerAngles = Vector3.Lerp(mainAssembly.eulerAngles, new Vector3(0, targetRotation, 0), Time.deltaTime);

		// Use a timer to pan the turret backwards and forwards.
		timer += Time.deltaTime;
		if (timer >= 10){
			targetRotation = 330;
			timer = 0;
		}
		else if (timer >= 5){
			targetRotation = 30;
		}

Here is a picture to better illustrate what I am trying to achieve.
i41.tinypic.com/2uoqnh2.jpg

Sorry if this is not clear enough. Let me know i’ll try explain it better.

Thanks

  • Matt

The issue is that eulerAngles is a reflection of Transform.rotation and does not preserved any specific euler settings. For example, you might set the rotation to (180,0,0) only to read it back immediately and find the settings are instead (0,180,180)…the same physical rotation, but different eulerAngles. So if you set the the rotation to -30 it appears you are getting a rotation of 330 instead. One solution is to maintain your own Vector3 and assign it every frame (i.e. treat eulerAngles as ‘write-only’). v3Current is defined at the top of the class and is originally set to the starting rotation.

v3Current = Vector3.Lerp(v3Current, new Vector3(0, targetRotation, 0), Time.deltaTime);
mainAssembly.eulerAngles = v3Current;

Note target rotation would then be -30 and 30 (don’t use 330).

There are a number of other ways to get the above motion. Here is another:

public class BackForth : MonoBehaviour {

	public float rotationAngle = 30.0f;
	public float speed = 0.25f;
		
	void Update() {
		transform.eulerAngles = new Vector3(0,rotationAngle * Mathf.Sin(Time.time * speed));		
	}
}