0
\$\begingroup\$

I’m working on the framework for my 2.5D fighting game in Unity (it’s gonna be similar to Street Fighter IV) and I’ve got movement down so I want to start adding attacks. Since there’s going to be special attacks I want to add an input buffer as well (the input system I currently have is incredibly basic so I want to apply the input buffer to it as well). This is how it looks like so far:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public CharacterController controller;
    private Vector3 direction;
    public float speed;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        float hInput = Input.GetAxis("Horizontal");
        direction.x = hInput * speed;

        controller.Move(direction * Time.deltaTime);
    }
}

I’ve seen some tutorials on it but they either aren’t explained that well or not that easy to translate into different kinds of projects. What would be the most efficient way to implement an input buffer? I've found this code on a Unity forum, but I'm not sure how I can apply this to multiple chained inputs like quarter circle forward motions:

private List<ActionItem> inputBuffer = new List<ActionItem>();  //The input buffer
private bool actionAllowed;  //set to true whenever we want to process actions from the input buffer, set to false when an action has to wait in the buffer
//Notice I don't have any code here which sets actionAllowed to true, because that probably depends on states like in the middle of throwing a punch animation, or checking if a jump has finished, or whatever
 
void Update()
{
    checkInput();
    if (actionAllowed)
    {
        tryBufferedAction();
    }
}
 
//Check inputs here, and add them to the input buffer
private void checkInput()
{
    if (Input.GetKeyDown(Keycode.W))
    {
        inputBuffer.Add(new ActionItem(ActionItem.InputAction.Jump, Time.time));
    }
    //Add checks for all other inputs here
}
 
//Call when we want to process the inputBuffer
private void tryBufferedAction()
{
    if (inputBuffer.count > 0)
    {
        foreach (ActionItem ai in inputBuffer.ToArray())  //Using ToArray so we iterate a copy of the list rather than the actual list, since we will be modifying the list in the loop
        {
            inputBuffer.Remove(ai);  //Remove it from the buffer
            if (ai.CheckIfValid())
            {
                //Means the action is still within the allowed time, so we do the action and then break from processing more of the buffer
                doAction(ai);
                break;  //We probably only want to do 1 action at a time, so we just break here and don't process the rest of the inputBuffer
            }
        }
    }
}
 
private void doAction(ActionItem ai)
{
    //code to jump, punch, kick, etc
 
 
    actionAllowed = false;  //Every action probably has some kind of wait period until the next action is allowed, so we set this to false here.
    //Some code somewhere else needs to be written to set it back to true
}

I also plan on adding graphic to show the player’s inputs similar to DBFZ or SFV but I’ll climb that mountain once I reach it

\$\endgroup\$
3
  • \$\begingroup\$ Have you searched for past Q&A on this topic and the related topic of detecting analog stick flick gestures? How have you tried applying what you've found so far, and where specifically did you run into trouble? \$\endgroup\$
    – DMGregory
    May 9 at 14:21
  • \$\begingroup\$ It looks like you want to achieve a Hadouken or something like that. Input buffers are not for this. It is used to store inputs that can't have immediate effect, and will be automatically released when idle. You need to cache all instructions (released or not) for a period of time and extract combos from them according to the rules. Maybe use an algorithm like string matching. \$\endgroup\$
    – Mangata
    May 11 at 2:37
  • \$\begingroup\$ @Mangata Ur right. I did some more digging and I managed to come up with a pretty good solution where I name an attack in a list then I can add and remove inputs in that list. If you press all the inputs withtin a certain time frame of each other the special move comes out. Here's the code: toptal.com/developers/hastebin/mazaleqomu.csharp \$\endgroup\$
    – user162217
    May 15 at 14:53

0

You must log in to answer this question.