Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Thumbstick and Mouse Support for 2D Top-down Freelook?

Discussion in 'Input System' started by WilsonCWong, Jan 10, 2020.

  1. WilsonCWong

    WilsonCWong

    Joined:
    Mar 20, 2013
    Posts:
    35
    Hello,

    I'm making a 2D top-down prototype to learn the new input system and I implemented the ability for the player sprite to rotate according to the thumbstick or mouse position. For the action map, I came up with this:

    upload_2020-1-10_2-1-1.png

    The action returns a Vector2 value. However, I found that I had to do additional processing on the pointer position, namely a screen space transformation to turn it into a usable direction vector:

    Code (CSharp):
    1.  
    2. void Update()
    3. {
    4.     if (_lookInput.action.triggered)
    5.     {
    6.         var dirVector = _lookInput.action.ReadValue<Vector2>();
    7.  
    8.         if (_lookInput.action.activeControl.parent.name == "Mouse")
    9.         {
    10.             dirVector = _playerCamera.ScreenToWorldPoint(dirVector);
    11.         }
    12.  
    13.         // Other processing...
    14.     }
    15. }
    16.  
    Is this a proper way to do it or is there a better way? I keep getting a hunch that I'm doing it wrong...
     
    Last edited: Jan 10, 2020
  2. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    988
    I have the same issue the other way around, the mouse position suits my need but the stick vector2 does not, I have to transform it relative to camera. I used a custom porcessor wich works great if you only have one camera, for multiplayer, I'm stuck (I just posted a new theead on hte topic).

    FYI here is a non tested processor for your used :

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using UnityEngine.InputSystem;
    4.  
    5. #if UNITY_EDITOR
    6. [InitializeOnLoad]
    7. #endif
    8. public class ToWorldPoint: InputProcessor<Vector2>
    9. {
    10.  
    11. #if UNITY_EDITOR
    12.     static ToPointerPosition()
    13.     {
    14.         Initialize();
    15.     }
    16. #endif
    17.  
    18.     [RuntimeInitializeOnLoadMethod]
    19.     static void Initialize()
    20.     {
    21.         InputSystem.RegisterProcessor<ToPointerPosition>();
    22.     }
    23.  
    24.  
    25.     public Camera m_Camera;
    26.  
    27.     public override Vector2 Process(Vector2 value, InputControl control)
    28.     {   if (m_Camera == null) m_Camera = Camera.main;
    29.        return  m_Camera.ScreenToWorldPoint(dirVector)
    30.     }
    31. }
    You just ha.ve to had it to the list of processors in your action map on the mouse pointer