Search Unity

Get mouse axis like the old input system?

Discussion in 'Input System' started by CyanSlinky, May 3, 2020.

  1. CyanSlinky

    CyanSlinky

    Joined:
    Feb 27, 2014
    Posts:
    28
    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:

    input-actions.png

    Code (CSharp):
    1. public class Player: MonoBehaviour
    2. {
    3.         // InputActions
    4.         private PlayerInputActions input;
    5.        
    6.         private Transform myTF;
    7.         private Transform camTF;
    8.        
    9.         private Vector2 coord;
    10.         private Vector2 heading;
    11.         private float zoom;
    12.        
    13.         // Move
    14.         private Vector2 moveAxis;
    15.        
    16.         private void Awake()
    17.         {
    18.             myTF = GetComponent<Transform>();
    19.            
    20.             input = new PlayerInputActions();
    21.             input.PlayerControls.MoveX.performed += ctx => moveAxis.x = ctx.ReadValue<float>();
    22.             input.PlayerControls.MoveY.performed += ctx => moveAxis.y = ctx.ReadValue<float>();
    23.         }
    24.  
    25.         private void Update()
    26.         {
    27.             Debug.Log(moveAxis);
    28.             Move();
    29.         }
    30.  
    31.         private void Move()
    32.         {
    33.             heading = Mouse.current.rightButton.IsPressed() ? (Vector2) myTF.TransformDirection(moveAxis) : Vector2.zero;
    34.             coord += heading * 1.0f;
    35.            
    36.             myTF.position = coord;
    37.         }
    38.        
    39.         public void OnEnable()
    40.         {
    41.             input.Enable();
    42.         }
    43.  
    44.         public void OnDisable()
    45.         {
    46.             input.Disable();
    47.         }
    48. }
    The debug log at line 27 always outputs (0.0, 0.0) so clearly i'm doing something wrong...
     
    qwerty_LS likes this.
  2. qwerty_LS

    qwerty_LS

    Joined:
    Mar 27, 2020
    Posts:
    1
    LOL. I'm struggling with it right now, just googled it and got here xDDD
    Awaiting answers with u mate....
     
    CyanSlinky likes this.
  3. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    You can remove the axis composite and just bind directly to Mouse/x and y.

    upload_2020-5-4_16-43-22.png

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

    Code (CSharp):
    1. var moveX = input.PlayerControls.MouseX.ReadValue();
    Or just bind it all in a single action.

    upload_2020-5-4_16-45-52.png
     
    viknesh2020, qwerty_LS and CyanSlinky like this.
  4. Deknoinarak

    Deknoinarak

    Joined:
    Mar 26, 2021
    Posts:
    1
    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
     
  5. Pixelith

    Pixelith

    Joined:
    Jun 24, 2014
    Posts:
    576
    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.
     
  6. Gamer2021Now

    Gamer2021Now

    Joined:
    Aug 1, 2021
    Posts:
    2
    Thankyou that worked for me, now all my ui buttons are working!
     
  7. mooooi5

    mooooi5

    Joined:
    Jan 8, 2022
    Posts:
    5
    I think mouse X in old input system = Deltamouse.x in new input system