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.

Mouse Look Issues

Discussion in 'Scripting' started by Thethispointer, Oct 1, 2020.

  1. Thethispointer

    Thethispointer

    Joined:
    Feb 11, 2018
    Posts:
    105
    Hello!

    I am perfecting my own mouseLook script, and right now I am finishing up a tutorial that is helping me learn about controlling the camera with the mouse. Please help me understand why my mouseLook isn't working like Brackey's!

    1. I rotate the player's game object on the Y axis.
    2. I rotate the gameObject (to my understanding...) on the X axis, which is seemingly working good...
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class myMouseLook : MonoBehaviour
    6. {
    7.     public float mouseSensitivity = 100f;
    8.  
    9.     public Transform playerBody;
    10.  
    11.     float xRotation = 0f;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         Cursor.lockState = CursorLockMode.Locked;
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    23.         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    24.  
    25.         xRotation -= mouseY;
    26.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    27.  
    28.         transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    29.         playerBody.Rotate(Vector3.up * mouseX);
    30.     }
    31. }
    32.  


    my rotation side to side isn't working. Can anyone help me out? Thank you! :)
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    1,610
    Code (csharp):
    1. xRotation -= mouseY
    why is everybody doing this??
    please kindly explain what did you expect this code would do?

    at this point I'm thinking there is some kind of Brackeys video that has this as a typo or something.
     
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    1,610
    Ok I take it back, they truly do it like this, not really caring for any of the implications down the line.
    I wouldn't do it like this in a million years, but ok, this seems to be a copy/paste of what Brackeys does anyway.

    Now pay special attention to where's what.
    You need to have a First Person Player as a main object, with a player model object and camera set as its children.
    The First Person Player is supposed to have Character Controller on it, and the camera is supposed to have this script you've shown.

    If you do it like this, THEN it should do what it does in the Brackeys video.

    From your video it's not obvious where your scripts are, and the camera seems to belong to some other object, that's at least what I'm seeing.
     
    Thethispointer likes this.
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    1,610
    (No wonder Brackeys has bailed the scene btw. Throngs of people want to know their location.)

    Btw this is the actual Brackeys video, for the reference.
     
  5. Thethispointer

    Thethispointer

    Joined:
    Feb 11, 2018
    Posts:
    105
    Next time I will include a Brackey's Disclaimer LOL! YES I want to understand his example! AND, if you want to be satisfied I prefer the charactercontroller.move found in the documentation! I am understanding Brackey's not exactly copy and pasting with intent to completely use the code. This part of the reply is not specifically toward anyone.

    To who replied here, great reply! Thank you!
     
  6. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    1,610
    Sorry if I was a bit harsh, it's because I've seen that particular code for at least a couple of times now, and always I'm a little baffled with that particular line. I have nothing against you trying to understand how it works.

    Why I wouldn't do that particular line like that is because that value tends to rotate "indefinitely" meaning that it will surely end up becoming -123873574.343535897 if you're constantly rotating in the same direction (which could happen). Obviously, this isn't an issue for beginners trying to make this feature to work and prototype some FPS, but it can become a source of hard to track jittery motion due to floating point imprecisions that will accumulate over time.

    Apart from that, it leads anyone sufficiently advanced to look at it and think "hmm" -- that line is a needless attention grabber for people who know how to code. Because this accumulation isn't particularly well handled, it definitely looks like a typo or an oversight. If I wanted to make this intentionally, I would intentionally write it as
    xRotation = xRotation - mouseY
    or
    xRotation -= mouseY; // TODO handle unwrapping
    .

    Yadda yadda programmer's blues. Hopefully you've set it up to work.
     
    Thethispointer likes this.
  7. Thethispointer

    Thethispointer

    Joined:
    Feb 11, 2018
    Posts:
    105
    Thank you! I will post an update when I work on this today!
     
  8. Thethispointer

    Thethispointer

    Joined:
    Feb 11, 2018
    Posts:
    105
    Okay, I am going to try to give a detailed reply.

    • I have a 3D primitive cube as the ground, plain material colorized green, kinematic enabled, box collider, rigidbody, freeze position and rotation on all three axis.
    • I have some objects with no colliders to render something to look at.
    • I have a Game Object (Character Brain) with two children, camera and the character's model.
    • script on camera and parent game object


    Mouse Look on Camera, credit Brackey's YouTube
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class myMouseLook : MonoBehaviour
    6. {
    7.     public float mouseSensitivity = 100f;
    8.  
    9.     public Transform playerBody;
    10.  
    11.     float xRotation = 0f;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         Cursor.lockState = CursorLockMode.Locked;
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    23.         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    24.  
    25.         xRotation -= mouseY; // TODO handle unwrapping
    26.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    27.  
    28.         transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    29.         playerBody.Rotate(Vector3.up * mouseX);
    30.     }
    31. }
    32.  
    Character Motor thanks to Unity Documentation
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class characterControllerMoveExample : MonoBehaviour
    6. {
    7.     private CharacterController controller;
    8.     private Vector3 playerVelocity;
    9.     private bool groundedPlayer;
    10.     private float playerSpeed = 2.0f;
    11.     private float jumpHeight = 1.0f;
    12.     private float gravityValue = -9.81f;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         controller = gameObject.AddComponent<CharacterController>();
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         groundedPlayer = controller.isGrounded;
    24.         if (groundedPlayer && playerVelocity.y < 0)
    25.         {
    26.             playerVelocity.y = 0f;
    27.         }
    28.  
    29.         Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    30.         controller.Move(move * Time.deltaTime * playerSpeed);
    31.  
    32.         if (move != Vector3.zero)
    33.         {
    34.             gameObject.transform.forward = move;
    35.         }
    36.  
    37.         // Changes the height position of the player..
    38.         if (Input.GetButtonDown("Jump") && groundedPlayer)
    39.         {
    40.             playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
    41.         }
    42.  
    43.         playerVelocity.y += gravityValue * Time.deltaTime;
    44.         controller.Move(playerVelocity * Time.deltaTime);
    45.     }
    46. }
    47.  
     
  9. Thethispointer

    Thethispointer

    Joined:
    Feb 11, 2018
    Posts:
    105
    Also, if anyone can help me configure my movement to not lock my x axis rotation, that would be amazing!
     
  10. nightwolf123501

    nightwolf123501

    Joined:
    Oct 9, 2020
    Posts:
    1
    if you do xRotation += mouseY, up and down looking is inverted so to prevent this we do xRotation -= mouseY so that up and down controls aren't inverted.
     
  11. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    1,610
    my remark had nothing to do with the sign of the operation.
     
unityunity