Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

rotating in the right direction

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

  1. BigB

    BigB

    Joined:
    Oct 16, 2008
    Posts:
    658
    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:
    658
    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.  
     
unityunity