Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Rotate/spin object 360 degrees over set time in coroutine

Discussion in 'Scripting' started by jaredchong83, Apr 2, 2016.

  1. jaredchong83

    jaredchong83

    Joined:
    Mar 23, 2016
    Posts:
    5
    Hi there, I have been working on getting an object to rotate 360 degrees within a set time using a coroutine.

    I am attempting to rotate the current Y transform 360 degrees, but seem to be encountering some issues that I thought were related to when the transform.eulerAngles.y goes over 360 and needs to be set back to 0, but it seems like that wasn't the root of my problems. As it is the object rotates many times around its y axis, rather than just a single time, and also doesn't finish where the y axis was when the rotation started. Any advice for how to approach this would be appreciated! Below is the code showing what I have attempted so far.

    Code (CSharp):
    1.     IEnumerator Rotate(float duration)
    2.     {
    3.         float i = 0;
    4.         float yRotation;
    5.         while (i < 1)
    6.         {
    7.             i += Time.deltaTime / duration;
    8.             yRotation = Mathf.Lerp(transform.eulerAngles.y, transform.eulerAngles.y + 360, i);
    9.             if (transform.eulerAngles.y > 360)
    10.             {
    11.                 transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y - 360, transform.eulerAngles.z);
    12.             }
    13.             transform.eulerAngles = new Vector3(transform.eulerAngles.x, yRotation, transform.eulerAngles.z);
    14.             yield return null;
    15.         }
    16.     }
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    I think the problem is with the end value of your lerp. You increase transform.eulerAngles.y and add 360 to it, so the lerp will never reach its end. Try just 360 as the end value.
     
    jaredchong83 likes this.
  3. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    I agree with @Laperen
    I would implement your coroutine as follow:
    Code (CSharp):
    1.     IEnumerator Rotate(float duration)
    2.     {
    3.         float startRotation = transform.eulerAngles.y;
    4.         float endRotation = startRotation + 360.0f;
    5.         float t = 0.0f;
    6.         while ( t  < duration )
    7.         {
    8.             t += Time.deltaTime;
    9.             float yRotation = Mathf.Lerp(startRotation, endRotation, t / duration) % 360.0f;
    10.             transform.eulerAngles = new Vector3(transform.eulerAngles.x, yRotation, transform.eulerAngles.z);
    11.             yield return null;
    12.         }
    13.     }
     
    Last edited: Apr 2, 2016
  4. jaredchong83

    jaredchong83

    Joined:
    Mar 23, 2016
    Posts:
    5
    Thanks guys, I understand what I did wrong now!
     
  5. rhysp

    rhysp

    Joined:
    Jun 14, 2016
    Posts:
    7
    @ericbegue's solution will work well, but make sure the set the transform value to the endRotation value following the while() loop, otherwise the final position won't precisely be where you want it (there would be a minor discrepancy based on the delta time that the final loop runs)
     
  6. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    @rhysp
    It's ok. In the last iteration, t will be greater than duration, therefore the last parameter of lerp will be more than one. That will make yRotation to have the same value as endRotation.
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Read this anytime you're tempted to read a value from Euler angles and apply any logic to it. Euler angles are terrible - literally the only thing they're good for is letting a human type in rotation values.

    Even if it works now, it's easy to imagine a tiny change in the future making this break, and giving you weird rotation bugs. Honestly, 95% of the rotation-related questions on this forum can be answered with a variation of "don't use Euler angles".

    In this case, I would suggest using .AngleAxis and not use Euler angles at all. It might look like:
    Code (csharp):
    1. IEnumerator Rotate(float duration) {
    2. Quaternion startRot = transform.rotation;
    3.  float t = 0.0f;
    4.         while ( t  < duration )
    5.         {
    6.             t += Time.deltaTime;
    7.             transform.rotation = startRot * Quaternion.AngleAxis(t / duration * 360f, Vector3.right); //or transform.right if you want it to be locally based
    8.             yield return null;
    9.         }
    10. transform.rotation = startRot;
    11. }
     
  8. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508

    Nice, thanks for posting this.
    With some very slight changes, you could make a variation that accepts N degrees. and instead of setting transform rotation to the original rotation at the end you would just set it to the ending rotation.
     
    tokar_dev likes this.
  9. NeverTrustShadows

    NeverTrustShadows

    Joined:
    Mar 15, 2020
    Posts:
    13
    This does not work, transform.eulerAngles changes everytime once you set it, because you use transform.eulerAngles.x,y values inside while, the resulting rotation is messed up.

    The correct version should be;

    Code (CSharp):
    1.     IEnumerator Rotate(float duration)
    2.     {
    3.         Vector3 startRotation = transform.eulerAngles;
    4.         float endRotation = startRotation.z + 360.0f;
    5.         float t = 0.0f;
    6.         while ( t  < duration )
    7.         {
    8.             t += Time.deltaTime;
    9.             float yRotation = Mathf.Lerp(startRotation.z, endRotation, t / duration) % 360.0f;
    10.             transform.eulerAngles = new Vector3(startRotation.x, yRotation, startRotation.z);
    11.             yield return null;
    12.         }
    13.     }
     
    danielbarraud07 likes this.
  10. ramkhilnani

    ramkhilnani

    Joined:
    Aug 20, 2019
    Posts:
    1
    I've tried the answers listed on this thread.
    My issue is the coroutine doesn't end when t > duration but instead t keeps bouncing back to 0.
    So the rotation never ends. Everytime t reaches more than 0 it loops back to 0 and i get an infinite rotation.

    Any ideas?
     
  11. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Not without seeing your code no
     
    Joe-Censored likes this.
  12. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you're talking about the code above your comment, are you sure you just aren't starting another coroutine after the first one ends?
     
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Well Mr Necro-Posting A Five-Year-Old Thread, it is probably best to start your own thread as forum rules dictate.

    When you do, here is how to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Also, consider this:


    You can also use this approach: To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.

    If you are running a mobile device you can also see the console output. Google for how.