Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    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:
    32
    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:
    11
    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...
     
  3. uani

    uani

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

    LeoFeitosa

    Joined:
    Jul 5, 2017
    Posts:
    32
    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
  5. Red_Knight_SBL

    Red_Knight_SBL

    Joined:
    May 18, 2021
    Posts:
    1
    Take my advice for what it is worth, but your code would be a bit cleaner if you did:
    Code (CSharp):
    1. test = !test
    Other than that, your information was very useful for my latest project.