Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

S Pen Stopped Working After Unity Update

Discussion in 'Input System' started by berkc, Sep 20, 2021.

  1. berkc

    berkc

    Joined:
    Jul 6, 2015
    Posts:
    13
    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:

    Code (CSharp):
    1.             if (Input.GetMouseButton(0))
    2.                 Vector3 touchPos = _mainCamera.ScreenToWorldPoint(Input.mousePosition);
    3.  
    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?
     
  2. makomarkus

    makomarkus

    Joined:
    Oct 28, 2020
    Posts:
    56
    @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
     
  3. makomarkus

    makomarkus

    Joined:
    Oct 28, 2020
    Posts:
    56
    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.

    Code (CSharp):
    1.  
    2. public class MyGame : MonoBehaviour
    3. {
    4.     #if UNITY_ANDROID
    5.     Vector2 mousePresPos = Vector2.zero;
    6.     Vector2 lastMousePos = Vector2.zero;
    7.     static public Vector2 mousePosition = Vector2.zero;
    8.  
    9.     public void updateMousePos() {
    10.         Vector2 mousePos = Input.mousePosition;
    11.  
    12.         if(mousePos != mousePresPos) {
    13.             mousePresPos = mousePos;
    14.             lastMousePos = mousePos;
    15.             yAccumulatedInput = 0;
    16.             xAccumulatedInput = 0;
    17.         }else{
    18.             var mx = Input.GetAxis("Mouse Y");
    19.             var my = Input.GetAxis("Mouse X");
    20.             if(mx != 0 || my != 0) {
    21.                 xAccumulatedInput += mx;
    22.                 yAccumulatedInput += my;
    23.                 lastMousePos.x = mousePos.x + (yAccumulatedInput * 15f);
    24.                 lastMousePos.y = mousePos.y + (xAccumulatedInput * 15f);
    25.             }
    26.         }
    27.         mousePosition = lastMousePos;
    28.     }
    29.     #else
    30.     static public Vector2 mousePosition {
    31.         get {
    32.             return Input.mousePosition;
    33.         }
    34.     }
    35.     #endif
    36.  
    37.     void Update() {
    38. #if UNITY_ANDROID
    39.        updateMousePos();
    40. #endif
    41.        // ...
    42.     }
    43. }
    44.  
     
    Last edited: Oct 6, 2021
  4. makomarkus

    makomarkus

    Joined:
    Oct 28, 2020
    Posts:
    56
    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.
     
  5. makomarkus

    makomarkus

    Joined:
    Oct 28, 2020
    Posts:
    56
    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 - https://us.forums.blizzard.com/en/hearthstone/t/fix-the-s-pen-in-android-version-of-the-game/27315 and https://www.reddit.com/r/hearthston..._it_will_be_one_year_without_s_pen_error_fix/ - 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? ).
     
    Last edited: Oct 6, 2021
  6. makomarkus

    makomarkus

    Joined:
    Oct 28, 2020
    Posts:
    56
  7. OleksiiSB

    OleksiiSB

    Joined:
    Jan 8, 2021
    Posts:
    4
    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?