Search Unity

New Input System - How to Read Mouse Position Continuously ?

Discussion in 'Input System' started by rekatha, Jul 9, 2020.

  1. rekatha

    rekatha

    Joined:
    Dec 18, 2017
    Posts:
    22
    Hello,
    I'm using new Input System.
    With this new Input System, I can make game read value of mouse position.
    The problem is, it only read when mouse is moved. If the mouse is idle, Input System won't invoke my mouse callback so my code to read mouse position won't start.

    upload_2020-7-9_16-32-7.png

    upload_2020-7-9_16-32-25.png


    Code (CSharp):
    1. public void OnAim(InputAction.CallbackContext context)
    2. {
    3.     mousePosition = mainCamera.ScreenToWorldPoint(context.ReadValue<Vector2>());
    while mouse focus on game screen :
    context.started always false
    context.performed always true
    context.cancelled always false

    How to read mouse position while my mouse is stop moving using new Input System?
     
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Solution A:

    Code (CSharp):
    1. Vector2 mousePosition;
    2.  
    3. void OnAim(InputAction.CallbackContext context)
    4. {
    5.     mousePosition = context.ReadValue<Vector2>();
    6. }
    7.  
    8. void Update()
    9. {
    10.     var projectedMousePosition = mainCamera.ScreenToWorldPoint(mouseMosition);
    Solution B:

    Code (CSharp):
    1. void Update()
    2. {
    3.     var mousePosition = playerInput.actions["aim"].ReadValue<Vector2>();
    4.     var projectedMousePosition = mainCamera.ScreenToWorldPoint(mousePosition);
     
  3. rekatha

    rekatha

    Joined:
    Dec 18, 2017
    Posts:
    22
    Thank you for your answer.