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

Character Rotating on Z Axis when not supposed too

Discussion in 'Scripting' started by treyacarey, Aug 20, 2020.

  1. treyacarey

    treyacarey

    Joined:
    Aug 20, 2020
    Posts:
    9
    Hello all,
    I have an issue where when moving my player and rotating he seems to be rotating along the Z axis as well when it should be staying at 0. My end goal is basically to make a free cam where the player can fly around the game.

    Here is all of my movement code, and the Hierarchy tree in which I have all of my objects

    Sample Scene
    >Directional Light
    > Ground (Cube with scale at 35, 1, 35)
    > Player
    >Camera (With the code below attached)​

    Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FreeCam : MonoBehaviour {
    6.  
    7.     public Camera playerCamera;
    8.  
    9.     float mouseInputX;
    10.     float mouseInputY;
    11.     private bool isPanning;
    12.  
    13.     //Control Modifiers
    14.     public float flySpeed;
    15.     public float panSpeed;
    16.     public float lookSens;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start() {
    20.  
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update() {
    25.  
    26.         //Right Movement
    27.         if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) {
    28.             transform.Translate(flySpeed * Time.deltaTime, 0, 0);
    29.         }
    30.  
    31.         //Left Movement
    32.         if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) {
    33.             transform.Translate(-flySpeed * Time.deltaTime, 0, 0);
    34.         }
    35.  
    36.         //Forward Movement
    37.         if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) {
    38.             transform.Translate(0, 0, flySpeed * Time.deltaTime);
    39.         }
    40.  
    41.         //Backwards Movement
    42.         if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) {
    43.             transform.Translate(0, 0, -flySpeed * Time.deltaTime);
    44.         }//END OF MOVEMENT
    45.  
    46.         //Mouse Panning
    47.         if (Input.GetMouseButton(1)) { //If the right mouse button is held
    48.             mouseInputX = Input.GetAxis("Mouse X");
    49.             mouseInputY = Input.GetAxis("Mouse Y");
    50.             transform.Rotate(-mouseInputY * lookSens, mouseInputX * lookSens, 0);
    51.         }//END IF STATEMENT
    52.     }
    53. }
    54.  
    I am very new to Unity, and any help would be greatly appriciated!
     
  2. Gramms66

    Gramms66

    Joined:
    Aug 15, 2017
    Posts:
    31
    Is the player using a rigidbody component? It might be colliding with something and that could cause the unexpected rotation.

    By the way, you seem to have mixed up the X and Y inputs in the mouse panning section.

    Also instead of checking with an if for each direction I would recommend using unity's built-in input system, that way you wouldn't have to use 4 if-s thus making your code more efficient and easy to read. The unity documentation actually has a code snippet for a simple movement system.

    https://docs.unity3d.com/ScriptReference/Input.GetAxis.html
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    The problem is that Z rotation can be "created" out of Y and X rotations. Imagine this: You rotate an object 90 degrees upwards, so it's looking at the ceiling. Then rotate it 90 degrees to its right. It's now laying on its side and looking at the right wall, even though we never rotated it on its side. It just happened as a result of the other two rotations.

    You can solve this like so:
    Code (csharp):
    1. private float totalXRot = 0f;
    2. private float totalYRot = 0f;
    3.  
    4. void Update() {
    5. ...
    6. totalXRot += Input.GetAxis("Mouse X") * lookSens;
    7. totalYRot += Input.GetAxis("Mouse Y") * lookSens;
    8. transform.rotation = Quaternion.Euler(totalYRot, totalXRot, 0f);
    9. }
    Basically, store your own X and Y rotation numbers, and overwrite the transform's rotation every frame.
     
    treyacarey and Gramms66 like this.
  4. treyacarey

    treyacarey

    Joined:
    Aug 20, 2020
    Posts:
    9
    This was amazing! Thank you so much for such a great explanation and telling me how to fix it, it worked beautifully. Now to do some study on Quaternions so I can understand this...
     
    Gramms66 likes this.