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

rotation issue

Discussion in 'Scripting' started by soorena, Apr 24, 2014.

  1. soorena

    soorena

    Joined:
    Mar 5, 2014
    Posts:
    35
    hi, i have a script for my gun rotation however while it only take mouse's X and Y axises and only should rotate in x and y yet for some reasons it also rotate in z as well when rotating in both x and y, is there any way that i could do something to avoid my gun rotate in z?

    here is the rotation part of my script:
    Code (csharp):
    1.  
    2. // MX=mouse x axis
    3.         RoX = Input.GetAxis("MX") * (Time.fixedDeltaTime * RoSp);
    4.         transform.Rotate(-RoX, 0, 0);
    5. //MY= mouse y axis
    6.         RoY = Input.GetAxis("MY") * (Time.fixedDeltaTime * RoSp);
    7.         transform.Rotate(0, RoY, 0);
    8.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Quaternions are strange beasts, and it's possible to rotate in every axis by only rotate on two if you're not controlling it more rigidly. (For example: Face forward; yaw left 90 degrees; pitch down 90; yaw right 90; and suddenly, you've rolled 90 degrees left, without ever rolling!)

    The best solution in my experience is to store the X and Y rotations separately in your script, and start the rotation fresh each frame rather than using transform.Rotate.
    Code (csharp):
    1.  
    2. transform.rotation = Quaternion.Euler(accumulatedX, accumulatedY, 0);
    3.  
     
  3. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Y in Unity is depth, not vertical, when dealing with 3d space. You want to have your mouse input y apply to the z axis of your camera, not y.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    No, it's vertical. X = left/right, Y = up/down, Z = forward/back.

    --Eric
     
  5. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Whoops, sorry, brain still booting up.