Getting the mouse position using the new Input System

If you want to use Unity’s new Input System to get the mouse’s position you can use Mouse.current.position.ReadValue() instead of using Input.mousePosition

Take note that this is a Vector2


        private void AdjustPlayerFacingDirection()
        {
            if (!_hasCamera) return;
            Vector3 mousePosition = Mouse.current.position.ReadValue();
            var playerScreenPosition = _camera.WorldToScreenPoint(transform.position);

            _isFacingLeft = mousePosition.x < playerScreenPosition.x;
            _spriteRenderer.flipX = _isFacingLeft;
        }

This took some digging in the docs for the new Input System.
https://docs.unity3d.com/Packages/com.unity.inputsystem@1.7/api/UnityEngine.InputSystem.html

https://docs.unity3d.com/Packages/com.unity.inputsystem@1.7/api/UnityEngine.InputSystem.Mouse.html

https://docs.unity3d.com/Packages/com.unity.inputsystem@1.7/api/UnityEngine.InputSystem.Pointer.html#UnityEngine_InputSystem_Pointer_position

https://docs.unity3d.com/Packages/com.unity.inputsystem@1.7/api/UnityEngine.InputSystem.Controls.Vector2Control.html#extensionmethods

https://docs.unity3d.com/Packages/com.unity.inputsystem@1.7/api/UnityEngine.InputSystem.InputControl-1.html#UnityEngine_InputSystem_InputControl_1_ReadValue

2 Likes

Privacy & Terms