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?
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.
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.
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 :