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

Right Joystick sticking around whole numbers

Discussion in 'Scripting' started by Afropenguinn, Aug 23, 2014.

  1. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    I am making a top down shooter (As in directly top down, not slanted at all). When using the controller the right analog stick is used to aim, however, it seems to get stuck at the up, down, left, and right positions (relative to the camera which is looking down at the player).

    Code (CSharp):
    1. //rotation
    2. //Vector3 mousePos = Input.mousePosition;
    3. Vector3 rightAxis = new Vector3(Input.GetAxis("Right Stick Horizontal"), 0f, Input.GetAxis("Right Stick Vertical")).normalized;
    4. //mousePos = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, Camera.main.transform.position.y - transform.position.y));
    5. //targetRotation = Quaternion.LookRotation(mousePos - new Vector3(transform.position.x,0,transform.position.z));
    6. targetRotation = Quaternion.LookRotation(rightAxis);
    7. transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRotation.eulerAngles.y, rotationSpeed * Time.deltaTime);
    It seems like the joystick output hangs around the positions where the values are a whole number (1 or 0). Is there any way to avoid this? When moving the stick back to the center it also tends to snap back to one of these positions.
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Change the dead zones to zero in the input manager
    Then you should probably program your own deadzone so it only uses the sticks input once the rightAxis vector.magnitude is above a certain threshold (do not normalize first)
     
  3. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    Ah thank you, I had already tried making my own deadzone but I neglected to remove the built in one. Does anyone know why the axis values behave this way?
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Maybe they just flipped a coin, to either (by default) allow free 360 movement, or allow accurately walking in a straight line on the major axes
     
  5. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    Well for most purposes I would say they made the right call. In any case, thank you very much! Relieved a lot of my headaches with this.

    EDIT: Also, what is the purpose of normalizing it?
     
    Last edited: Aug 23, 2014
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Normalising makes any vector have length (magnitude) of one (unit), but still points in the same direction.
    Alot of maths specifically need unit vectors, but in your case, if you want to ignore stick input for small nudges of the stick, you can't normalise your 'rightAxis' vector until you have checked it's magnitude
     
  7. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    I see, thank you very much! I am now using the same method for movement as well, and it feels much more fluid, and even more accurate when calculating speed! Is there a way to mark a thread as solved?