Home > Software design >  How to create mouse look using the new input system?
How to create mouse look using the new input system?

Time:05-22

I'm trying to create a smooth mouse look for my First-Person game. using the new input system like so:

public class MouseLook : MonoBehaviour
{
    [SerializeField] float sensitivityX = 8f;
        [SerializeField] float sensitivityY = 0.5f;
        float mouseX, mouseY;
        [SerializeField] Transform playerCamera;
        [SerializeField] float xClamp = 75f;
        float xRotataion = 0f;
     private void Update()
     { 
             transform.Rotate(Vector3.up, mouseX * Time.fixedDeltaTime);
             xRotataion -= mouseY;
             xRotataion = Mathf.Clamp(xRotataion, -xClamp, xClamp);
             Vector3 targetRotation = transform.eulerAngles;
             targetRotation.x = xRotataion;
             playerCamera.eulerAngles = targetRotation;
    } 
     public void ReceiveInput(Vector2 mouseInput) 
        {
             mouseX = mouseInput.x * sensitivityX;
             mouseY = mouseInput.y * sensitivityY;
        }
}

Here is the InputManager class :

Vector2 mouseInput;
 private void Awake() 
  { groundMovement.MouseX.performed  = ctx => mouseInput.x = ctx.ReadValue<float>();
        groundMovement.MouseY.performed  = ctx => mouseInput.y = ctx.ReadValue<float>();
}
  private void Update()
    {
        movement.ReceiveInput(horizontalInput);
        mouseLook.ReceiveInput(mouseInput); 
    }

    private void OnEnable()
    {
        controls.Enable(); 
    }

    private void OnDestroy()
    {
        controls.Disable(); 
    }

However, for some reason the mouse movement is jittery. So I wanted to use my code I was using before for the old input system which is this:

 [SerializeField][Range(0.0f, 0.5f)] float mouseSmoothTime = 0.03f;
    [SerializeField] float mouseSensitivity = 3.5f;
    float cameraPitch = 0.0f;
    Vector2 currentMouseDelta = Vector2.zero;
    Vector2 currentMouseDeltaVelocity = Vector2.zero;

    private void Update()
    {
       
        Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));

    currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, mouseSmoothTime);

    cameraPitch -= currentMouseDelta.y * mouseSensitivity;

    cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);

    playerCamera.localEulerAngles = Vector3.right * cameraPitch;

    transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity);
}

But how can I configure this for the new input system and still be able to use this ?

public void ReceiveInput(Vector2 mouseInput) 
        {
             mouseX = mouseInput.x * sensitivityX;
             mouseY = mouseInput.y * sensitivityY;
        } 

CodePudding user response:

Upgrade it as below. With the method below you can have mouse delta like the same as in the old system:

Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));

Vector2 targetMouseDelta = Mouse.current.delta.ReadValue()*Time.smoothDeltaTime;
  • Related