Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Rotating by (-1,-1,-1) and then (1,1,1) results in movement???

Discussion in 'Scripting' started by Kyrieru, Aug 29, 2020.

  1. Kyrieru

    Kyrieru

    Joined:
    Mar 2, 2015
    Posts:
    42
    If I do this,

    transform.Rotate(1,1,1);
    transform.Rotate(-1,-1,-1);

    The object rotates on every axis by a tiny amount.
    What causes this?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Probably accumulated floating point error.

    If you really wanna have your mind blown, try mixing and matching rotating and parenting and deparenting each frame... you'll make objects swell or shrink or do all kinds of weird things as their transform matrices get all out of normal.

    Always best to construct the rotation you want explicitly with Quaternion.Euler(x,y,z) each frame, then track the x,y,z yourself.
     
  3. Kyrieru

    Kyrieru

    Joined:
    Mar 2, 2015
    Posts:
    42
    Well, seems like this works then.

    Code (CSharp):
    1. var temp_rot = Quaternion.Euler(1f, 1f, 1f);
    2. transform.rotation = transform.rotation * temp_rot;
    3. transform.rotation = transform.rotation * Quaternion.Inverse(temp_rot);
     
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,921
    It works on your local rotation. The second one spins you (-1,-1,-1) on your _new_ xyz axis. Try it with larger numbers and you'll see move and more of a difference.
     
    Antistone likes this.