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

Input.GetAxisRaw returning values smaller than one on Xbox Controller

Discussion in 'Scripting' started by ScaryFace, Mar 31, 2020.

  1. ScaryFace

    ScaryFace

    Joined:
    Aug 7, 2015
    Posts:
    12
    Heya guys

    I am scripting 2D player movement currently and have run into a problem. from all the threads i have read GetAxisRaw should return a value of 1, 0 or -1. but when i tested it with controller horizontal Axis as input, i get values between those integers.

    Is There a way for GetAxisRaw to return whole numbers when using the controller ?

    My work around is below but it feels unnecessary. some background I am still new to coding, especially in C# (as i studied C++ years ago) your help will be appreciated.

    Code (CSharp):
    1.     void FixedUpdate()
    2.     {
    3.         axisHorizontal = Input.GetAxisRaw("Horizontal");
    4.         Debug.Log(axisHorizontal);
    5.  
    6.         if (Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Player")))
    7.         {
    8.             isGrounded = true;
    9.         }
    10.         else
    11.         {
    12.             isGrounded = false;
    13.         }
    14.      
    15.         if(axisHorizontal > 0)
    16.         {
    17.             moveDirection = 1f;
    18.             rb2d.velocity = new Vector2(moveDirection, rb2d.velocity.y); // X Velocty
    19.  
    20.         }
    21.         else if(axisHorizontal < 0)
    22.         {
    23.             moveDirection = -1f;
    24.             rb2d.velocity = new Vector2(moveDirection, rb2d.velocity.y); // X Velocty
    25.        
    26.         }
    27.         else
    28.         {
    29.             rb2d.velocity = new Vector2(0, rb2d.velocity.y); // X Velocty
    30.         }
    31.  
    32.         animator.SetFloat("Speed", Mathf.Abs(axisHorizontal));
     
    Last edited: Mar 31, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    No, GetAxis only returns floats, but you can easily "bucketize" them into useful integers by passing them to a simple filter like so:

    Code (csharp):
    1. int DigitizeAnalogInput( float axisInput)
    2. {
    3.   const float threshhold = 0.71f;
    4.  
    5.   if (axisInput < -threshhold) return -1;
    6.   if (axisInput > threshhold) return 1;
    7.   return 0;
    8. }
    You can fiddle with threshhold until the controls "feel right." I chose 0.71f because it is one over the square root of 2 and is a good starting point for diagonals. Smaller values make you require less initial control deflection to start motion.

    You would call the above function right between line 3 and line 4 in your above posted code, and you could even reassign the value back to the same variable, or a new one if you prefer.
     
  3. ScaryFace

    ScaryFace

    Joined:
    Aug 7, 2015
    Posts:
    12
    This is awesome ! Thank you ! really :D
     
    Kurt-Dekker likes this.