Search Unity

Question How to setup a simple mouse position action?

Discussion in 'Input System' started by Garrafote, Jul 8, 2020.

  1. Garrafote

    Garrafote

    Joined:
    Jun 13, 2013
    Posts:
    48
    Hi, I'm struggling trying to setup a simple action mapped to mouse position.

    Here is how I'm doing the action map:

    (I've tried with 'Value' and 'Pass Through' action types)
    upload_2020-7-8_10-55-41.png

    upload_2020-7-8_10-55-50.png

    Here is how I'm reading the value from script:
    Code (CSharp):
    1. // code extracted from Update()
    2. if (_isPointerEnabled)
    3. {
    4.     // Added this line just to make sure that the Pointer action is enabled
    5.     // calls <input map instance>.Disable() to disable the whole action map
    6.     // then calls <input map instance>.Player.Pointer.Enable() to selectively enable the input action
    7.     GameInput.Enable(InputActions.PlayerPointer);
    8.     _pointerPlane = new Plane(socket.rightHand.forward, socket.rightHand.position);
    9.  
    10.     // action.ReadValue<Vector2>() always returns (0.0, 0.0)
    11.     Vector2 pointerPosition = GameInput.Player.Pointer.ReadValue<Vector2>();
    12.  
    13.     // added this line to compare the input action value with the current mouse value.
    14.     // seems to return the proper value
    15.     pointerPosition = UnityEngine.InputSystem.Mouse.current.position.ReadValue();
    16.  
    17.     var ray = GameManager.cinemachineBrain.OutputCamera.ScreenPointToRay(pointerPosition);
    18.     if (_pointerPlane.Raycast(ray, out float enter))
    19.     {
    20.         Vector3 target = ray.GetPoint(enter);
    21.         _pointer.position = Vector3.MoveTowards(_pointer.position, target, Time.deltaTime*10);
    22.     }
    23. }
    24.  
    (this image shows that even though the input action is enabled I get a default value (0.0, 0.0))
    upload_2020-7-8_10-50-28.png

    (stepping through the next line I get a proper mouse position)
    upload_2020-7-8_10-53-11.png

    What am I missing here?

    EDIT:
    This snippet of code where call ReadValue<Vector2>() followed by Mouse.position.ReadValue() was an attempt to inspect what is wrong with my code and have a temporary workaround.
    For production I need this code to work with different input schemes.
     
    Last edited: Jul 9, 2020
  2. rekatha

    rekatha

    Joined:
    Dec 18, 2017
    Posts:
    22
    Not sure what you want.
    I just assume you want mouse position in current screen.
    if that the case, set Action:

    Action Type : Value
    Control Type: Vector2
    upload_2020-7-9_16-12-4.png

    set Binding:
    Path: Position [Mouse]
    upload_2020-7-9_16-13-1.png

    On `Player Input` component set `Events` to `Invoke Unity Events`

    Callback:

    Code (CSharp):
    1. public Camera mainCamera;
    2.  
    3. void OnMouseMove(InputAction.CallbackContent context)
    4. {
    5.     Vector2 mousePosition = mainCamera.ScreenToWorldPoint(context.ReadValue<Vector2>());
    6. }
     
  3. Garrafote

    Garrafote

    Joined:
    Jun 13, 2013
    Posts:
    48
    Yes, you are right. I want the mouse position on the current screen.

    Got it! I've tried having Type "Value" and also tried "Pass Through" as it is what is being used by the default 'UI/Point' with no luck.

    I've tried that too! Actually I've changed mine to 'Position [Pointer]' after reading in the documentation that Mouse is a Pointer with extra functionality. Is that not right? Should I change it back to Mouse?

    I'm not using the Player Input component though. I'm instantiating and managing the action map myself. Also, on other parts of my code I'm reading Input Action values through ReadValue<Vector2>() just fine without the need for rising events. Can I do that with the mouse position as well?

    here is a little snippet of code that already works for me:
    upload_2020-7-9_9-12-10.png

    Code (CSharp):
    1. // GameInput.cs
    2. public static InputActionCollection Map => _map;
    3. public static PlayerActions Player => _map.Player;
    Code (CSharp):
    1. // PlayerMovement.cs
    2. private void Update()
    3. {
    4.     if (_controller.enabled)
    5.     {
    6.         ProcessMoveInput();
    7.     }
    8.    
    9.     ProcessLookInput();
    10. }
    11.  
    12. private void ProcessMoveInput()
    13. {
    14.     float dt = Time.deltaTime;
    15.  
    16.     // GameInput.Player is the generated PlayerActions code from my action map
    17.     Vector2 moveInput = GameInput.Player.Move.ReadValue<Vector2>();
    18.  
    19.     [...]
    20. }
    21.  
    22. private void ProcessLookInput()
    23. {
    24.     float dt = Time.deltaTime;
    25.  
    26.     _lookInput = GameInput.Player.Look.ReadValue<Vector2>();
    27.  
    28.     [...]
    29. }
    To clarify, I already have my input system laid down and working, and now I want to add a new InputAction bound to the current mouse position. I've configured and pulled the new InputAction value according to the documentation but somehow I'm getting only (0.0, 0.0).

    I've tried to debug it but I couldn't figure out what is wrong, everything looks to be setup right and when I step through the code I don't see anything wrong. Am I missing something obvious here? Does this look a bug? Does this thread need more clarification?
     
  4. Garrafote

    Garrafote

    Joined:
    Jun 13, 2013
    Posts:
    48
    Ok I figured it out.

    The action map was disabled while the input action was enabled. This somehow caused my input actions to partially work. Like, they fire events but their Interactions wont have any effect and their values wont be read properly.

    My solution was to rework my GameInput class in a way that the action maps are always enabled when at least one of its input actions are enabled.
     
    Last edited: Jul 10, 2020
    rekatha likes this.
  5. X3doll

    X3doll

    Joined:
    Apr 15, 2020
    Posts:
    34
    Sorry for the necro-posting. I have the same problem, but somehow not understand how do you fixed.
    I Either don't use the player input evil class, and bind the input like you putting the reference in a static field and access from a generic InputManager class.

    Code (CSharp):
    1. public class InputManager : MonoBehaviour
    2.     {
    3.         public static GameControls inputActions;
    4.        
    5.         private void Awake()
    6.         {
    7.             // Create the game controls
    8.             inputActions = new GameControls();
    9.  
    10.             // Make it permanent through scenes
    11.             DontDestroyOnLoad(gameObject);
    12.         }
    13.  
    14.         private void OnEnable()
    15.         {
    16.             inputActions.Enable();
    17.         }
    18.  
    19.         private void OnDisable()
    20.         {
    21.             inputActions.Disable();
    22.         }
    23.     }
    I Have a strange problem with the mouse position. In shorts, i have the mouse position that and the left stick that handle the character looking. I handle the mouse position value convertion in this way:

    Code (CSharp):
    1. public void OnTrajectoryDirection(InputAction.CallbackContext ctx)
    2.         {
    3.             if(isShootingPressed)
    4.             {
    5.                 if (ctx.control.device.path == "/Mouse")
    6.                 {
    7.                     Vector2 mousePosition = Camera.main.ScreenToWorldPoint(ctx.ReadValue<Vector2>());
    8.                     inputDirection = (mousePosition - (Vector2)transform.position).normalized;
    9.                 }
    10.                 else
    11.                 {
    12.                     inputDirection = ctx.ReadValue<Vector2>();
    13.                 }
    14.  
    15.                 Debug.Log("Trajectory: " + inputDirection);
    16.             }
    17.         }
    In this order if i use the right stick, it works fine, and if swap to the mouse after it works fine. BUT if i switch out to the gamepad right stick again, it does not work anymore (Seems that the mouse still catch the input but does not call this event).

    Do you have the same problem?