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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Need help with rotation over time

Discussion in 'Scripting' started by Chaosgod_Esper, Apr 11, 2015.

  1. Chaosgod_Esper

    Chaosgod_Esper

    Joined:
    Oct 25, 2012
    Posts:
    295
    Hi there..
    First of all, my current Code:
    Code (csharp):
    1.  
    2.     public void RotateLeft(){
    3.         if(!inRotation){
    4.             oldY = transform.rotation.y;
    5.             time = 0.0f;
    6.             RotationRate = 1.0f/RotationTime;
    7.             StartCoroutine(RotLeft());
    8.         }
    9.     }
    10.     private IEnumerator RotLeft(){
    11.         inRotation = true;
    12.         while(time < 1.0f){
    13.             nextstep.y = Mathf.Lerp(oldY, oldY+90, time);
    14.             transform.rotation = nextstep;
    15.             time += RotationRate*Time.deltaTime;
    16.             time = Mathf.Clamp(time,0,1);
    17.             yield return new WaitForEndOfFrame();
    18.         }
    19.         oldY = oldY+90;
    20.         nextstep.y = oldY;
    21.         transform.rotation = nextstep;
    22.         inRotation = false;
    23.     }
    24.  
    I made it a coroutine, cause i want this code to be started only once i hit a button - so no update methode in this class.


    My Problem now is Lerp, cause it makes a slowdown effect at the near end of the rotation - what isn't really nice..

    So my Question is:
    How to start a rotation over time, without that slowdown?

    Hoping for help :)
    ad thanks for reading.
     
  2. Xavior87

    Xavior87

    Joined:
    Feb 22, 2015
    Posts:
    23
    You can use transform.Rotate.

    public float rotSpeed;

    function Update(){
    transform.Rotate(0,rotSpeed,0);
    }

    Let me know if this works.
     
  3. Chaosgod_Esper

    Chaosgod_Esper

    Joined:
    Oct 25, 2012
    Posts:
    295
    no, since i want the object to rotate from it´s old y to a new y - and not endless ^^
     
  4. Xavior87

    Xavior87

    Joined:
    Feb 22, 2015
    Posts:
    23
    Ahhh. Wait, so you want a rotation that just stops?
     
  5. Chaosgod_Esper

    Chaosgod_Esper

    Joined:
    Oct 25, 2012
    Posts:
    295
    Found it out now by myself - sry for wasting your time :)

    Here´s my solution:
    Code (csharp):
    1.  
    2. void Start () {
    3.      nextstep = Vector3.zero;
    4.      RotationRate = 1.0f/RotationTime;
    5.      inRotation = false;
    6.    }
    7.  
    8.    public void RotateLeft(){
    9.      if(!inRotation){
    10.        oldY = transform.eulerAngles.y;
    11.        time = 0.0f;
    12.        RotationRate = 1.0f/RotationTime;
    13.        StartCoroutine(RotLeft());
    14.      }
    15.    }
    16.    private IEnumerator RotLeft(){
    17.      inRotation = true;
    18.      while(time < 1.0f){
    19.        nextstep.y = oldY + Mathf.Lerp(0, 90, time);
    20.        transform.eulerAngles = nextstep;
    21.        time += RotationRate*Time.deltaTime;
    22.        time = Mathf.Clamp(time,0,1);
    23.        yield return new WaitForEndOfFrame();
    24.      }
    25.      nextstep.y = oldY+90;
    26.      transform.eulerAngles = nextstep;
    27.      inRotation = false;
    28.    }
    29.  
     
  6. Xavior87

    Xavior87

    Joined:
    Feb 22, 2015
    Posts:
    23
    Alright.