Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Virtual mouse weird movement issue

Discussion in 'Input System' started by oxeliz, Mar 2, 2023.

  1. oxeliz

    oxeliz

    Joined:
    Jan 13, 2020
    Posts:
    36
    Hi!
    I found a tutorial (
    ) about creating a virtual mouse and it should be kinda working, but it's not.

    Base code
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3. using UnityEngine.InputSystem.LowLevel;
    4.  
    5. namespace nStation
    6. {
    7.     public class GamepadCursor : MonoBehaviour
    8.     {
    9.  
    10.         [SerializeField]
    11.         private RectTransform cursorTransform;
    12.         [SerializeField]
    13.         private float cursorSpeed = 1000f;
    14.         [SerializeField]
    15.         private RectTransform canvasRectTransform;
    16.         [SerializeField]
    17.         private Camera mainCamera;
    18.         [SerializeField]
    19.         private Canvas canvas;
    20.  
    21.         private bool previousMouseState;
    22.         private Mouse virtualMouse;
    23.  
    24.         private void OnEnable()
    25.         {
    26.             mainCamera = Camera.main;
    27.  
    28.             if (virtualMouse == null)
    29.                 virtualMouse = InputSystem.AddDevice<Mouse>("VirtualMouse");
    30.             else if (!virtualMouse.added)
    31.                 InputSystem.AddDevice(virtualMouse);
    32.  
    33.             if(cursorTransform!=null)
    34.             {
    35.                 Vector2 position = cursorTransform.anchoredPosition;
    36.                 InputState.Change(virtualMouse.position, position);
    37.             }
    38.  
    39.             InputSystem.onAfterUpdate += UpdateMotion;
    40.         }
    41.  
    42.         private void OnDisable()
    43.         {
    44.             InputSystem.RemoveDevice(virtualMouse);
    45.             InputSystem.onAfterUpdate -= UpdateMotion;
    46.         }
    47.  
    48.         private void UpdateMotion()
    49.         {
    50.             if (virtualMouse == null || Gamepad.current == null)
    51.                 return;
    52.  
    53.             Vector2 deltaValue = Gamepad.current.leftStick.ReadValue();
    54.             deltaValue *= cursorSpeed * Time.deltaTime;
    55.  
    56.             Vector2 currentPosition = virtualMouse.position.ReadValue();
    57.             Vector2 newPosition = currentPosition + deltaValue;
    58.  
    59.             newPosition.x = Mathf.Clamp(newPosition.x, 0, Screen.width);
    60.             newPosition.y = Mathf.Clamp(newPosition.y, 0, Screen.height);
    61.  
    62.             InputState.Change(virtualMouse.position, newPosition);
    63.             InputState.Change(virtualMouse.delta, deltaValue);
    64.  
    65.             bool aButtonIsPressed = Gamepad.current.aButton.IsPressed();
    66.             if(previousMouseState!= aButtonIsPressed)
    67.             {
    68.                 virtualMouse.CopyState<MouseState>(out var mouseState);
    69.                 mouseState.WithButton(MouseButton.Left, aButtonIsPressed);
    70.                 InputState.Change(virtualMouse, mouseState);
    71.                 previousMouseState = aButtonIsPressed;
    72.             }
    73.  
    74.             // situation 2
    75.             //AnchorCursor(newPosition);
    76.  
    77.             // situation 1
    78.             //cursorTransform.anchoredPosition = newPosition;
    79.         }
    80.  
    81.         private void AnchorCursor(Vector2 position)
    82.         {
    83.             Vector2 anchoredPosition;
    84.             RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, position,
    85.                 canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : mainCamera, out anchoredPosition);
    86.             cursorTransform.anchoredPosition = anchoredPosition;
    87.         }
    88.     }
    89. }
    90.  
    The end of the UpdateMotion() method is where I set the position of the mouse cursor.
    If i uncomment the situation 1(
    ), the mouse cursor moves in the top right quarter of the screen, and the buttons are mouse over, from a side
    If I uncomment the situation 2 (
    ), the mouse cursor mover all the screen, but no mouse over occurs.
    What am i doing wrong?
     
  2. oxeliz

    oxeliz

    Joined:
    Jan 13, 2020
    Posts:
    36
    I think it's an issue with my scaler, but I really can`t figure it out
     

    Attached Files:

  3. smonbrogg

    smonbrogg

    Joined:
    Dec 11, 2012
    Posts:
    17
  4. oxeliz

    oxeliz

    Joined:
    Jan 13, 2020
    Posts:
    36
    if you find your code and could share here, I'd be really glad. I'm still struggling