- Home /
Event from simultaneously pressing button + axis?
Hi all, I'm attempting to create input for combat and could use some help. I need to be able to handle 3 "types" of attack input, ala smash bros - doing in C# but it's really the logic I'm interested in.
1) button press, no axis input 2) button press while tilting analog stick (or holding axis key) 3) button press and analog stick direction press at the same time
Each of these scenarios results in different actions. How would you go about the logic of 3), without it just resulting in the 1) or 2) events firing? Also, how might you create a "buffer" in 3) so that the player's timing in pressing the button and axis direction simultaneously can be a bit off (a bit more forgiving for the player)?
Bonus! How would you allow for case 3) to be chargeable, also ala smash bros? That is, you can initiate the move and then continue to hold the button (but axis no longer matters) to charge the move?
Thanks for any input!
Answer by Eno-Khaon · Jun 05, 2015 at 09:25 PM
If it's meant to literally be based on Smash Bros. style, then you need to consider some of the things they specifically handle: For control pad/keyboard input, you would need to double-tap a direction to run, whereas the analog stick input monitors the time from starting to hold a direction until it passes a threshhold to determine whether the player is walking or running.
Based on that threshhold for analog stick control or a secondary timer relative to pressing a direction/key, figure out how long of a delay there should be on the input before the attack is no longer attached to the action.
With this in mind, you would want a few variables available, including time for last directional input, last direction pressed and/or held (could be a Vector2, depending on how much diagonals would be factored in), and a delay value before the combination of direction/attack is invalidated. Once the direction is no longer held, empty the timer (-1 is often a convenient, "safe" value to fall back on). Or, if a new direction is initialized quickly, reset the timer instead (in the case of the Vector2 basis for direction, test whether the Dot product of the previous and current direction held on the stick is less than 0, basically. If so, the player mashed the opposite direction for a quick turn-around attack).
If it's intended for either attack or direction to come first when pairing them together, then it becomes a messy decision on whether there should be a delay before the attack input is fully counted (which is generally a bad idea for fighting games).
Edit: As for charging up such an attack, it might be prudent to keep tabs on whether buttons are actively being held or not (i.e. OnKeyDown("SomeKey") sets a bool to true, OnKeyUp("SomeKey") sets it to false). Then, when the condition is met for the strong attack, instead of simply executing the attack immediately, wait until the charge time has elapsed or the attackKeyDown boolean is false, whichever comes first.
Answer by yamslayer · Jun 08, 2015 at 05:58 AM
Thanks, there's good ideas in there, and I ended up using most of those ideas, and it simplified the code down into a small nested if series. Here's what I've ended up with so far, in case anyone else is looking to solve something similar:
if (attack){
// start counting button hold time
attackHoldTimer = 0f;
if( h != 0 || v != 0 ){
if( axisInputTimer <= axisInputWindow ){
smashAttack();
} else {
tiltAttack();
}
} else {
neutralAttack();
}