Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question InputValue.isPressed throws an error

Discussion in 'Input System' started by Deleted User, Aug 22, 2020.

  1. Deleted User

    Deleted User

    Guest

    Hello,

    I've been trying to check if the key is being pressed or released like so:
    Code (CSharp):
    1. void OnMove(InputValue v)
    2. {
    3.     if (v.isPressed)
    4.     {
    5.         // pass
    6.     }
    7. }
    but it turned out every time the property is equal to true it throws an error. however, when it's equal to false it works just fine.
     
  2. peterfiftyfour

    peterfiftyfour

    Joined:
    Jan 8, 2016
    Posts:
    20
    Bump because I am getting this same issue :mad: How is this even possible!
     
  3. Stephen1701

    Stephen1701

    Joined:
    Mar 29, 2016
    Posts:
    132
    Well I'm late to the party but it's happening to me too.
    And the value DOES work because my code inside if isPressed is working correctly. But I'm still getting the errors.
     
  4. Stephen1701

    Stephen1701

    Joined:
    Mar 29, 2016
    Posts:
    132
    Hello, I figured this out.
    If you Action has a Control Type of Vector 2 (for example if you're trying to use an analogue stick) then Input Value is supplying a Vector2, not a float, and so isPressed is useless.

    The reason it works when set to false is because the Vector will be (0,0) and any 0 value is false.
    So my solution was to compare the value being received against a Vector2.zero, if that is true then the analogue stick has been released, and you can do whatever you need to then,

    if(value.Get<Vector2>() == Vector2.zero)
    {
    // Do something when input is 0, for example, when the analogue stick has been released
    }
     
    dekatron1 likes this.