Search Unity

Mouse Position Update

Discussion in 'Windows' started by gerardB_fr, Jan 21, 2017.

  1. gerardB_fr

    gerardB_fr

    Joined:
    Jul 21, 2016
    Posts:
    28
    Hi, (sorry for my english)

    I want move the mouse cursor with a gamepad.
    It's ok in Unity Editor and Windows Desktop (SetCursorPos API) but not for windows 10 store apps.
    I partially succeeded with this (very simplified):
    Code (CSharp):
    1.  
    2. // x,y from GetAxis
    3. UnityEngine.WSA.Application.InvokeOnUIThread(() =>
    4.          {
    5.              Point pos = CoreWindow.GetForCurrentThread().PointerPosition;
    6.              Point newPos = new Point(pos.X - x * JoySpeed, pos.Y - -y * JoySpeed);
    7.              CoreWindow.GetForCurrentThread().PointerPosition = newPos;
    8.           }, false);
    9.  
    The mouse movement is perfect but Unity don't update the mouse postion and all my RaycastHit2D (based on Input.mousePosition) over all GameObject (UI and other) are wrong.
    It's as if the mouse did not move for Unity.
    If i move my mouse physically (not in code) the capture is done correctly but not if I move it with the code above.
    My question is : how force Unity to capture the mouse position after my code ?
    Thank you for your help.

    Greetings.
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,423
    Sounds like mouse move event was not fired.
    Only one thing comes to mind: try playing with Player Settings. Could be that you use independent input source, so changing cursor position for main view doesn't trigger event. Try other input options.
     
  3. gerardB_fr

    gerardB_fr

    Joined:
    Jul 21, 2016
    Posts:
    28
    Hi Aurimas,

    Thank you for your answer.
    I made another system because I want the player to physically move the mouse cursor at any time and take over the gamepad, so I created a gameobject (Rect:top-left;32x32;anchors:0-1 all) in a canvas(size of screen) with a collider having the same size as the cursor (offset:16,-16;size:32,32) and that will move with it when the gamepad moves the mouse:
    Code (CSharp):
    1.  
    2. //Vector2 mousePos;
    3. //double xyFactor = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
    4. if (x != 0 || y != 0)
    5.             {
    6.                 UnityEngine.WSA.Application.InvokeOnUIThread(() =>
    7.                 {
    8.                     pos = CoreWindow.GetForCurrentThread().PointerPosition;
    9.                     Point newPos = new Point(pos.X - x * JoySpeed, (float)pos.Y - -y * JoySpeed);
    10.                     CoreWindow.GetForCurrentThread().PointerPosition = newPos;
    11.                     pos = new Point(newPos.X * xyFactor, screenHeight - (newPos.Y * xyFactor));//dips to pixels
    12.                 }, true);
    13.  
    14.                 cursorObject.transform.position = new Vector3((float)pos.X, (float)pos.Y);
    15.             }
    16.           else if (mousePos != (Vector2)Input.mousePosition)
    17.                 cursorObject.transform.position = Input.mousePosition;
    18.  
    19.            mousePos = Input.mousePosition;
    20.  
    If the player moves the cursor or presses a button with the gamepad the detection is done on the collider(or RaycastHit2D) of the cursorObject and not with mousePosition (not updated).
    This code is called only if the current player is playing with the gamepad.
    If it can help someone.

    Greetings.
     
  4. Bagen

    Bagen

    Joined:
    Jun 1, 2014
    Posts:
    7
    Hello! Did you figure out what the problem was?
     
unityunity