Search Unity

New Input System problem

Discussion in 'Scripting' started by pantang, Jun 21, 2020.

  1. pantang

    pantang

    Joined:
    Sep 1, 2016
    Posts:
    219
    I managed to get my movement(vector2) working fine but registering a bool seems to be a hole other issue.

    I just want to register a simple button/key press as true or false.

    So far I have tried setting my config to button/value & digital/any/key/button but whatever I set it to it the only bool it returns is false, and when the key is pressed I get an exception. I could take the float when its one and set it true then but I get the feeling there is a better way?

    InvalidOperationException: Cannot read value of type 'Boolean' from control '/Keyboard/space' bound to action 'player/Jump[/Keyboard/space]' (control is a 'KeyControl' with value type 'float')
    UnityEngine.InputSystem.InputActionState.ReadValue[TValue] (System.Int32 bindingIndex, System.Int32 controlIndex, System.Boolean ignoreComposites) (at Library/PackageCache/com.unity.inputsystem@1.0.0/InputSystem/Actions/InputActionState.cs:2020)
    UnityEngine.InputSystem.InputAction.ReadValue[TValue] () (at Library/PackageCache/com.unity.inputsystem@1.0.0/InputSystem/Actions/InputAction.cs:934)
    UnityStandardAssets.Characters.ThirdPerson.TopDownControl.Update () (at Assets/Scripts/Player/TopDownControl.cs:26)

    Code (CSharp):
    1.                 Debug.Log(m_controls.player.Jump.ReadValue<float>());
    2.                 Debug.Log(m_controls.player.Jump.ReadValue<bool>());
    3.                 m_Jump = m_controls.player.Jump.ReadValue<bool>();
     
    adnanzmn97 likes this.
  2. You mean this?
    Code (CSharp):
    1. if (m_controls.player.Jump.triggered)
    2.       doSomething();
    Although I'm not sure because you didn't say how you are using it and how did you set it up.
     
    Moonthsoft, adnanzmn97 and pantang like this.
  3. pantang

    pantang

    Joined:
    Sep 1, 2016
    Posts:
    219
    .triggered

    was my answer.
     
  4. pantang

    pantang

    Joined:
    Sep 1, 2016
    Posts:
    219
    Yup I just needed to use Triggered instead of read value, thank you!
     
  5. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    I've been sitting AAAALLLLL day looking for a quick solution to replace my old script...
    Thank you fine sir! Exactly what I was looking for.

    This works great when i want to press something once, you dont happen to know how to make it work when I want to HOLD a button?
    Or how to replace Input.GetAxis("Horizontal");:?
    Code (csharp):
    1.  
    2.       if (canTurn) //if canTurn is false the player can not move and is stuck in the direction she is looking. Good for Stunn, Damage, Death.
    3.             {              
    4.                 _moveDirection.x = Input.GetAxis("Horizontal");
    5.             }
    6.  
    7.  
     
  6. pantang

    pantang

    Joined:
    Sep 1, 2016
    Posts:
    219


    You can do it a few ways, heres 2, there is another way to bind it to a method but I cant remember that one off the top of my head.


    if (controls.Bike.RearBrake.ReadValue<float>() > 0)
    {
    rearBrakeOn = true;
    }

    //Jump
    if (controls.Bike.Jump.triggered)
    {
    //do something
    }
     
    RuneShiStorm likes this.
  7. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    omg - That worked! Thank you!
     
  8. pantang

    pantang

    Joined:
    Sep 1, 2016
    Posts:
    219
    Your welcome sir. If you happen to stumble across a good rebind guide hook me up, still stuck on that part.
     
    RuneShiStorm likes this.
  9. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    I was scratching my head too. Thanks for the answer.

    You can also do this:

    bool isPressed=m_controls.player.Jump.ReadValue<Single>() >0;


    or this:

    bool isPressed=m_controls.player.Jump.ReadValue<float>() >0;


    Value read will be 1 if the button is down, and 0 if the button is up.

    in ReadValue<T>, T is a struct, and I think all"Button" actions types are read Single struct, which is a float.
     
    Last edited: Dec 27, 2020