# Smooth Rotate Object While Orbiting

I am working on a 3D game where you can control an object that orbits a sphere with an on-screen virtual joystick. So far I have that part working, but the object follows the joystick angle perfectly and "snaps" into the angle, rather than smoothly interpolating to the new angle of the joystick.

I would like to redo this and have it interpolate the values. I have it very close using the following code:

angle = Mathf.Atan2(joystickY, joystickX) * Mathf.Rad2Deg;
Quaternion targetRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, currentAngle);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, TURNING_SPEED * Time.deltaTime);


This works great on one side of the sphere, but as soon as I move the object halfway around the sphere, the rotation is now mirrored and the object rotates 180 degrees until it reaches the other side again. What am I missing here?

I am basically trying to calculate the joystick angle, then apply that to the object as it orbits, but do this smoothly using slerp.

Let me know if you need any further information. Thanks!

• Changing just one angle out of a set of Euler angles can be dicey, since the meaning of that axis can depend on the other two. Can you show us an image/animation or diagram of how your object is set up, so we understand what set of orientations we can expect coming in from transform.rotation? – DMGregory Nov 14 '17 at 1:14
• @DMGregory Well it looks like I just figured this out after all! All I had to do was use transform.localRotation instead of transform.rotation... Oops haha – DRiFTy Nov 14 '17 at 1:47

joystickAngle = (Mathf.Atan2(-joystickY, -joystickX) * Mathf.Rad2Deg) + 90;