Press interaction on a mouse position not working?

So I’m new to this Input System and trying to create an action that occurs when the user presses, holds and releases the mouse (or touch screen).

I want to take the screen position of the input, convert it to normalized coordinates (-1, 1) and use that value in the “performed” callback. The goal of the action is to move the player left/right depending on where you press/touch on screen. It should also work with gamepads and keyboard as well.

I thought this might be how to achieve it:

Here I have a “Position [Mouse]” action that has a “Press and Release” interaction and a custom “Screen Input” processor to turn the position into the normalized coordinates.

But I never receive any started, performed, cancelled events. If I remove the “Press” interaction then I receive events, but every time the mouse moves, which is not what I want.

Can also see I have a game pad and keyboard sending “Move” commands as well. So I’d like to keep mouse position and touch position within the “Move” action.

Is this not possible? Is there a better/different way I should be doing this?

Ok, after looking at the internals a bit. This is the wrong approach. It’s not actually checking a press, rather, it considers a press if the mouse position magnitude < 0.5. In other words, 0.5 pixels from bottom left corner.

so not sure how to continue here.

Ok, think I got it. Had to create a custom composite binding:

using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.Utilities;

public class ScreenInputComposite : InputBindingComposite<Vector2> {

    #region Fields

    [InputControl(layout = "Axis")]
    public int position;

    [InputControl(layout = "Button")]
    public int button;

    #endregion

    #region Public Methods

    public override Vector2 ReadValue(ref InputBindingCompositeContext context) {

        var positionValue = context.ReadValue<Vector2, Vector2MagnitudeComparer>(position);
        var buttonValue = context.ReadValueAsButton(button);

        if (buttonValue) {

            float sw = Screen.width / 2;
            float sh = Screen.height / 2;

            float x = positionValue.x - sw;
            float y = positionValue.y - sh;

            float h = Mathf.Clamp(x / sw, -1, 1);
            float v = Mathf.Clamp(y / sh, -1, 1);

            return new Vector2(h, v);
        }

        return Vector2.zero;
    }

    public override float EvaluateMagnitude(ref InputBindingCompositeContext context) {

        var buttonValue = context.ReadValueAsButton(button);

        return buttonValue ? 1 : 0;
    }

    #endregion

    #region Private Methods

#if UNITY_EDITOR
    [UnityEditor.InitializeOnLoadMethod]
    static void RegisterEditor() {

        Register();
    }
#endif

    [RuntimeInitializeOnLoadMethod]
    private static void Register() {

        InputSystem.RegisterBindingComposite<ScreenInputComposite>();
    }

    #endregion
}


So now I can use the “Press” interaction correctly and receive the position in normalized coordinates :slight_smile: