Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Rotation problem!!! Pls help me!

Discussion in 'Scripting' started by killerz, Mar 2, 2011.

  1. killerz

    killerz

    Joined:
    Aug 28, 2010
    Posts:
    32
    Why I have this problem??? I make this script (it's a test) :

    Code (csharp):
    1.  
    2.  
    3. function Update ()
    4. {
    5.     if(Input.GetKey(KeyCode.LeftShift))
    6.     {
    7.         if(Input.GetKeyDown(KeyCode.UpArrow))
    8.         {
    9.             transform.rotation.x += 90;
    10.         }
    11.         if(Input.GetKeyDown(KeyCode.DownArrow))
    12.         {
    13.             transform.rotation.x -= 90;
    14.         }
    15.         if(Input.GetKeyDown(KeyCode.LeftArrow))
    16.         {
    17.             transform.rotation.z += 90;
    18.         }
    19.         if(Input.GetKeyDown(KeyCode.RightArrow))
    20.         {
    21.             transform.rotation.z -= 90;
    22.         }
    23.     }
    24.    
    25. }
    26.  
    When I press Shift + UpArrow (or other Arrow) the script don't add 90° to the transform rotation but make x-axis 0.(+something), y-axis = 180, z-axis = 180. I attach to the post package with the example. Pls help me it's important!
     

    Attached Files:

  2. Myx

    Myx

    Joined:
    Nov 29, 2010
    Posts:
    196
    Hello there!

    The reason it's not working is because you're trying to handle the rotation which is a quaternion as euler-angles.
    Replace your rotation lines according to this:

    Code (csharp):
    1.  
    2. transform.Rotate(new Vector3(90, 0, 0));
    3.  
     
  3. killerz

    killerz

    Joined:
    Aug 28, 2010
    Posts:
    32
    Thanks, it works but i would add to x-axis 90° not set x-axis to 90°. Can you help me?
     
  4. Ahonek

    Ahonek

    Joined:
    Jan 12, 2011
    Posts:
    140
    From the documentation:

    function Rotate (eulerAngles : Vector3, relativeTo : Space = Space.Self) : void

    Description

    Applies a rotation of eulerAngles.z degrees around the z axis, eulerAngles.x degrees around the x axis, and eulerAngles.y degrees around the y axis (in that order).


    Looks to me like it does rotate by 90, not set rotation to 90.