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.