Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Resolved How to get Boolean value from the new input system?

Discussion in 'Input System' started by LeoFeitosa, Jul 31, 2020.

  1. LeoFeitosa

    LeoFeitosa

    Joined:
    Jul 5, 2017
    Posts:
    15
    Hello
    Could someone help me get a Boolean value when a button is pressed?
    I've tried several types but it looks like they all return float
     
  2. FamousJoshua

    FamousJoshua

    Joined:
    Feb 20, 2016
    Posts:
    4
    It all returns floats so you just gotta check the float itself.

    Code (CSharp):
    1. if(InputAction.ReadValue<float>() > 0.5f)
    2. //do stuff
    For buttons it returns 1 for down and 0 for up I think. Though I'm not positive so test that. It'd be nice if they would give us a simpler bool option though. Especially for Button types because a float seems silly in this case to me. The solution I'm still using just has properties like Action.IsPressed that returns a bool. Of course we could write our own on top there but still...
     
    LeoFeitosa likes this.
  3. uani

    uani

    Joined:
    Sep 6, 2013
    Posts:
    97
  4. LeoFeitosa

    LeoFeitosa

    Joined:
    Jul 5, 2017
    Posts:
    15
    Hello people.
    I managed to reach the expected result as follows

    First I configured the button action like this:


    Then I just used the "triggered" method to check if the button was pressed
    Code (CSharp):
    1. public static bool test = false;
    2. if (PlayerActions.triggered)
    3. {
    4.     if (test)
    5.     {
    6.         test = false;
    7.     }
    8.     else
    9.     {
    10.         test = true;
    11.     }
    12. }
    Thank you all for your help:)
     
    Last edited: Aug 5, 2020 at 9:05 PM
unityunity