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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Mouse movement working as gamepad

Discussion in 'Input System' started by tannic, Jul 23, 2020.

  1. tannic

    tannic

    Joined:
    Nov 6, 2016
    Posts:
    5
    Hi!
    How am i able to get mouse movement working as an analog stick on a gamepad ?
    I need a value between -1 and 1.. I have tried with scale and clamp to get proper results, but i don't get the right "feeling" when using the mouse.
    In my code, i would like to treat it the same way, no matter what kind of input device i use (keys, mouse or gamepad)

    Code (CSharp):
    1.        
    2. public void Roll(InputAction.CallbackContext context)
    3.         {
    4.  
    5.             m_Roll = context.ReadValue<float>();
    6.             Debug.Log("Roll!:" + m_Roll);
    7.  
    8.             m_Roll = Mathf.Clamp(m_Roll, -1f, 1f);
    9.         }
    10.  
    I have looked at some code etc. but i didn't find exactly what i was looking for.
     
  2. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,498
    The X and Y position of the mouse from -1 to 1, with 0 being the middle of the screen?
    Code (CSharp):
    1. Vector2 mousePosition = new Vector2(
    2.   ((Input.mousePosition.x / Screen.width) * 2) - 1,
    3.   ((Input.mousePosition.y / Screen.height) * 2) - 1
    4. );
    I just wrote here - haven't tested but should work.
     
  3. tannic

    tannic

    Joined:
    Nov 6, 2016
    Posts:
    5
    Hi,
    I Guess something like that could work, but then i need different methods to select between mouse and keyboard.
    I thought maybe some "Delta Mouse Movement" could work. But i guess i need to add different methods, depending on the inputs.
    actually it might be the best, then i also could have different Roll / Pitch values from the keys, depending on how long they are pressed....
     
  4. FullMe7alJacke7

    FullMe7alJacke7

    Joined:
    Dec 17, 2013
    Posts:
    55
    Code (CSharp):
    1.     [System.Serializable]
    2.     public class PointerDataProcessor
    3.     {
    4.         public Vector2 CurrentMousePosition => currentMousePosition;
    5.  
    6.         private InputMap _inputMap;
    7.         private Vector2 currentMousePosition;
    8.  
    9.         public void Initialize()
    10.         {
    11.             _inputMap = InputSystem_Manager.Map;
    12.  
    13.             _inputMap.Player.Pointer.Enable();
    14.             _inputMap.Player.Pointer.performed += ctx => currentMousePosition = ctx.ReadValue<Vector2>();
    15.         }
    16.     }
    It sounds like you need to normalize your vector.
    Normalizing will clamp its values to -1 and 1 like you're describing.

    You can also add this to the specific input or action via the input system by adding a normalize processor to it.
    https://docs.unity3d.com/ScriptReference/Vector2-normalized.html
     
    Last edited: Jul 24, 2020
    tannic likes this.
  5. tannic

    tannic

    Joined:
    Nov 6, 2016
    Posts:
    5
    Hi!
    Wouldn't that just clamp the value of the screen position? Everything will be (1,1)?
     
  6. FullMe7alJacke7

    FullMe7alJacke7

    Joined:
    Dec 17, 2013
    Posts:
    55
    Perhaps I misunderstood your usage.
    I have the code above in my project and it is giving accurate input for my rotate function using pointer Delta from the input settings.
    I made a static poco class for mine so I can just say "InputSystem_Manager.PointerData.CurrentMousePosition" from anywhere in the project.
     
    Last edited: Jul 27, 2020