Search Unity

Rotation routine

Discussion in 'Scripting' started by Omar Rojo, Dec 10, 2009.

  1. Omar Rojo

    Omar Rojo

    Joined:
    Jan 4, 2007
    Posts:
    494
    Ok,

    I already have a headache trying to do this, i kind of managed to make it work but i need some serious math class about rotations.

    What i need is the following..

    Facts:

    1. I have two gameObjects
    2. Both move
    3. Both can be facing anywhere

    Problem:

    I need a script that rotate one gameObject until it faces the other, Quaternion.Slerp seems to solve the problem except for the fact that i need a constant rotation (i.e. 5 degrees per second). With Quaternion.Slerp I'm getting a fraction between the two given rotations that decreases as they came closer.

    So, i need another function or algorithm to do this, or keep using Quaternion.Slerp but have a dynamic factor for this function ?

    Any ideas ? Couldn't find anything in the forum.

    .org
     
  2. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    Omar,

    I am not 100% sure if this is what you are asking for but in calling Quaternion.Slerp, there is a 3rd property that deals with the advancing of the rotation over time.

    Code (csharp):
    1. Quaternion.Slerp (from.rotation, to.rotation, Time.time * speed);
    Also, in the Mathf class there is LerpAngle that could prove useful.

    HTH.
     
  3. Omar Rojo

    Omar Rojo

    Joined:
    Jan 4, 2007
    Posts:
    494
    littlelingo,

    Yeah, that would work if from.rotation and to.rotation were fixed, but in my problem, those values varies each frame. If your variable speed is the same always, then the object will rotate at different speeds when rotating from 0 to 45 and rotating from 0 to 180.

    Am I wrong ?

    .org
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    If you have the starting and ending rotations, you can find the angle between them using Quaternion.Angle. If you want a particular rate of rotation (5º per second, say) then divide the angle by this rate to get the number of seconds the rotation should take. Then, use Quaternion.Slerp and vary the interpolation value from zero to one over that time period.
     
  5. Omar Rojo

    Omar Rojo

    Joined:
    Jan 4, 2007
    Posts:
    494
    I think I understand, I'll try a little, thanks andeeee

    .org