I am using Quaternion.Slerp to rotate an object from -30 to 30 (rotating fine). But i am unable to make the reverse rotation from 30 to -30. Please help me how to do that reverse rotation?
I am working in a demo game project in which, there is a tower that shot bullets and can rotate from -30 to +30 then +30 to -30 and continue this rotation forever. I am having problem with roatation. I am using Quaternion.Slerp to rotate it from -30 to 30 (rotating fine). But i am unable to make the reverse rotation from +30 to -30. Please help me how to do that reverse rotation. I am new to Unity. Thank you! //Here is my code
 using UnityEngine;
 using System.Collections;
 
 public class RotateTo : MonoBehaviour
 {
 
     private Quaternion rotationFrom;
     private Quaternion rotationTo;
 
     void Start () {
         
 
         rotationFrom = Quaternion.Euler(0.0f, -30, 0.0f);
         rotationTo = Quaternion.Euler(0.0f, 30, 0.0f);
         
     }
     // Update is called once per frame
     void Update ()
     {
         transform.rotation = Quaternion.Slerp(rotationFrom, rotationTo, 0.5f * Time.time);
     }
 }
 
 
 
              You could try making the Quaternion.Euler a new Vector3. Otherwise it looks fine. The Quaternion.Slerp API shows that you have the time and speed flip flopped but I am not sure if that would make a difference.
Answer by Mike-Geig · Apr 06, 2016 at 03:24 AM
You're close, you just need to flip flop the numbers when you reach your goal:
 private Quaternion rotationFrom;
 private Quaternion rotationTo;
 
 float ellapsedTime = 0f;
 
 void Start () {
 
 
     rotationFrom = Quaternion.Euler(0.0f, -30, 0.0f);
     rotationTo = Quaternion.Euler(0.0f, 30, 0.0f);
 
 }
 // Update is called once per frame
 void Update ()
 {
     ellapsedTime += Time.deltaTime;
     transform.rotation = Quaternion.Slerp(rotationFrom, rotationTo, 0.5f * ellapsedTime);
 
     //if we get within a 10th of a degree...
     if (Quaternion.Angle (transform.rotation, rotationTo) < .1f) 
     {
         //Start our time over
         ellapsedTime = 0f;
 
         //Flip flop our values
         Quaternion temp = rotationTo;
         rotationTo = rotationFrom;
         rotationFrom = temp;
     }
 }
 
              Thank you $$anonymous$$r. $$anonymous$$ike. This is perfectly what i want.
Your answer
 
             Follow this Question
Related Questions
Is it possible to rotate an object around its z-axis without changing its x-axis and y-axis? 1 Answer
Object rotation lerp with acceleration 0 Answers
Slerp rotation issue when calling a lot 0 Answers
Movement and Direction script bugging out 1 Answer
How to set the time value in Lerp, Slerp, RotateTowards? 0 Answers