Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

New Input System FPS Script Help!!

Discussion in 'Scripting' started by djc51401, Jun 5, 2020.

  1. djc51401

    djc51401

    Joined:
    Apr 3, 2020
    Posts:
    12
    Hi! Im trying to learn the new Input system and i got movement down, but i cant seem to figure out mouse look. its a 1st person controller by the way. This is the script i got:

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6.  
    7. public class PlayerController : MonoBehaviour
    8. {
    9.  
    10.  
    11.     public int walkSpeed;
    12.     public int runSpeed;
    13.  
    14.     public float sensitivityX;
    15.     public float sensitivityY;
    16.  
    17.     int moveSpeed;
    18.  
    19.     CharacterController controller;
    20.     Vector3 moveDirection;
    21.     bool jump;
    22.     bool run;
    23.  
    24.     Transform cam;
    25.     Transform player;
    26.  
    27.     float lookX;
    28.     float lookY;
    29.  
    30.  
    31.  
    32.  
    33.     // Start is called before the first frame update
    34.     void Start()
    35.     {
    36.         controller = GetComponent<CharacterController>();
    37.         player = transform;
    38.         cam = Camera.main.transform;
    39.     }
    40.  
    41.     // Update is called once per frame
    42.     void Update()
    43.     {
    44.         Move();
    45.         Look();
    46.     }
    47.  
    48.     void Look()
    49.     {
    50.         player.rotation = Quaternion.Euler(0, lookX, 0);
    51.         cam.rotation = Quaternion.Euler(lookY, 0, 0);
    52.     }
    53.  
    54.     void Move()
    55.     {
    56.         if (run)
    57.         {
    58.             moveSpeed = runSpeed;
    59.         }
    60.  
    61.         if (!run)
    62.         {
    63.             moveSpeed = walkSpeed;
    64.         }
    65.  
    66.         controller.Move(moveDirection * moveSpeed * Time.deltaTime);
    67.     }
    68.  
    69.     public void OnMove(InputAction.CallbackContext context)
    70.     {
    71.         moveDirection = new Vector3(context.ReadValue<Vector2>().x, 0, context.ReadValue<Vector2>().y);
    72.     }
    73.  
    74.     public void OnLook(InputAction.CallbackContext context)
    75.     {
    76.         lookX = context.ReadValue<Vector2>().x;
    77.         lookY = context.ReadValue<Vector2>().y;
    78.     }
    79.  
    80.     public void OnJump(InputAction.CallbackContext context)
    81.     {
    82.         jump = context.started;
    83.     }
    84.  
    85.     public void OnRun(InputAction.CallbackContext context)
    86.     {
    87.         run = context.performed;
    88.     }
    89.  
    90. }
    91.  
    On the player I have this script attached, a player Input Component, with unity components that attach to each of the on look on jump and on run functions. What this script does now is when i move the mouse it moves but then quickly shoots back to the original position so it doesnt actually look around. Thanks for the help in advance!!
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    5,902
    Didn't you want increase or decrease the rotation by whatever the lookx/y contains? The Input System will go back to zero on these values as soon as you don't push the joystick/move the mouse. You need incremental rotation.
     
  3. djc51401

    djc51401

    Joined:
    Apr 3, 2020
    Posts:
    12
    Yes, and i was thinking about that, but even when i added say:

    Code (CSharp):
    1.         player.rotation = Quaternion.Euler(0,player.rotation.y + lookX, 0);
    2.         cam.rotation = Quaternion.Euler(cam.rotation.x + lookY, 0, 0);
    it still would do the same thing.
     
  4. djc51401

    djc51401

    Joined:
    Apr 3, 2020
    Posts:
    12
    I fixed it!

    Heres what i didnt incase anyone comes across this:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6.  
    7. public class PlayerController : MonoBehaviour
    8. {
    9.  
    10.  
    11.     public int walkSpeed;
    12.     public int runSpeed;
    13.  
    14.     public float sensitivityX;
    15.     public float sensitivityY;
    16.  
    17.     public float minY = -60f;
    18.     public float maxY = 60f;
    19.  
    20.     int moveSpeed;
    21.  
    22.     CharacterController controller;
    23.     Vector3 moveDirection;
    24.     bool jump;
    25.     bool run;
    26.  
    27.     Transform cam;
    28.     Transform player;
    29.  
    30.     float lookX;
    31.     float lookY;
    32.  
    33.  
    34.  
    35.  
    36.     // Start is called before the first frame update
    37.     void Start()
    38.     {
    39.         Cursor.lockState = CursorLockMode.Locked;
    40.         controller = GetComponent<CharacterController>();
    41.         player = transform;
    42.         cam = Camera.main.transform;
    43.     }
    44.  
    45.     // Update is called once per frame
    46.     void Update()
    47.     {
    48.         Move();
    49.         Look();
    50.     }
    51.  
    52.     void Look()
    53.     {
    54.         lookY = Mathf.Clamp(lookY, minY, maxY);
    55.         player.localEulerAngles = new Vector3(0,lookX, 0);
    56.         cam.localEulerAngles = new Vector3(lookY, 0, 0);
    57.     }
    58.  
    59.     void Move()
    60.     {
    61.         if (run)
    62.         {
    63.             moveSpeed = runSpeed;
    64.         }
    65.  
    66.         if (!run)
    67.         {
    68.             moveSpeed = walkSpeed;
    69.         }
    70.  
    71.         controller.Move(moveDirection * moveSpeed * Time.deltaTime);
    72.     }
    73.  
    74.     public void OnMove(InputAction.CallbackContext context)
    75.     {
    76.         moveDirection = new Vector3(context.ReadValue<Vector2>().x, 0, context.ReadValue<Vector2>().y);
    77.     }
    78.  
    79.     public void OnLook(InputAction.CallbackContext context)
    80.     {
    81.         lookX += context.ReadValue<Vector2>().x * sensitivityX * Time.deltaTime;
    82.         lookY -= context.ReadValue<Vector2>().y * sensitivityY * Time.deltaTime;
    83.     }
    84.  
    85.     public void OnJump(InputAction.CallbackContext context)
    86.     {
    87.         jump = context.started;
    88.     }
    89.  
    90.     public void OnRun(InputAction.CallbackContext context)
    91.     {
    92.         run = context.performed;
    93.     }
    94.  
    95. }
    96.  
    instead i had it as:

    lookX = context.ReadValue<Vector2>().x * sensitivityX * Time.deltaTime;
    lookY = context.ReadValue<Vector2>().y * sensitivityY * Time.deltaTime;

    Which isnt incremental, and i did it on local rotation too because for some reason quaternions made it very glitchy.
     
    Lucifer95 likes this.
  5. JDaniel1998

    JDaniel1998

    Joined:
    Dec 30, 2019
    Posts:
    1
    Hey could you attach a screen shot of your Action Map. I'm trying something similar but I cant get it to read my input actions
     
    Niffler_ likes this.
  6. marcospgp

    marcospgp

    Joined:
    Jun 11, 2018
    Posts:
    65
    @djc51401 You shouldn't be using
    Time.deltaTime
    in OnLook(). The delta value tells you how much the mouse has moved since the last time the event was triggered, so it doesn't depend on how long that interval was.

    Besides, Time.deltaTime is about how long it has been since the last frame, and your event handlers (such as OnLook()) are not tied to the frame rate at all. They are called separately by the input system whenever there is a user input.
     
  7. ExNinja

    ExNinja

    Joined:
    Dec 4, 2013
    Posts:
    20
    @djc51401 thanks for the post. This helped me with some new Input System stuff. :)

    In regards to your comment about the Quaternion making things glitchy: It looks like you were trying to set the x and y values of the Quaternion as if they were the x and y Euler values. You fixed this in the last version you posted, but just in case someone else is coming along and reading this, Quaternions are internally very different from the Euler angles we usually think of for rotations. In a Quaternion, x, y, and z are imaginary numbers (i.e., multiples of i, which is the square root of -1), and w is a real number. It's a bizarre way to store rotations, but it works really well in matrix multiplication (and the order of the matrix multiplication determines whether it's a local or global rotation), and it works because of the interesting circular nature of multiplying i:

    i * i = -1
    -1 * i = -i
    -i * i = 1
    1 * i = i
    i * i = -1 …and so on​

    When I'm teaching, I usually tell my students to not worry about how Quaternions work and to just use Euler angles instead, as you have done.

    More info: https://en.wikipedia.org/wiki/Quaternion
     
unityunity