Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Joystick sometime never back to 0 for Unity

Discussion in 'Scripting' started by AlexandreH5, Aug 18, 2019.

  1. AlexandreH5

    AlexandreH5

    Joined:
    Sep 24, 2013
    Posts:
    61
    Hello guys,

    Currently I use a gamepad (xbox controller). My left and right joystick sometime do not back to 0.

    When I use this website (https://html5gamepad.com/), I see that my 2 joystick are back to 0.
    I thought I saw somewhere that Unity does not work on this. Do you have a solution ?

    Thank you.

    Alexandre.
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,083
    For analogue joysticks this is normal, it will get close to 0 but never usually exactly 0. That's why joysticks usually have a 'dead zone' setting - anything under that value is considered to be 0. There's some more info here: https://docs.unity3d.com/Manual/ConventionalGameInput.html
     
    Joe-Censored and RecursiveFrog like this.
  3. AlexandreH5

    AlexandreH5

    Joined:
    Sep 24, 2013
    Posts:
    61
    Hi tonemcbride,

    In Input manager:
    Gravity: 0
    Dead: 0.25
    Sensivity: 0.5

    In my code (below) DeadZone variable egal 0.25f.

    Code (CSharp):
    1.             if (StickInput.magnitude < DeadZone) {
    2.                 StickInput = Vector2.zero;
    3.             } else {
    4.                 StickInput = StickInput.normalized * ((StickInput.magnitude - DeadZone) / (1 - DeadZone));
    5.             }
    But the Vector2 StickInput is sometime not egal to 0
    The print indicated (0.0, 0.2).

    The problem is always here no matter the type of controller.

    Any idea?
     
  4. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,083
    Hi,

    I didn't think that you had to deal with the dead zone yourself - I thought Unity just did all that itself so you could use StickInput without doing anything. Have you tried just using StickInput on it's own?
     
  5. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Unity rounds values then you print them. Let actual input vector be (0.049, 0.24). It's magnitude will be more than 0.25, it will be around 0.253, so it will pass your check of magnitude but print (0, 0.2) to the console. Obvious solution here will be to check both input vector components for being in dead zone and zero them separately.
    Always check your logic against edge cases.
     
  6. AlexandreH5

    AlexandreH5

    Joined:
    Sep 24, 2013
    Posts:
    61
    This is unfortunately not that. I can block the stick with values like this one.

    (Image)

    I don't understand how it's possible...
     

    Attached Files:

  7. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    How is what possible? That's in the deadzone then. 1.01-05 is very small and is essentially 0. As mentioned, it will likely never be exactly 0.
     
  8. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    It is exactly this. Values on your screenshot gives magnitude > 0.25. Same chech passed, input not zeroed.
     
  9. AlexandreH5

    AlexandreH5

    Joined:
    Sep 24, 2013
    Posts:
    61
    My input go to 1 and 0.6 and stay like that, the value are too much higher. I will not make a deadzone >1.

    I have try all 2017 version and the problem is still here. Higher version (2018 / 2019) the bug disappear!
    I am now on Unity. 2018.3.14f1 and everything is ok!

    I also tried on another video game that I made with friends that we will release on PC and Switch. And the problem is also there on the 2017 versions with 3 differents gamepad XBox360 controller, Xbox360 controller made by PowerA and a IFYOO V-one. So we are now on Unity 2018.