Search Unity

stop rotating towards

Discussion in 'Scripting' started by BlueMumm, Mar 5, 2008.

  1. BlueMumm

    BlueMumm

    Joined:
    Feb 7, 2008
    Posts:
    43
    I been trying to get this function to stop rotating when the model does not have to rotate anymore. I have tried checking the models current rotation against the look rotation to see if they are the same. I have also tried comparing the y value of the quaternions because thats what I am rotating on. Any help would be greatly appreciated.

    Code (csharp):
    1. function RotateTowards (position : Vector3) : boolean {
    2.     var direction = position - transform.position;
    3.     direction.y = 0;
    4.    
    5.     if( transform. rotation ==Quaternion.LookRotation(direction))
    6.         return  true;
    7.  
    8.     // Rotate towards the target
    9.     transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
    10.     //transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);
    11.    
    12.     return false;
    13.  
    14. }
     
  2. CoherentInk

    CoherentInk

    Joined:
    Jul 16, 2006
    Posts:
    216
    I tried this before and also had no luck. I believe it doesn't work because of the lack of precision in floating point numbers. I ultimately decided to change my code to Slerp over a fixed amount of time, so I could be sure when the rotation had finished.
     
  3. BlueMumm

    BlueMumm

    Joined:
    Feb 7, 2008
    Posts:
    43
    Yeah I think it is something to do with floats I even tried adding a threshold. Sadly Slerping over time will not work very well for my game. I guess I will keep trying.