Search Unity

I can look on the y axis only but not x

Discussion in 'Scripting' started by Deathlord38, Mar 19, 2021.

  1. Deathlord38

    Deathlord38

    Joined:
    Mar 19, 2021
    Posts:
    2
    so i was doing some scripting for a game(my first time using unity) and i used some code and it did not work so i messed with it so now i have a peaice of code that can only look up and down but not left to right here is my code


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class MouseLookAround : MonoBehaviour
    {

    public float mouseSensitivity = 150f;

    public Transform playerBody;

    float xRotation = 0f;


    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
    float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

    xRotation -= mouseY;
    xRotation = Mathf.Clamp(xRotation, -90f, 90f);

    transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    playerBody.Rotate(Vector3.up * mouseX);
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    To track down a weird behavior like this, Debug.Log() is always your friend.

    That way you can see where the part that is supposed to turn you isn't getting through.

    In fact, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
    Deathlord38 likes this.
  3. Deathlord38

    Deathlord38

    Joined:
    Mar 19, 2021
    Posts:
    2
    how do you mean this do i like put the line number in the parenthisis then i just put it in my script for unity
     
  4. StankAwake

    StankAwake

    Joined:
    Feb 19, 2019
    Posts:
    2
    Just helped someone else with a rotation issue. It looks like you are only updating one axis for your camera. Also, I am assuming when you say "y axis", you are referring to up/down rotation?

    Rotations are different than postitions, so keep in mind that when referring to axis of rotations, make sure you mention the correct one to avoid confusion. I was initially confused because the X axis for an euler rotation angle is the up/down rotation, not the Y axis.

    Anyways, you need to create a yRotation variable and assign it accordingly.
    Here is a modified version of your souce:
    Code (CSharp):
    1. float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    2. float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    3.  
    4. // Up/down rotation.
    5. xRotation -= mouseY;
    6. xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    7.  
    8. // Left/right rotation.
    9. yRotation += mouseX;
    10.  
    11. transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0f);
    Again, make sure to create a yRotation variable at the top of your source file.