# I am struggling with a combo system for a game of mine

I am currently trying to make a game with a friend but at the moment I am having a few problems with finding a combo system. Until we progressed I copied this code from a video where I use our first combo hit as a source of damage. My code for this is below:

public Animator anim;
private bool attack1 = false;
private float attackTimer = 0;
private float attackCd = 0.3f;
public Collider2D attackTrigger;
public KeyCode Stance1;
public KeyCode Attack1;

// Use this for initialization
void Awake ()
{
anim = gameObject.GetComponent<Animator>();
attackTrigger.enabled = false;
}

public void CreateHitbox (int createhitbox)
{
attackTrigger.enabled = true;
}

public void DestroyHitbox (int destroyhitbox)
{
attackTrigger.enabled = false;
}

// Update is called once per frame
public void Update ()
{
if (Input.GetKeyDown(Stance1))
anim.SetTrigger("Stance");

if (Input.GetKeyUp(Stance1))
anim.SetTrigger("Stance");

if (Input.GetKey(Stance1))
{
//Melee attack
if (Input.GetKeyDown(Attack1) && !attack1)
{
attack1 = true;
attackTimer = attackCd;
}

if (attack1)
{
if (attackTimer > 0)
attackTimer -= Time.deltaTime;
else
attack1 = false;
}

anim.SetBool("attack1", attack1);
}
}


Our attack animations take a bit to initiate so we work with a hitbox as an attack trigger and activate the trigger through an animation event so that enemies are hit when they are supposed to. Furthermore in our game you have to go in a stance where you draw your sword to attack so I wrote it so that you can only attack when you are in the stance.

The problem is that I want to have a 3 hit combo in the end but I have a problem finding a solution that works. In addition I would like the different combo attacks to deal different damage so giving each attack different values would be great. My friend and I are beginners when it comes to C# as well so thinking of something ourselves was not possible.

you can solve a few different ways depending on how you setup the combos. a simple solution may be to be able to combo within the attack itself.

if (attack1)
{
if (attackTimer > 0)
{

attackTimer -= Time.deltaTime;
if(Input.GetKeyDown(Attack1)&& attackTimer >0)
{
TransitionToAttack1Combo();
}
}
else
{
attack1 = false;

}
}
void TransitionToAttack1Combo()
{
//DoComboStuff
}


another solution could be to setup a Sequence array and issue a timespan to be able to initiate the combo.

• To be able to combo within the attack itself sound quite nice and simple. Could you show me how I would set this up? Sadly I don't have the knowledge in C# at the moment. – Montoblass Jul 10 '18 at 14:03
• updated the example of using within your attack. just play around with different ways. this one is a simple solution but if you have a large number of combos I would suggest looping through a combo array. – Justin Markwell Jul 10 '18 at 14:13
• So I would put the bool for my second combo animation in the TransitionToAttack1Combo void? – Montoblass Jul 10 '18 at 14:45
• Yes. Presumably this will look quite ugly in the long run if you have lots of combos, but you may be able to refactor it nicely. I'll stew on it and possibly write an answer that has such a refactoring. – blurry Jul 10 '18 at 14:51
• Wow that would be a great help thank you! We are planning to only use a 3 hit combo so I guess this method should work for us. – Montoblass Jul 10 '18 at 14:53

I think you should allow input while the attack is happening as this feels natural. I think you may build a set of N N-nary trees for how you represent your combo; where 'N' is the number of 'Attack buttons' that you can use to combo; which happens to be the same "branching factor" each node in the N-nary tree would have at maximum.

This would allow you to build your attack trees from a file or an initialization step; rather than dozens of functions.

Each tree would have at its root the first button in an attack, then if another button is pressed; you check to see if that child exists on this node.

Some incomplete code below can give you an idea of what I mean:

class AttackNode
{
enum ATTACK_TYPES
{
X,
Y,
B,
A
};

int AttackDuration;
// other attributes like animation frames, etc.

var children = new AttackNode[ATTACK_TYPES];

}

class AttackManager
{
var baseAttacks = new AttackNode[ATTACK_TYPES];
AttackNode currentAttack;
AttackNode nextAttack;

AttackButtonPress(int button)
{
if(currentAttack != null && currentAttack.AttackTimer < currentAttack.AttackDuration)
{
if(currentAttack.children[button] != null)
{
NextAttack = currentAttack.children[button];
}
}
else
{
currentAttack = baseAttacks[button];
}
}
}


This means that your combos aren't directly stored, but rather implicit in the datastructure you build for your attacks. Rather than ever searching for moves or writing more than one piece of code to do this, you can instead simply build your N-nary tree to solve it for you.

What's more, this is extendable to any depth of combo; so if you desire to add another move, it's as simple as defining it in your JSON (or whatever file type you use) file and having your program read it in.

Note: The above is obviously incomplete code and requires initialization and various Properties/methods you need.

• I think this approach would be a pretty good solution but it is a little bit too complicated for me yet. I am sure that at some point I will need to get to this kind of thinking but at the moment it is just too much for me to handle. – Montoblass Jul 10 '18 at 16:06
• @SomeWindowsUser How is this javascript? – Haytam Jan 29 '19 at 19:48
• @Haytam Sorry, my fault...didn't know c# had implicitly typed variables. – SomeWindowsUser Jan 29 '19 at 19:53