Search Unity

Rotate/Lerp Gameobject 90 degrees?

Discussion in 'Scripting' started by turbosheep, Mar 27, 2015.

  1. turbosheep

    turbosheep

    Joined:
    Jul 19, 2013
    Posts:
    25
    Alright, I'm new to this stuff and the internet is confusing me pretty hard. You have Euler Angles, Quaternions; one uses degrees, the other radians, you should use lerp, slerp, lookat, you can't set pivots manually without parent objects... etc.

    What I want to do is gradually rotate a gameobject around its pivot point 90 degrees clockwise in Z whenever I press a key. It's like a Tetris block.

    I'm really looking for a short, simple explanation so I understand what I'm doing and to avoid further confusion. Help would really be appreciated! Thanks!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    See the Rotation function here.

    --Eric
     
  3. turbosheep

    turbosheep

    Joined:
    Jul 19, 2013
    Posts:
    25
    Thank you! I tried using this in the update() function:

    Code (CSharp):
    1. //Rotate pathblock 90 degrees clock-wise
    2.     void RotatePathblock()
    3.     {
    4.         //pathblock.transform.Rotate(0,0,90f);
    5.         //Pivot has to be the same as the middle of the block that the mouse pointer is on... since all blocks are the same size, this pivot point is min. cell+width/2 in x, and minus in y.
    6.         Vector3 degrees = new Vector3(0,0,90f);
    7.         Quaternion start = pathblock.transform.rotation;
    8.         Quaternion end = start * Quaternion.Euler(degrees);
    9.         float duration = 500f;
    10.         float rate = 1.0f/duration;
    11.         float t = 0.0f;
    12.  
    13.         while(t < 1.0)
    14.         {
    15.             t += Time.deltaTime * rate;
    16.             pathblock.transform.rotation = Quaternion.Slerp(start, end, t);
    17.         }
    18.     }
    When I press a key, that function executes and it rotates fine. But, it doesn't interpolate... as in, it doesn't move 90 degrees over time. I tried changing the duration from 3f to 500f, to no avail. Could you explain what might be going wrong here?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That's because you're running the entire loop in one frame. You should use coroutines like in the link.

    --Eric
     
  5. turbosheep

    turbosheep

    Joined:
    Jul 19, 2013
    Posts:
    25
    I thought the slerp/lerp function would take care of dealing with the rotation over multiple frames as long as I didn't put it into Start() or something. This is the first time I've heard about using Lerp in a co-routine... is everyone doing it like this? As I said earlier, I'd love to actually learn about what I'm doing.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Lerp is just a really basic math function; it doesn't do anything over time. If you want to use it for animation you need to call it repeatedly every frame while changing the third parameter (typically advancing it from 0 to 1). It's just easier to use a coroutine then to set up (what typically becomes) convoluted logic in Update.

    --Eric