Get mouse axis like the old input system?

So I’ve been trying to use the new input system but i can’t seem to figure out how to setup the mouse axis to have the same behaviour as the old input system.

Here’s what i have so far:

public class Player: MonoBehaviour
{
        // InputActions
        private PlayerInputActions input;
       
        private Transform myTF;
        private Transform camTF;
       
        private Vector2 coord;
        private Vector2 heading;
        private float zoom;
       
        // Move
        private Vector2 moveAxis;
       
        private void Awake()
        {
            myTF = GetComponent<Transform>();
           
            input = new PlayerInputActions();
            input.PlayerControls.MoveX.performed += ctx => moveAxis.x = ctx.ReadValue<float>();
            input.PlayerControls.MoveY.performed += ctx => moveAxis.y = ctx.ReadValue<float>();
        }

        private void Update()
        {
            Debug.Log(moveAxis);
            Move();
        }

        private void Move()
        {
            heading = Mouse.current.rightButton.IsPressed() ? (Vector2) myTF.TransformDirection(moveAxis) : Vector2.zero;
            coord += heading * 1.0f;
           
            myTF.position = coord;
        }
       
        public void OnEnable()
        {
            input.Enable();
        }

        public void OnDisable()
        {
            input.Disable();
        }
}

The debug log at line 27 always outputs (0.0, 0.0) so clearly i’m doing something wrong…

1 Like

LOL. I’m struggling with it right now, just googled it and got here xDDD
Awaiting answers with u mate…

1 Like

You can remove the axis composite and just bind directly to Mouse/x and y.

Also, to have something closer to the old system, you can read out values directly instead of via callbacks.

var moveX = input.PlayerControls.MouseX.ReadValue();

Or just bind it all in a single action.

4 Likes

I have this error after using your method

InvalidOperationException: You are trying to read Input using the UnityEngine.Input class, but you have switched active Input handling to Input System package in Player Settings.
UnityEngine.Input.get_mousePosition () (at <6af207ecd21044628913f7cc589986ae>:0)
UnityEngine.UI.MultipleDisplayUtilities.GetMousePositionRelativeToMainDisplayResolution () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/MultipleDisplayUtilities.cs:40)
UnityEngine.EventSystems.BaseInput.get_mousePosition () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/InputModules/BaseInput.cs:75)
UnityEngine.EventSystems.StandaloneInputModule.UpdateModule () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/InputModules/StandaloneInputModule.cs:175)
UnityEngine.EventSystems.EventSystem.TickModules () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:336)
UnityEngine.EventSystems.EventSystem.Update () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:351)

My unity version: 2021.3.11f1

That’s because your event system is still using the old system. Click it and it’ll give you the option to switch it to a new event system.

Thankyou that worked for me, now all my ui buttons are working!

I think mouse X in old input system = Deltamouse.x in new input system