Rotate object to target position

I have an object in my Unity scene which receives rotation values from another script. These rotation values are angles for rotating the object in the world space (and not around object’s local axes).

The object already has an orientation in World Space (say 50deg pitch, 50 deg yaw) and we can take this current orientation to be S. When the object receives rotation values like (0,180,0), it has rotate from it’s current orientation by 180 degrees around Y axis (in world space).
When it receives the next rotation value (0,270,0), it has to rotate from current orientation to the orientation - S + 270(around Y axis in world space)

I tried to do Quaternion.Slerp but Slerp always takes the shortest path because of which this doesn’t work correctly. Here’s what my code looks like right now:

IEnumerator RotateObject(Transform thisTransform, Vector3 endDegrees, float time){
		Quaternion startQuat = transform.rotation;
		Quaternion endQuat = transform.rotation * Quaternion.Euler (endDegrees);

		float rate = 1.0f / time;
		float t = 0.0f;
		while (t < 1.0) {
			t += Time.deltaTime * rate;
			transform.rotation = Quaternion.Slerp(startQuat, endQuat, t);
			yield return 0;
		}
	}

I am not sure how I can interpolate the eulerAngles directly, because it is not recommended to read the eulerAngles values (should be write-only), but I’d need to read the angles to get the current orientation and add/subtract rotation values to it.

I just read your comment on my old question. In particular, you will keep a class variable…something like myAngles:

private Vector3 myAngles;

I don’t know how you have your objects setup. If they always start at (0,0,0) rotation, then you can just assign Vector3.zero; Or you can make ‘myAngles’ public and then initialize eulerAngles to a set value. Or usually you can get away reading eulerAngles once in Start() or Awake().

After that you will always do your Lerp() to your variable. Here is a untested rewrite of your code to give you the idea:

IEnumerator RotateObject(Transform thisTransform, Vector3 deltaDegrees, float time){
   Vector3 start = myAngles;
   Vector3 end = start + deltaDegrees;
   float rate = 1.0f / time;
   float t = 0.0f;
   while (t < 1.0) {
     t += Time.deltaTime * rate;
     myAngles = Vector3.Lerp(start, end, t);
     transform.eulerAngles = myAngles;
     yield return 0;
   }
}