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

GetAxisRaw not snapping to 1/-1 when using joystick

Discussion in 'Editor & General Support' started by Hemessfell, Jun 17, 2020.

  1. Hemessfell

    Hemessfell

    Joined:
    Aug 3, 2019
    Posts:
    46
    I don't know if it is meant to be that way or if this is a bug, but when I'm using a joystick to check for horizontal inputs, using the GetAxisRaw() function, it simply doesn't return the integer values 1/-1. If this isn't a bug, how can I snap these values?
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,915
    What values are you getting instead? Because you used the word integer and joystick. And GetAxisRaw return -1,0,1 only for keyboards. The joysticks return floating point numbers between -1 and 1.
    https://docs.unity3d.com/ScriptReference/Input.GetAxisRaw.html
     
  3. Hemessfell

    Hemessfell

    Joined:
    Aug 3, 2019
    Posts:
    46
    Yeah, this is exactly what is happening. Any ideas on how to prevent that from happening? Like, make the joystick return 1 and -1 instantly?
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,915
    You can Mathf.RoundToInt the value you're looking at.
     
  5. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,004
    Make it manually. If it's over a value (say 0.5, or it could be smaller if you want it to feel more responsive), treat it like a 1.0, if it's smaller than -0.5 (or whatever) treat it as -1.0, and ignore the rest.
     
  6. Hemessfell

    Hemessfell

    Joined:
    Aug 3, 2019
    Posts:
    46
    Man you saved my life, ty!