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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question L2 and R2 buttons are float -1 to 1

Discussion in 'Input System' started by n00b_013, Jun 6, 2021.

  1. n00b_013

    n00b_013

    Joined:
    Dec 20, 2020
    Posts:
    14
    Dear community,

    I finally got my PS4 controller working on the racing game I'm making, though, with some issues still.. As the input for the triggers (L2 and R2) are in float -1 (unpressed) to +1 (pressed).. My vehicle is responding to the non-pressed by driving in reverse..

    Now I'm stuck on 2 questions, which my search online could not resolve..

    1) How can I normalize the input for R2 to be between 0 and 1
    2) How can I make the input for L2 to be between -1 and 0

    Both triggers are used for the vertical input..
     
  2. joaorequiao

    joaorequiao

    Joined:
    Oct 15, 2015
    Posts:
    5
    Hey! That's a simple math problem. You can use the following formula:

    (value-min)/(max-min)

    in you case that would be: value - (-1)/ 1 - (-1) = (value +1)/ 2

    So, if you get a value of -0.8 from the input system, you would have: (-0.8+1)/2 = 0.2/2 = 0.1. So, turning -0.8 into a value between 0 and 1 (normalizing) gives you 0.1. Got it?

    For your second question, you can just return -value.

    Hope this helped!

    * a code example (not optimized, written in a way that's easier for beginners to understand):

    Code (CSharp):
    1. public void R2()
    2. {
    3.       float value = controls.yourMap.YourR2Action.ReadValue<float>();
    4.       return (value +1)/2;
    5. }
    6.  
    7. public float L2()
    8. {
    9.       float value = controls.yourMap.YourL2Action.ReadValue<float>();
    10.       return -((value +1)/2);
    11. }
    12.  
     
    Last edited: Jun 7, 2021
    n00b_013 likes this.
  3. n00b_013

    n00b_013

    Joined:
    Dec 20, 2020
    Posts:
    14
    Thnks a lot for the quick response! I'm going to try and implement your script tomorrow and update accordingly! :)