Search Unity

Mathf.LerpAngle - rotate a block via input

Discussion in 'Scripting' started by adamprocter, Mar 31, 2007.

  1. adamprocter

    adamprocter

    Joined:
    Jan 18, 2006
    Posts:
    22
    I am trying to get a block to smooth rotate from one angle to another

    All I seem to get at the moment it the block jumping into positions and not the smooth rotate I was hoping for ?

    here is my code so far..

    Code (csharp):
    1. var current_angle = 0;
    2. var desired_angle = 0;
    3.  
    4. function Update () {
    5.  
    6.  if (Input.GetKey ("up")) {
    7.         desired_angle = 0;
    8.     }
    9.     else if (Input.GetKey ("right")) {
    10.         desired_angle = 90;
    11.     }
    12.     else if (Input.GetKey ("down")) {
    13.         desired_angle = 180;
    14.     }
    15.      else if (Input.GetKey ("left")) {
    16.         desired_angle = 270;
    17.      }
    18.  
    19. var angle = Mathf.LerpAngle(current_angle, desired_angle, Time.time);
    20. transform.eulerAngles = Vector3(0, angle, 0);
    21.  
    22.  
    23. }
     
  2. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    Try using Time.deltaTime instead of Time.time?

    EDIT: Also, it looks like current_angle is always zero, which means you will always be lerping from 0. I doubt this is what you want? Maybe try this:

    Code (csharp):
    1. var current_angle = 0;
    2. var desired_angle = 0;
    3.  
    4. function Update () {
    5.  
    6.  if (Input.GetKey ("up")) {
    7.         desired_angle = 0;
    8.     }
    9.     else if (Input.GetKey ("right")) {
    10.         desired_angle = 90;
    11.     }
    12.     else if (Input.GetKey ("down")) {
    13.         desired_angle = 180;
    14.     }
    15.      else if (Input.GetKey ("left")) {
    16.         desired_angle = 270;
    17.      }
    18.  
    19. current_angle = Mathf.LerpAngle(current_angle, desired_angle, Time.deltaTime);
    20. transform.eulerAngles = Vector3(0, current_angle, 0);
    21.  
    22.  
    23. }
     
  3. adamprocter

    adamprocter

    Joined:
    Jan 18, 2006
    Posts:
    22
    nice - it now rotates but only when i press down and seems to only get to about 88 degrees before it stops , pressing up gets me back to 0 degrees and so does pressing left at that point .... thanks
     
  4. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    Duh. That's what I get for not looking at the docs.

    I tested this one and it works for me:

    Code (csharp):
    1. var desired_angle = 0;
    2. var speed = 1.0;
    3.  
    4. function Update () {
    5.  
    6.  if (Input.GetKey ("up")) {
    7.         desired_angle = 0;
    8.     }
    9.     else if (Input.GetKey ("right")) {
    10.         desired_angle = 90;
    11.     }
    12.     else if (Input.GetKey ("down")) {
    13.         desired_angle = 180;
    14.     }
    15.      else if (Input.GetKey ("left")) {
    16.         desired_angle = 270;
    17.      }
    18.      
    19.  var target_rotation = transform.rotation.Euler(0, desired_angle, 0);
    20.  transform.rotation = Quaternion.Slerp( transform.rotation, target_rotation, Time.deltaTime * speed );
    21. }
    Sorry about that!
     
  5. adamprocter

    adamprocter

    Joined:
    Jan 18, 2006
    Posts:
    22
    Thank you - someone mentioned slerp at some point, I was getting very confused - if I ever manage to put anything more polished together I will post it.

    adam