Search Unity

Alternative for old Input.GetKey()

Discussion in 'Input System' started by io-games, Aug 21, 2019.

Thread Status:
Not open for further replies.
  1. io-games

    io-games

    Joined:
    Jun 2, 2016
    Posts:
    104
    Please help.
    Need alternative for old Input.GetKey()

    for example
    var jumpHeld = MyInput.Player.Jump.isHeld;

    please, no advice to use performed action, I just need bool
     
  2. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    If you are ok with hard-coding the key, to, say "K", then you can just do

    `var jumpHeld = Keyboard.current.kKey.isPressed;`
     
  3. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Code (CSharp):
    1. var jumpHeld = MyInput.Player.Jump.triggered;
     
  4. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Actually, should have read more closely, that will only be true for the frame the action triggered. I.e. equivalent to GetKeyDown() not to GetKey().

    Setting the action type to "Value" and the control type to "Button" and doing

    Code (CSharp):
    1. var jumpHeld = MyInput.Player.Jump.ReadValue<float>() > 0;
    should do the trick.
     
    io-games likes this.
  5. io-games

    io-games

    Joined:
    Jun 2, 2016
    Posts:
    104
    Thanks!

    Would be great if Unity writes migration guide for old basic method and use cases. It will save a lot of time.
     
  6. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
  7. abhinandanpoll

    abhinandanpoll

    Joined:
    Mar 19, 2021
    Posts:
    1
    You should have read more closely, that will only be true for the frame the action triggered. I.e. equivalent to GetKeyDown() not to GetKey().

    Setting the action type to "Value" and the control type to "Button" and doing or more detail visit now Et20slam
     
  8. pantang

    pantang

    Joined:
    Sep 1, 2016
    Posts:
    219
    Depends if you want to register the push just once then,
    if(MyInput.Player.Jump.triggered) DoSomething();

    but if its every frame you want todo something, like say run then
    if(MyInput.Player.Jump.ReadValue<float>() > 0) DoSomethingEverytime():
     
Thread Status:
Not open for further replies.