Search Unity

Mouse Position with New Input System

Discussion in 'Input System' started by gsbenton027, Feb 16, 2020.

  1. gsbenton027

    gsbenton027

    Joined:
    Feb 22, 2018
    Posts:
    6
    Recently I've converted my project to the new input system to be able to use mouse and keyboard, and controllers, allowing for multiple players. While the conversion works for the gamepad, it the player cannot aim based with the mouse. All I want it to do is face where the mouse is on screen. I've used Pointer (Position) and Pointer (Delta), and tried varying it between using Transform.up = (Vector2) aim, to hand.transform.rotate(aim).

    Video with Controller:


    Video with Mouse:


    The problem seems to be that the mouse's coordinates are generating based on the lower left-hand corner of the screen rather than the player's position. If there was a way to change the offset and so that it was world coordinate based, I would do it this way, however, it looks like I have to do it based on the Pointer(Delta). I cannot figure out how to do this and would appreciate suggestions and feedback :).
     
    KokodokoGames likes this.
  2. abel1986

    abel1986

    Joined:
    Feb 15, 2020
    Posts:
    2
    There is way to change screen coordinates to world coordinates, You neeed o use camera.
    Code (CSharp):
    1. worldPos = mainCam.ScreenToWorldPoint(Mouse.current.position);
     
  3. gsbenton027

    gsbenton027

    Joined:
    Feb 22, 2018
    Posts:
    6
    Thanks! I'll try that out!
     
  4. gsbenton027

    gsbenton027

    Joined:
    Feb 22, 2018
    Posts:
    6
    So that worked! But there is one problem... I now need a way to do certain actions depending on different input devices. I'll look into it.
     
  5. Jichaels

    Jichaels

    Joined:
    Dec 27, 2018
    Posts:
    237
    [/ICODE]So, I'm testing mouse position using the new input system, and there is something wrong.

    This is my mousePositionReference (no interaction, no processor) : Sans titre.png

    And this code :

    Code (CSharp):
    1. #if ENABLE_INPUT_SYSTEM
    2.         Vector3 mousePosition = mousePositionReference.action.ReadValue<Vector2>();
    3. #else
    4.         Vector3 mousePosition = Input.mousePosition;
    5. #endif
    6.         mousePosition.z = 20;
    7.         mousePosition = camera.ScreenToWorldPoint(mousePosition);
    8.         mousePosition.z = 0;
    9.         mouseCursor.position = mousePosition;
    Does not produce the same output. Using
    Input.mousePosition
    , mouseCursor stick to my mouse (as expected), but using
    mousePositionReference.action.ReadValue<Vector2>()
    it behaves weirdly (movement of the cursor is not 1:1 with my mouse and there is an offset that gets bigger the closer I get to the edges of the screen).

    Am I being stupid ? Doing something wrong ? Or is it a bug ?

    Ping @Rene-Damm
     
    Last edited: Feb 17, 2020
    ZZ_pf, TechRoxx5, deus0 and 3 others like this.
  6. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    The "position" control of Pointers (mice, pens, touchscreens) is expected to work the same as in the old system. If the mouse position is different between the old and the new system inside of player code, that'd be a bug.

    I've given it a try on 2019.1 and 2019.3 using the Input Device Tester sample (on Windows only) and I do see an occasional 1 pixel difference on X but otherwise the coordinates match 1:1 from what I've seen so far. The 1 pixel difference should get looked into though I'm suspecting it's probably just a precision difference based on the new input system picking up mouse coordinates from a different API (new uses RawInput, old uses WM_ messages for pointer coordinates).
     
  7. Jichaels

    Jichaels

    Joined:
    Dec 27, 2018
    Posts:
    237
    Ok, so after testing for a bit, the problem lies somewhere else. I have one project on the old input system that works fine, and the other one on the new input system that doesn't work. I activated "Both" in the project that doesn't work, and I get the same "wrong" values from both system.

    I'll try to understand what could cause this issue, but it was not related to that apparently, sorry !
     
  8. Jichaels

    Jichaels

    Joined:
    Dec 27, 2018
    Posts:
    237
    Ok, I found the issue. On the project using the new input system, I have the XR packages installed, and that somehow mess up with the game view if any HMD is plugged. After forcing VR off, everything works fine and both system gives the same results.
     
  9. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Hmm, but even with it enabled, the old system *does* keep reporting correct coordinates whereas only the new one starts reporting wrong ones? I'd still consider that a bug and would appreciate a ticket in that case.
     
  10. Jichaels

    Jichaels

    Joined:
    Dec 27, 2018
    Posts:
    237
    No, sorry if I was unclear. Let me explain a bit more :

    I made a game for a gamejam last weekend, and as we only needed mouse position / clicks, we went with the old input system to not lose time on setting up everything with the new input system.

    I'm currently putting a bunch of my code in different custom packages using UPM with git (which is really cool !) so I have a project with all packages in it, including my package for VR (so VR is by default enabled, and I'm able to switch on/off at runtime).

    I added some code and sample from the gamejam project onto my other project and even if I had the exact same scene / setup it was wrong, because of VR (gameview by default only show "lefteye" apparently, you can't say "normal" or "middle"...).

    So everything was good in my gamejam project without VR with the old input system, but in my "custom packages" project, neither (old and new) were working because of VR being enabled.

    To make it simple :

    - No XR plugins installed : Working with old input system
    - XR plugins installed : Wasn't working with the new input system, so I switched to "both" input handling and both were not working
     
  11. harshalm1521

    harshalm1521

    Joined:
    Jan 30, 2020
    Posts:
    1
    hey i am not getting mouse position its showing this kind of error ,

    Assets/Scripts/attack.cs(43,33): error CS0029: Cannot implicitly convert type '(UnityEngine.InputSystem.Controls.AxisControl x, UnityEngine.InputSystem.Controls.AxisControl y)' to 'UnityEngine.Vector2'
     
    ModLunar likes this.
  12. Symo7

    Symo7

    Joined:
    Mar 2, 2014
    Posts:
    23
    Camera.main.ScreenToViewportPoint(Mouse.current.position.ReadValue())
    This doesn't work, at least in the current version as Mouse.current.position returns a Vector2Control object. Try this instead (for 1.0 Preview 7):

    Code (CSharp):
    1. Vector2 pos = Camera.main.ScreenToViewportPoint(Mouse.current.position.ReadValue());
    This returns a (float) range between 0 and 1 independent of the screen size for x and y.
     
  13. Dr4ne

    Dr4ne

    Joined:
    Oct 12, 2018
    Posts:
    1
    Just wanted to say thanks, I've been struggling for a week to get a proper mouse position and this works perfectly :)
     
    ModLunar, Symo7 and azevedco like this.
  14. VedTheDead

    VedTheDead

    Joined:
    Apr 21, 2020
    Posts:
    3
    This doesn't return Vector2 value. I don't know why.
    I am getting this error:

    "cannot convert from 'method group' to 'Vector3' "

    Edit: I just forgot to write the paranthesis. 'ReadValue()'
     
    idbrii likes this.
  15. ditlevrisdahl

    ditlevrisdahl

    Joined:
    May 30, 2017
    Posts:
    26
    How do you convert it to worldSpace then?
     
  16. jgol

    jgol

    Joined:
    Aug 16, 2020
    Posts:
    6
    This works for me:
    Code (CSharp):
    1. Vector3 mousePos = Mouse.current.position.ReadValue();
    2. mousePos.z = Camera.main.farClipPlane * .5f;
    3. Vector3 worldPoint = Camera.main.ScreenToWorldPoint(mousePos);
    The `farClipPlane * .5f` is just the distance from the camera you prefer. Can be anything
     
    ditlevrisdahl likes this.
  17. ditlevrisdahl

    ditlevrisdahl

    Joined:
    May 30, 2017
    Posts:
    26
    TY I will try it out :)
     
  18. SudhanshLone

    SudhanshLone

    Joined:
    May 25, 2021
    Posts:
    1
    The Mouse position is not updating, is this a bug?
    here's the code:

    Controls con;
    Rigidbody2D rb;

    private void Awake()
    {
    con = new Controls();
    }
    void Update()
    {
    Vector2 mousePos = Camera.main.ScreenToWorldPoint(con.GamePlay.LookPos.ReadValue<Vector2>());
    Hold(mousePos);
    }

    public void Hold(Vector2 position)
    {
    var difference = position - (Vector2)pivot.position;
    float rot = Mathf.Atan2(difference.x, -difference.y) * Mathf.Rad2Deg;

    rb.MoveRotation(Mathf.LerpAngle(rb.rotation, rot + offset, speed * Time.deltaTime));
    }
     
  19. jgol

    jgol

    Joined:
    Aug 16, 2020
    Posts:
    6
    Looks like you did not enable your controls. Add this to the end of your Awake() function and see if it helps:

    Code (CSharp):
    1. con.Enable();
     
  20. altair2020

    altair2020

    Joined:
    Mar 6, 2011
    Posts:
    48
    GetMousePositionRelativeToMainDisplayResolution in Library\PackageCache\com.unity.ugui@1.0.0\Runtime\UI\Core\MultipleDisplayUtilities.cs

    looks like its hardcoded to use:

    var position = Input.mousePosition;

    Is this incomptible with the new input system or is the work around to use "Both" input systems ( Player->Other Settings->Active Input Handling = Both) as found below?

    https://www.reddit.com/r/Unity3D/comments/kcvmz2/unitys_ui_event_system_doesnt_seem_to_work_with/

    Am i doing it wrong? is there a new event system for the new input system?
     
  21. nglasl

    nglasl

    Joined:
    Oct 22, 2018
    Posts:
    7
    For anyone else that stumbles across this wondering why their Mouse.current.position.ReadValue() is not working and always returns a zero vector, this doesn't seem to work inside FixedUpdate. So you'll just need to move it somewhere else such as Update.
     
    Spyri, Lorrak and Hoody-indi like this.
  22. If you want to read input in
    FixedUpdate
    you need to switch the input system's Update Mode to either
    FixedUpdate
    or Manually and do it for yourself.

    screenshot.png
     
    Lorrak and SnowKaya like this.
  23. rlabrecque

    rlabrecque

    Joined:
    Aug 27, 2014
    Posts:
    15
    I wish the Input System would throw an exception (in debug/development modes) if it's called from FixedUpdate when Update Mode is set to Dynamic Update or Manual, and vice versa.
     
    ina, Waz and coatline like this.
  24. cloud02468

    cloud02468

    Joined:
    Oct 7, 2020
    Posts:
    15
    Hello, i just have added map system to my game recently, it work very well in editor but in android build it has some problem, i cant move my map anymore. here my code move map:

    Code (CSharp):
    1. void panCamera()
    2.     {
    3.         if (playerInputAction.PlayerActions.Touch.WasPressedThisFrame())
    4.         {
    5.             dragOrigin = mapCam.ScreenToWorldPoint(Mouse.current.position.ReadValue());
    6.         }
    7.  
    8.         if (playerInputAction.PlayerActions.Touch.IsPressed())
    9.         {
    10.             Vector3 difference = dragOrigin - mapCam.ScreenToWorldPoint(Mouse.current.position.ReadValue());
    11.             mapCam.transform.position += difference;
    12.         }
    13.     }
     
    sirshelley likes this.
  25. Domcoc

    Domcoc

    Joined:
    Sep 1, 2017
    Posts:
    11

    I definitely confirm this issue!! I've a VR Project and a simple script to move the VR hand with the mouse for testing purpose. During the past week I really got creazy because I could move the hand with the mouse properly, but my collegue could not do it with her workstation (attached to a hmd). We have tryed everything including copying the whole project on my collegue machine, but nothing seemed to work. Now after reading this answer (thanks to Jichaels) I simply tried to switch off my linkbox and the hand worked properly again! It seems that the OpenXR plugin messes with ScreenToWorldPoint function. I really hope they will revise OpenXR, since it is not the first "inexplicable" issue I've found using this plugin...
     
    sirshelley likes this.
  26. sirshelley

    sirshelley

    Joined:
    Aug 14, 2014
    Posts:
    26