Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Differnce between many small rotations and 1 large rotation.

Discussion in 'Scripting' started by Ryuuguu, May 10, 2007.

  1. Ryuuguu

    Ryuuguu

    Joined:
    Apr 14, 2007
    Posts:
    391
    I am rotating an object through 180 degrees along a diagonal-
    trans = Vector3(1,0,1).normalized
    when I do it in one step
    transform.Rotate(trans * 180,Space.World)
    I get a different answer than if I do it in many small (1 degree steps)
    transform.Rotate(trans * 1,Space.World)
    executed in a loop 180 times.

    in one step I get euler angles (52.7,180,307.3)
    in many steps I get (0.4,90,180.4) (little rounding erros I guess)
    (0,90,180) is what I expected.

    I don't have a good grasp or Quaternion so I don't understand what is happening. Can anyone explain to me why I get a different answer when I rotate in a large step. This only happens in my tests when the rotations vector has more than one non-zero value and I normalize it.

    below is code I attached to a capsule to test including rotating the capsule and printing the results to the console.


    Code (csharp):
    1. import UnityEngine
    2.  
    3. class Test_function (MonoBehaviour):
    4.    
    5.     def OnMouseDown ():    
    6.         trans =Vector3(1,0,1).normalized
    7.         angle = 180
    8.         progress = 0       
    9.         temp = transform.rotation
    10.         transform.Rotate(trans * angle,Space.World)
    11.         finalRotation = transform.rotation
    12.         Debug.Log("fre:${transform.eulerAngles}")
    13.         transform.rotation =temp
    14.         while  (progress <angle ) :
    15.                 tDelta = 5
    16.                 transform.Rotate(trans * tDelta,Space.World)
    17.                 progress += tDelta
    18.         //transform.rotation = finalRotation
    19.         Debug.Log("prog:${progress}")
    20.         Debug.Log("fr::${finalRotation}")
    21.         Debug.Log("ar:${transform.rotation}")
    22.         Debug.Log("are:${transform.eulerAngles}")
    23.  
    cheers,
    Grant
     
  2. Omar Rojo

    Omar Rojo

    Joined:
    Jan 4, 2007
    Posts:
    494
    When you use

    Code (csharp):
    1. Transform.Rotate (Vector3, Space);
    you are specifying euler angles. To rotate around an axis you should use

    Code (csharp):
    1. Transform.Rotate (Vector3, float, Space)
    so, don't multiply but separate with a comma.

    Code (csharp):
    1. transform.Rotate(trans, angle, Space.World)
     
  3. Ryuuguu

    Ryuuguu

    Joined:
    Apr 14, 2007
    Posts:
    391
    Thank you very much (it was driving me nuts).

    I rereading the Doc I see it does explain that :oops:

    cheers,
    Grant
     
  4. Omar Rojo

    Omar Rojo

    Joined:
    Jan 4, 2007
    Posts:
    494
    It happens :oops:

    ORG