Search Unity

Rotation Confusion

Discussion in 'Scripting' started by Silentcoast, Oct 4, 2010.

  1. Silentcoast

    Silentcoast

    Joined:
    Mar 23, 2010
    Posts:
    140
    I have watched a few videos and tried to read up on the documentation then just doing some simple test and using:

    Debug.Log("Rotation X:" + transform.rotation.x);
    Debug.Log("Local Rot X:" + transform.localRotation.x);
    Debug.Log("EulerAngles X:" + transform.eulerAngles.x);
    Debug.Log("Local Euler X:" + transform.localEulerAngles.x);

    to try to figure it out.

    Here is what my issue is though. I have a cube I set its rotation to 0,0,0 and then click play and watch the debug obviously it reads 0 on everything. I then rotate it by the x axis and if everything was set to 0,0,0 then it outputs as I would expect.

    What I am trying to get though is to rotate the object on the z axis lets say by 90 degrees. Then I want to rotate it on the worlds x axis. But when I rotate with the x axis(using the rotate tool) it rotates it on the y axis.

    Surely I am just missing something but I have been banging my head on this trying to figure it out. I have a object that rotates in all directions but in my code I am just telling it ok if right/left arrow pressed rotate on z axis. If up/down is pressed rotate on x but like said above if im already rotated on z then I would really be wanting to rotate on the y axis if that makes any sense. Maybe there is a math equation I am not seeing that would help me?

    Thanks!
    Weylin
     
  2. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,853
    Use Quaternions. They don't go to 180 twice. It comes down to rotation order. I looked for a video to explain this but it disappeared. Seems you rotate 90 on the X, 90 on the Y and 90 on the Z and then reverse the order you do not get back to where you started. It has to do with rotation order. I believe X is last, Z is first and Y is second in Unity. I resorted to dropping objects into an empty game object and rotating that on the Z axis and orienting the mesh with its X axis aligned to the Z axis in order to get a simple vertical and horizontal compass working with an X axis input...which became a Z axis input to the parent.

    HTH
    BTH
     
  3. KennyW

    KennyW

    Joined:
    Aug 20, 2010
    Posts:
    128
    There should be a tutorial on hoe Unity3D handles the camera rotation in various ways, I supposed.
     
  4. Silentcoast

    Silentcoast

    Joined:
    Mar 23, 2010
    Posts:
    140
    Sorry for not responding I didnt get an email saying there was a response even though it showed im signed up on the thread. I will look at quaternions again and see if I can get any ideas on how to accomplish this. Its really driving me bonkers.
     
  5. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
  6. Silentcoast

    Silentcoast

    Joined:
    Mar 23, 2010
    Posts:
    140
    Thats what I ended up doing it was so simple I am not sure how I over looked it? I am thinking I was trying to make it much more complicated then it needed to be.

    Here is kinda what I did(its for a ship you fly around)

    Code (csharp):
    1.  
    2. bool rotating = false;
    3.         float v = 0;
    4.         float h = 0;
    5.         if (Input.GetAxis("Vertical") != 0)
    6.         {
    7.             v = verticalSpeed * Input.GetAxis("Vertical");
    8.             rotating = true;
    9.         }
    10.  
    11.         if (Input.GetAxis("Horizontal") != 0)
    12.         {
    13.             h = horizontalSpeed * Input.GetAxis("Horizontal");
    14.             rotating = true;
    15.             if (!invertLeftRight)
    16.                 h *= -1;
    17.         }
    18.  
    19.         transform.Rotate(v,0, h);
    20.  
    21.         float translation = forwardSpeed * Time.deltaTime;
    22.         transform.Translate(0,0,translation);
    23.  
    Ignore the rotating variable. I use that to auto level the ship if they are not rotating on any of the axis.