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

Question C# Mouse look using new InputSystem

Discussion in 'Scripting' started by qwinci, Oct 19, 2020.

  1. qwinci

    qwinci

    Joined:
    Feb 21, 2017
    Posts:
    7
    Hello, anyone can explain to me how to make mouse look for first-person and third-person players using the new InputSystem?
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Unfortunately, I'm yet to delve deeper and get some practice with it myself, for now I've just acquainted myself with the core principles. Have you tried searching for this, because this has to be pretty common? For example
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    This is my look code for my current first-person project. It's attached to the player's root object and "vCam" is the camera transform:
    Code (CSharp):
    1. [SerializeField]
    2. PlayerInput pi;
    3. [SerializeField]
    4. bool InvertY = false;
    5. [SerializeField]
    6. float MaxPitch = 89f;
    7. [SerializeField]
    8. float LookSensitivity = 1f;
    9. [SerializeField]
    10. Transform vCam;
    11.  
    12. InputAction lookAction;
    13. float pitch = 0;
    14.  
    15. private void Start() {
    16.     Cursor.lockState = CursorLockMode.Locked;
    17.  
    18.     lookAction = pi.currentActionMap.FindAction("Look");
    19. }
    20.  
    21. void Update() {
    22.     ProcessLook();
    23. }
    24.  
    25. private void ProcessLook() {
    26.     var lookInput = lookAction.ReadValue<Vector2>();
    27.  
    28.     pitch += lookInput.y * LookSensitivity * (InvertY ? -1 : 1);
    29.     pitch = Mathf.Clamp(pitch, -MaxPitch, MaxPitch);
    30.  
    31.     vCam.localRotation = Quaternion.Euler(pitch, 0, 0);
    32.     transform.Rotate(0, lookInput.x * LookSensitivity, 0);
    33. }
    My "Look" input action is set up as follows: upload_2020-10-19_21-24-19.png

    upload_2020-10-19_21-24-45.png
     
  4. NullOverrideX

    NullOverrideX

    Joined:
    Oct 18, 2020
    Posts:
    15
  5. qwinci

    qwinci

    Joined:
    Feb 21, 2017
    Posts:
    7
  6. qwinci

    qwinci

    Joined:
    Feb 21, 2017
    Posts:
    7
    I'll try that. Thanks.
     
  7. qwinci

    qwinci

    Joined:
    Feb 21, 2017
    Posts:
    7
    Well, that almost works, only thing is that my character is also slowly rotating without moving the mouse.
    Code (CSharp):
    1.         angle += mouse.delta.y.ReadValue() * Sensitivity * -1;
    2.         angle = Mathf.Clamp(angle, -maxAngle, maxAngle);
    3.  
    4.         Camera.main.transform.localRotation = Quaternion.Euler(angle, 0, 0);
    5.         transform.Rotate(0, mouse.delta.x.ReadValue() * Sensitivity, 0);