I have a code that is supposed to rotate an object towards another object on one axis, the code works the object is forced to turn more than 180 degrees, as if it can’t fully turn around which is my only problem. I really want to keep the interpolation so that rotation is smooth. There is more to the code but here is the part where all the magic happens.
rotation = Quaternion.LookRotation(transform.position - target.transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, rotSpeed * Time.deltaTime);
var eulerAngles = transform.rotation.eulerAngles;
eulerAngles.y = startRot.eulerAngles.y;
eulerAngles.z = startRot.eulerAngles.z;
// Set the altered rotation back
transform.rotation = Quaternion.Euler(eulerAngles);
So what I am doing is creating a look rotation then slerping the object between current rotation and desired rotation, there is no problem there, then I get the euler angles of that altered rotation, then the y and z components of the euler angles are set to the same as the rotation at the start of the scene (the default rotation), therefore the other two axes will be reset (I know the documentation says don’t alter the axes separately but this does’t seem to be the cause of the problem). Then set the rotation back, but only for the y and z axes while the x axis remains the way it was when it was “slerped”. Again the problem is that the object stops rotating once the target moves beyond the starting rotation (view), anyone know how to fix this?