- Home /
              This question was 
             closed Sep 20, 2017 at 08:22 AM by 
             tmalhassan for the following reason: 
             
 
            The question is answered, right answer was accepted
 
               Question by 
               tmalhassan · Sep 20, 2017 at 07:30 AM · 
                c#rotationcoroutinewaitforsecondsrotatetowards  
              
 
              Rotate an object using Quaternion.RotateTowards() inside a Coroutine?
Hello, i am trying to rotate an object inside a Coroutine using Quaternion.RotateTowards(). But its not rotating smoothly like it does in Update(). instead, it snaps to the target rotation. What is wrong? This is my script:
 public class SpinWheelScript : MonoBehaviour {
 public GameObject[] rewards;
 public GameObject[] pointerDir;
 public GameObject[] ringDir;
 public GameObject pointer;
 public GameObject ring;
 private Quaternion pointerTargetRotation;
 private Quaternion ringTargetRotation;
 private bool spinIsEnabled = true;
 private bool isSpinning = false;
 private float pSpeed;
 private float rSpeed;
 private float pointerRotateFloat;
 private float ringRotateFloat;
 private int rewardEnabler = 0;
 private int randomReward;
 private int pRandomDir;
 private int rRandomDir;
 private int plastRandomDir = 0;
 private int rlastRandomDir = 0;
 private void Update()
 {
     if (spinIsEnabled == false)
     {
         // Do Nothing
     }
     else if (spinIsEnabled == true && isSpinning == false)
     {
         // Select reward
         if (rewardEnabler == 0)
         {
             RewardSelector();
             rewardEnabler = 1;
         }
         StartCoroutine(Spin());
     }
 }
 IEnumerator Spin()
 {
     isSpinning = true;
     pSpeed = 0;
     rSpeed = 0;
     // Speed increasing spin for 5 seconds
     PointerRandomDirection();
     RingRandomDirection();
     while (true)
     {
         pointerRotateFloat = (((pRandomDir + 1) * 60) - 30) - pointer.transform.rotation.z;
         ringRotateFloat = (((rRandomDir + 1) * 60) - 30) - ring.transform.rotation.z;
         pSpeed = (pointerRotateFloat / 2);
         rSpeed = (ringRotateFloat / 2);
         pointerTargetRotation = Quaternion.Euler(new Vector3(0, 0, pointerRotateFloat));
         pointer.transform.rotation = Quaternion.RotateTowards(pointer.transform.rotation, pointerTargetRotation, pSpeed * Time.deltaTime);
         ringTargetRotation = Quaternion.Euler(new Vector3(0, 0, ringRotateFloat));
         ring.transform.rotation = Quaternion.RotateTowards(ring.transform.rotation, ringTargetRotation, rSpeed * Time.deltaTime);
         if ((pointer.transform.rotation == pointerTargetRotation) && (ring.transform.rotation == ringTargetRotation))
             break;
     }
     yield return new WaitForSeconds(1);
     Debug.Log("Done!");
 }
 private int RewardSelector()
 {
     randomReward = Random.Range(0, rewards.Length);
     return randomReward;
 }
 private int PointerRandomDirection()
 {
     int pRandomDir = plastRandomDir;
     if (pointerDir.Length <= 1)
         return 0;
     while (pRandomDir == plastRandomDir)
     {
         pRandomDir = Random.Range(0, pointerDir.Length);
     }
     plastRandomDir = pRandomDir;
     return pRandomDir;
 }
 private int RingRandomDirection()
 {
     int rRandomDir = rlastRandomDir;
     if (ringDir.Length <= 1)
         return 0;
     while (rRandomDir == rlastRandomDir)
     {
         rRandomDir = Random.Range(0, ringDir.Length);
     }
     rlastRandomDir = rRandomDir;
     return rRandomDir;
 }
 }
Thanks in advance :)
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by tmalhassan · Sep 20, 2017 at 08:22 AM
Turns out the problem was that i wasn't yielding a Wait-Instruction inside the coroutine.
Just had to add this line at the end of the While loop: yield return null;.
This will tell the coroutine to delay the execution of the next line of code to the next frame.
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Doing WaitForSeconds in C# 0 Answers
Coroutine wait is not working 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                