- Home /
arcade stick input commands implementation
So Im trying to find a way to make quarter circle inputs like in 2-d fighting games but i can't find any documentation or tutorials to help me, i know im going to need to do input buffering but im running out of leads.
Answer by WarmedxMints · Mar 21, 2019 at 10:11 PM
I've never needed to do anything like this but it got me interested so I had a think. What I came up with was to split the directions into regions, that way if the player is using a gamepad and not a stick, it won't matter. Then when the region changes, compare it again a preset sequence.
This is what I came up with;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum StickRegion
{
Centre,
Top,
TopLeft,
TopRight,
MiddleLeft,
MiddleRight,
BottomLeft,
BottomRight,
Bottom
}
public class InputRegion : MonoBehaviour
{
public List<Sequence> sequences = new List<Sequence>();
private StickRegion _currentRegion;
private StickRegion _lastRegion;
public void Update()
{
var stickpos = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
_currentRegion = GetStickRegion(stickpos);
if(_currentRegion != _lastRegion)
{
Debug.Log(_currentRegion);
_lastRegion = _currentRegion;
CheckSequences(_currentRegion);
}
}
private void CheckSequences(StickRegion region)
{
for(var i = 0; i < sequences.Count; i++)
{
//If the new region matechs the next region in the sequence
if(region == sequences[i].sequence[sequences[i].currentIndex])
{
//Advanve our index by one
sequences[i].currentIndex++;
//Count has reached the end of the sequence so all inputs matched
if(sequences[i].currentIndex > sequences[i].sequence.Count - 1)
{
//Do something when that combo has been performed
Debug.Log("Combo " + i.ToString() + " Success");
//Reset the index back to 0 ready for it to fire again
sequences[i].currentIndex = 0;
}
continue;
}
//The inputs did not match the sequence somewhere so we reset it back to the start
sequences[i].currentIndex = 0;
}
}
public StickRegion GetStickRegion(Vector2 stickPos)
{
if (Mathf.Abs(stickPos.x) < 0.3f && Mathf.Abs(stickPos.y) < 0.3f)
return StickRegion.Centre;
if(Mathf.Abs(stickPos.x) < 0.3f)
{
if (stickPos.y > 0.3f)
return StickRegion.Top;
if (stickPos.y < -0.3f)
return StickRegion.Bottom;
}
if(stickPos.x > 0.3f)
{
if (Mathf.Abs(stickPos.y) < 0.3f)
return StickRegion.MiddleRight;
if (stickPos.y > 0.3f)
return StickRegion.TopRight;
if (stickPos.y < -0.3f)
return StickRegion.BottomRight;
}
if(stickPos.x < -0.3f)
{
if (Mathf.Abs(stickPos.y) < 0.3f)
return StickRegion.MiddleLeft;
if (stickPos.y > 0.3f)
return StickRegion.TopLeft;
if (stickPos.y < -0.3f)
return StickRegion.BottomLeft;
}
return StickRegion.Centre;
}
}
[Serializable]
public class Sequence
{
public List<StickRegion> sequence;
public int currentIndex;
}
Now, of course, you would want to expand on the Sequence class and likely record the time of the last input so you can check it has not taken too long to perform each move. I would also probably make it a scriptable object with a virtual method which each Sequence can inherit from and then you can call that method to decide what to do when the combo has been performed.
Your answer
Follow this Question
Related Questions
how can I assign and use different controller schemes for a single keyboard? 0 Answers
How to Attack using Animation and Input 0 Answers
Make objects invisable when they are passing through 2 Answers
How do i make my 2d character move one step at a time? 1 Answer
Creating a 3D world for a 2D game: better to do it all in 2D or 3D? 1 Answer