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. Dismiss Notice

Analog Stick distance from deadzone

Discussion in 'Scripting' started by DrLilo, Mar 12, 2015.

  1. DrLilo

    DrLilo

    Joined:
    Oct 16, 2013
    Posts:
    6
    Hey guys. Sorry if this is a re-post, but I couldn't find a satisfactory answer in the old threads.

    Trying to get the distance from the centre (or the deadzone in fact) to the stick's current position, in terms of a float from 0 to 1. I tried using Vector2.magnitude, but this gives a value of 1.5 when the stick is at a diagonal angle. What I want is to see a result of 1 if the stick is "fully pressed" in ANY direction. In other words I only want to know how much pressure is being applied to the stick, I'm not interested in which direction.
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    1.5 sounds about right
    if its 1 in the horizontal, and 1 in the vertical, then the distance is root(2) = 1.414
    You may need to to break it down into 2 tests, for a value of 1 on either H or V
     
  3. DrLilo

    DrLilo

    Joined:
    Oct 16, 2013
    Posts:
    6
    I'm not sure I follow, won't that only work if the stick is pressed in one of the cardinal compass directions?
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Oh right, dont care about direction
    well just normalise down from 1.5
     
  5. DrLilo

    DrLilo

    Joined:
    Oct 16, 2013
    Posts:
    6
    How do I do that? ^_^;
     
  6. DrLilo

    DrLilo

    Joined:
    Oct 16, 2013
    Posts:
    6
    Still no further with this program guys, anyone have any further insight? :(
     
  7. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    If you are holding your analog stick all the way at a 45 degree angle and you get anything but a value extremely close to 1, then you have a cheap controller. Analog sticks report information in an x and y axis from -1 to 1. this, it is possible to report a value at (1,1), which has a distance greater than 1. You want to account for this by doing this (on my phone, no code, sorry):

    Make a vector2 called input = to analog stick movement.
    Float length = input.magnitude
    if magnitude is greater than 1, input = input * (1 / length)

    There ya go! If you're not interested in (x,y) and ONLY want to know how much the stick is displaced:

    float length = input.magnitude
    if length is greater than 1, length = 1
     
    Last edited: Mar 19, 2015
  8. DrLilo

    DrLilo

    Joined:
    Oct 16, 2013
    Posts:
    6
    Thanks a lot, that solution worked. :)

    To clarify, there's nothing wrong with my controller, the magnitude was always going to be 1.4ish at a 45 degree angle, I needed that "if magnitude is greater than 1, input = input * (1 / length)" bit of code in place to get it to work as expected.
     
  9. Brominion

    Brominion

    Joined:
    Sep 30, 2012
    Posts:
    48
  10. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    It depends on which type of controller you have. If you have something like an Xbox controller with a round analog socket, then your controller is not very high quality. If your analog stick is physically in a square socket, then you're fine and it's working as intended.

    When I roll my Xbox One or Xbox 360 controller's analog stick in a circle, the magnitude doesn't stray far from 1. When I use my girlfriend's cheap knockoff Xbox 360 controller, pushing the stick roughly 90% of the way at a 45 degree angle results in a length at sqrt(2). That means there is about 10% of extra distance I can push the stick that will do nothing different. If I roll the stick in a circle, it just follows the outside of the square with one dimension always capped at 1.

    This is why it's always good to check the magnitude of your controller input and cap it at 1. If you don't, then modded or junky controllers have an advantage of moving faster than normal players at a 45 degree angle.
     
  11. DrLilo

    DrLilo

    Joined:
    Oct 16, 2013
    Posts:
    6
    It's a fairly new official Xbox 360 controller. I also have an Xbox one controller to test it with.
     
  12. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    That's actually rather odd...