Search Unity

Input.GetAxis("Horizontal") running multiple times in Update

Discussion in 'Scripting' started by siddhantchd, May 26, 2019.

  1. siddhantchd

    siddhantchd

    Joined:
    May 17, 2019
    Posts:
    9
    Hi

    I am trying to use the joystick in the VR BOX controller to move left and right but i m having an issue with executing the code and it is being run multiple times inside Update method.

    Code (CSharp):
    1. void Update()
    2.     {
    3.  
    4.         if (Input.GetAxis("Horizontal") > 1)
    5.         {
    6.             Debug.Log("Right Movement");
    7.         }
    8.         if (Input.GetAxis("Horizontal") < 1) //move backward
    9.         {
    10.             Debug.Log("Left Movement");
    11.         }
    12. }
    upload_2019-5-26_21-8-16.png

    Could anyone help with this regard.
    Thanks
     
    Last edited: May 26, 2019
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,088
    Axis should range from -1 to +1, Check your comparison values.
     
    SparrowsNest likes this.
  3. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    1,642
    Also compare against a float l, not an int.

    You can just do
    If(input > 0)
    //right
    else
    //left

    *on my phone, sorry for yhe format
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,088
    Ah yeah, i didnt spot the float comparison
    This is not enough to work, you need the zero condition
    Code (CSharp):
    1. If(input > 0f)
    2. //right
    3. else if (Input < 0f)
    4. //left
    5. else
    6. //No move
     
  5. siddhantchd

    siddhantchd

    Joined:
    May 17, 2019
    Posts:
    9
    @hpjohn @SparrowsNest Thank you for the code
    so i have changed the same as mentioned but still the issue persists

    Code (CSharp):
    1. if (Input.GetAxis("Horizontal") > 0f)
    2.         {
    3.             Debug.Log("Right Movement");
    4.         }
    5.         else if (Input.GetAxis("Horizontal") < 0f) //move backward
    6.         {
    7.             Debug.Log("Left Movement");
    8.         }
    9.         else
    10.         {
    11.             //Do Nothing
    12.         }
    I still get the output in debug multiple times which i do not want
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,088
    Just to check a beginner hazard: Update runs once per frame, per component instance, while the game is running; code inside Update will run multiple times
     
    SparrowsNest likes this.
  7. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    1,642
    If you want it to be triggered only once per click, as opposed to as long as you're holding the key you need to do it this way:

    Code (CSharp):
    1. if(Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)){
    2. //left
    3. } else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow)){
    4. //right
    5. }
    Notice that I used GetKeyDown, you have GetKey that behaves like the input axis by constantly refreshing the value, and you have GetKeyDown/Up, they only evaluate to true for the frame the key was pressed/released.
    this is also true for GetButton if that's what you prefer, look up the docks for more info.
     
  8. siddhantchd

    siddhantchd

    Joined:
    May 17, 2019
    Posts:
    9
    @hpjohn
    Yes i was aware of that. But is there anyway i can run it only once when the joystick is being used.
     
    Last edited: May 27, 2019
  9. siddhantchd

    siddhantchd

    Joined:
    May 17, 2019
    Posts:
    9
    When implementing this i am not able to detect the joystick movement in controller
     
  10. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,088
  11. siddhantchd

    siddhantchd

    Joined:
    May 17, 2019
    Posts:
    9
    Thank you @hpjohn the solution worked.