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