Search Unity

Check For Input Combo

Discussion in 'Scripting' started by nammo, Aug 8, 2018.

  1. nammo

    nammo

    Joined:
    Aug 8, 2018
    Posts:
    2
    Hi guys and gals,

    I am new to Unity and C# and am trying to make a simple game mechanic work. For now, I just want to move the character left if the correct keyboard/joystick combo is detected, then add to the player's streak (correct number of times the combo was entered). If the player does not enter the combo correctly, the character should not move and the streak should reset to 0.

    In simple English, I mean something like this:

    CHECKINPUT1
    if user inputs correct input1
    checkinput2​
    else
    reset streak
    checkinput1 again​

    CHECKINPUT2
    if user inputs correct input2
    correctcombo​
    else
    reset streak
    checkinput1 again​

    CORRECTCOMBO
    move character
    increment streak
    checkinput1 again​

    I think I understand how to write out the code, but I'm not sure when and where the functions should be called, or how to do this most efficiently. I've tried nesting the IF statements with varying results. I'm also not sure which of the code (if any) should be written under the Update function of the script.

    I know the answer is simple, but I've been staring at my computer screen for too long, lol. I would very much appreciate if someone could give me a hint as to how the code should be structured :)

    Thanks very much!
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,739
    Generally inputs should be checked in Update or a method called by Update, as they can be missed if checked somewhere else because most of the input related methods only return true for the frame the input is occurring. I'd suggest using Input.GetKey instead of Input.GetKeyDown if checking for multiple keys pressed at the same time, because humans are rarely so exact as to press two different keys so Input.GetKeyDown would report true for both on the exact same frame. If you were to use GetKeyDown you'll need to build an input tracking system that caries over between frames.

    Just a last tip, try not to mix your checking for input code with your reacting to input code.
     
  3. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,363
    Solid info from Joe, I would implement this as a Scriptable object that way you can create combos as assets and confugure each for it's keys
     
    Joe-Censored likes this.
  4. nammo

    nammo

    Joined:
    Aug 8, 2018
    Posts:
    2
    I'll try that out. Thanks for the advice!
     
unityunity