Search Unity

rotating in the right direction

Discussion in 'Scripting' started by BigB, Jul 15, 2009.

  1. BigB

    BigB

    Joined:
    Oct 16, 2008
    Posts:
    672
    Hi,

    I have a object, facing a direction, with lets imagine a rotation of 30 (in 2D space).
    Now, if the user clicks anywhere in the screen, i wanted that the object would rotate to that direction (but making the small rotation possible).
    For example, if i click on the right of the mesh, he should rotate -15 degrees, instead of 345 degrees.
    How can i do this ?

    thanks
     
  2. shaKu

    shaKu

    Joined:
    May 19, 2009
    Posts:
    28
    Well this should be fairly simple; obviously you do not want to snap directly to that angle because this conversation would be void in that case. If you want to lerp to that angle use Mathf.LerpAngle()
     
  3. BigB

    BigB

    Joined:
    Oct 16, 2008
    Posts:
    672
    You got the wrong idea, my problem is not rotating smoothly the mesh, my problem is knowing to which direction should the rotation be made in a way to make the smaller rotation possible.
     
  4. Charles Hinshaw

    Charles Hinshaw

    Joined:
    Feb 6, 2008
    Posts:
    1,070
    Totally untested, but something like this might work. Or I might be tired and not thinking about this clearly. My assumption is that Quaternion.LookRotation is handling making the shortest rotation for you.
    Code (csharp):
    1.  
    2. transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), Time.deltaTime);
    3.