S Pen Stopped Working After Unity Update

I have a Samsung Tablet with S Pen. I tried different methods to get input from it. New input system did not detect it as a pen, it was detected as a touch but it was really buggy so I discarded this option. After numerous tries, I managed to get it work in Unity 2020.3.1 using this code piece:

            if (Input.GetMouseButton(0)) 
                Vector3 touchPos = _mainCamera.ScreenToWorldPoint(Input.mousePosition);

Mouse position was changing while I was dragging the S Pen and moving the object in the scene as I wanted.

However, I updated to Unity 2020.3.18 and suddenly S Pen is not working as expected. After debugging, I realised that Mouse Position is only updated when it first touches the screen. While dragging, its value is always constant. GetMouseButton(0) still returns true thus detects the S Pen. Only when I press the button on S Pen the mousePosition value starts to get updated.

Did Unity modified Input methods in recent updates? Is there a workaround for this problem?

@berkc ran into the same issue. Have you ever found a solution?

Input.mousePosition stops updating when the S-Pen touches the screen / mouse button down is registered. Unity 2020.3.18f1

I have submitted a bug report ( case 1370814 )

Currently I’m trying to get around this by recalculating Input.mousePosition in the update cycle and then using a static MyGame.mousePosition instead of Input.mousePosition.

But it’s not satisfactory results, it’s a bit lagging behind or overshooting here and there.

public class MyGame : MonoBehaviour
{
    #if UNITY_ANDROID
    Vector2 mousePresPos = Vector2.zero;
    Vector2 lastMousePos = Vector2.zero;
    static public Vector2 mousePosition = Vector2.zero;

    public void updateMousePos() {
        Vector2 mousePos = Input.mousePosition;

        if(mousePos != mousePresPos) {
            mousePresPos = mousePos;
            lastMousePos = mousePos;
            yAccumulatedInput = 0;
            xAccumulatedInput = 0;
        }else{
            var mx = Input.GetAxis("Mouse Y");
            var my = Input.GetAxis("Mouse X");
            if(mx != 0 || my != 0) {
                xAccumulatedInput += mx;
                yAccumulatedInput += my;
                lastMousePos.x = mousePos.x + (yAccumulatedInput * 15f);
                lastMousePos.y = mousePos.y + (xAccumulatedInput * 15f);
            }
        }
        mousePosition = lastMousePos;
    }
    #else
    static public Vector2 mousePosition {
        get {
            return Input.mousePosition;
        }
    }
    #endif

    void Update() {
#if UNITY_ANDROID
       updateMousePos();
#endif
       // ...
    }
}

If we would have a source code reference on how Input.mousePosition is calculated based on Input.GetAxis("Mouse Y"); Input.GetAxis("Mouse X"); or Input.GetAxisRaw("Mouse Y"); Input.GetAxisRaw("Mouse X"); then we could implement a work-around for this bug.

But I was not able to find any references on how Input.mousePosition is calculated. My code above was the closest I got to it, but I think it may be lacking small amounts of data in a few frames, or my estimated multiplier may be off by a tiny bit. Currently it’s set to 15f based on experimentation.

In our Input settings for the project, Mouse X / Y are set to sensitivity 0.1.

Also not sure if related, but Input.GetAxis("Mouse ScrollWheel") seems to be getting weird values with S-Pen, causing a lot of zoom ins/outs. Would be interesting to see how to detect if we’re actually connected to a S-Pen to disable it, but keep it enabled for a “proper” mouse.

Would be great if we could get S-Pen support working, as it seems many other players in bigger games are begging for such support - Fix the S Pen in Android version of the game! - Bug Report - Hearthstone Forums and Reddit - Dive into anything - so demand for proper S-Pen support is there.

It’s also weird that S-Pen is recognized as a mouse, and not simply as a touch and that Input.stylusTouchSupported returns false ( or is it unrelated? ).

Bug was accepted and fixed in 2022.1.0a3.
They are evaluating requests for backporting the fix to 2019.4, 2020.3, 2021.1, and 2021.2. So leave a comment if that’s interesting for you :

I already left comment in issue, but maybe we can have someone from Unity side to comment on expected time we see fix ported to 2020.3 LTS?