Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Smooth 90 degree turn

Discussion in 'Scripting' started by shawnpigott, Dec 20, 2006.

  1. shawnpigott

    shawnpigott

    Joined:
    Sep 28, 2005
    Posts:
    262
    I modified a script that Joachim Ante wrote a while ago to try to create a smooth 90 turn but it doesn't want to work.

    The idea is that you hit the 'b' key to turn 90 degrees to the right. I have it working another way but it is frame dependent so I wanted to improve upon it.

    Code (csharp):
    1.  
    2.  
    3. var startrotation = 270;
    4.  
    5. function Update () {
    6.  
    7.     if (Input. GetKeyDown ("b")) {
    8.         StartCoroutine("Slerp", 1.0);
    9.     }
    10. }
    11.  
    12. function Slerp (time : float)
    13. {
    14.   var curroation : Vector3;
    15.   var newrotation : Vector3;
    16.   var originalTime = time;  
    17.      
    18.     curroation = Vector3(0, startrotation, 0);
    19.     newrotation = Vector3(0,(startrotation + 90),0);
    20.  
    21.     startrotation = startrotation + 90;
    22.  
    23.    while (time > 0.0)
    24.    {
    25.       time -= Time.deltaTime;
    26.       transform.rotation = Quaternion.Slerp(curroation, newrotation, time / originalTime);
    27.       yield;
    28.    }
    29. }
    30.  
    Thoughts?
     
  2. shawnpigott

    shawnpigott

    Joined:
    Sep 28, 2005
    Posts:
    262
    I did some digging and found my error.

    Code (csharp):
    1.  
    2. var startrotation = 270;
    3.  
    4. function Update () {
    5.  
    6.     if (Input. GetKeyDown ("b")) {
    7.         StartCoroutine("Slerp", 1.0);
    8.     }
    9. }
    10.  
    11. function Slerp (time : float)
    12. {
    13.     var curroation = Quaternion.Euler(0, startrotation, 0);
    14.     var newrotation = Quaternion.Euler(0, (startrotation + 90), 0);
    15.     var originalTime = time;  
    16.  
    17.     startrotation = startrotation + 90;
    18.  
    19.    while (time > 0.0)
    20.    {
    21.       time -= Time.deltaTime;
    22.       transform.rotation = Quaternion.Slerp(newrotation, curroation, time / originalTime);
    23.       yield;
    24.    }
    25. }
    26.