Search Unity

Why does the player stick to vertical and horizontal axis when looking?

Discussion in 'Scripting' started by datagreed, Sep 17, 2019.

  1. datagreed

    datagreed

    Joined:
    Sep 17, 2018
    Posts:
    42
    I am making a 3d twin-stick shooter.

    This is the code I use for looking with a right stick in FixedUpdate:
    Code (CSharp):
    1. //process aim player input
    2. float aimHorizontal = Input.GetAxisRaw("Horizontal2");
    3. float aimVertical = Input.GetAxisRaw("Vertical2");
    4.  
    5. var lookDirection = new Vector3(aimHorizontal, 0, aimVertical);
    6.  
    7. if (lookDirection != Vector3.zero)
    8. {
    9.     transform.LookAt(transform.position - lookDirection, Vector3.up);
    10. }
    11.  
    Everything works, but the player keeps "snapping" to vertical and horizontal looking directions (you can see this in video - https://www.youtube.com/watch?v=zLOm89RWZX0 ).

    How can I avoid this? The xbox gamepad I use itself seems fine, games like "Ruiner" does not seem to suffer from this issue. I tried using both GetAxisRaw and GetAxis and didn't see any changes.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    This seems to be an effect of the "dead zone" - when an axis is near zero, it gets reported as being zero (so that a slightly miscalibrated stick doesn't result in constant bad input). Try setting that to zero. (If desired, you can re-implement your own "dead zone" algorithm that only kicks in if BOTH axes are near zero.)
     
  3. datagreed

    datagreed

    Joined:
    Sep 17, 2018
    Posts:
    42
    @StarManta OMG, that's it! Thank you very much! I never realized that the dead zone implementation works for both axis independently, that makes total sense now