Street Fighter Input Command

Hello,

I’m trying to implement Street-Fighter-like input command (e.g. down → left + punch). From what I know, I can only access user input via GetXXX(), GetXXXDown() and GetXXXUp() queries. But at low framerate, I cannot figure out the input sequence by these queries (e.g. If the user finished down->left in within a frame, I will get all key-down and key-up events of down and left keys. For this case, I don’t know the input is down->left or left->down).

My temporary workaround is to use GUI input event system. But the problem is I cannot access the button-key mapping: I don’t know which key represents the “punch” button. Now I just hardcode the key name. Another problem is GUI does not generate joystick events.

My questions are:

  1. Is there any user input callback in Unity? If not, can I access the input buffer?

  2. How to get the button-key mapping defined in input manager?

Thanks.

Have a look at this thread on how to do combos, should be exactly what you’re looking for.

Hi CoherentInk,

Thank you for the link. But it’s not exactly my problem. Combo by repeating single key is fine because it only need to check whether a key is pressed. It does not care the input ordering nor how many times the key is pressed.

For input command like “down → left + punch”, it’s sensitive to input ordering. When the framerate is low, it seems no way to detect the input ordering by using Input.GetButtonDown().

The code there is to give you an idea of how to detect a combo. You can use it to find out what the key press just before was and if they were close enough to execute the combo. You could just keep a running tally knowing only the previous key hit and when it was it to decide if it was recent enough. As you go along, you keep track of what combos are currently possible and execute the combo if time between presses is too great or another key is hit, your call.

You’ll have to adapt the code to suit, but it was at least a start. You could store the last few key presses in an array to determine if there was a combo. As the other thread suggests, you could store the time when the key press happened as well so you can find out if they are close enough to warrant a combo.

Unless you’re looking at having a frame rate less than 30fps, why would you have a problem with Input.GetButtonDown()? What sort of timing between key presses are you looking at?

Also, what are the chances that you’d get down and left in the same frame and they were actually separate events? My guess is that if you got both in the same frame, they were effectively pressed at the same time.

How about having two variables for each button, one storing the time a button has been pressed and another storing the time it has been released? Like this more or less:

var Fire1_Pressed = 0;
var Fire1_Released = 0;

function Update()
{
if (Input.GetButton ("Fire1")) // button being held
{
Fire1_Pressed += 1 * time.deltaTime;
}
// Button no longer held but Fire1_Pressed is bigger 
// than zero
else if (!Input.GetButton ("Fire1")  (Fire1_Pressed > 0 || Fire1_Released > 0))
{
Fire1_Pressed = 0; // reset this
if (Fire1_Released == 0) // hasn't started yet
{ Fire1_Released = 10; // or whatever suits
else // already started so decrease every loop
{ Fire1_Released -= 1 * time.deltaTime; }
}

}

Now you can just check how long it’s been since Fire1, or any other button set up like that, got released. Down, Left + Punch would then check how big the Down_Released countdown number is and if it’s within the acceptable range then execute the special move.

As for running on low framerates, since GetButton (not GetButtonDown) will return true all the time it’s being pressed it should register the input as long as it’s not running at slide-show speed.

To CoherentInk:

My game is multiplayer game, so the framerate can easily get slower than 30 fps. And I get this problem when the fps is 2X.

Hi Spacemonkey,
Thank you for the code. But if the framerate is low, the key may be pressed and released within a frame. Then I will miss that key.

What is 2X fps? Two frames per second? Ouch! I’m not sure how being a multiplayer game factors in, but I would argue that any game that runs at 2fps is unplayable. I take it you are testing on older hardware that you want to support?

You could also try looking at Input.inputString since that should give you the order of the keys pressed this frame. That way you could easily determine if the player pressed down->left or left->down (supposing you use wasd rather than the arrow keys).

2X means twenty something, sorry for misleading.

I tried Input.inputString. It has the same problem as GUI event: it stores key name instead of button name. And it does not have any key up/down information. :frowning: