Hello, first time posting here so apologies if I screw up somewhere, I’m having a problem with some code for a combo system. Now admittedly I’m not the best when it comes to coding and programming so feel free to shoot down my abysmal attempt. The code itself is modified from the keycombo at the Unify Wiki.
Here is the code:
KeyCombo.cs (revised):
using UnityEngine;
using System.Collections;
public class KeyCombo : MonoBehaviour{
public string[] buttons;
public int currentIndex = 0; //moves along the array as buttons are pressed
public int keysPressed;
public float allowedTimeBetweenButtons = 0.3f; //tweak as needed
private float timeLastButtonPressed;
public KeyCombo(string[] b)
{
buttons = b;
}
//usage: call this once a frame. when the combo has been completed, it will return true
public bool Check()
{
if (Time.time > timeLastButtonPressed + allowedTimeBetweenButtons) currentIndex = 0;
{
keysPressed = 0;
if (currentIndex < buttons.Length)
{
if ((buttons[currentIndex] == "Fire1" && Input.GetMouseButtonDown(0)) ||
(buttons[currentIndex] == "Fire2" && Input.GetMouseButtonDown(1)) ||
(buttons[currentIndex] != "Fire1" && buttons[currentIndex] != "Fire2" && Input.GetButtonDown(buttons[currentIndex])))
{
timeLastButtonPressed = Time.time;
currentIndex++;
keysPressed++;
}
if (currentIndex >= buttons.Length)
{
currentIndex = 0;
return true;
}
else return false;
}
}
return false;
}
}
Test2.cs (revised):
using UnityEngine;
using System.Collections;
public class Test2 : MonoBehaviour {
private KeyCombo keycom;
private KeyCombo punch1= new KeyCombo(new string[] {"Fire1"});
private KeyCombo punch2= new KeyCombo(new string[] {"Fire1", "Fire1"});
private KeyCombo punch2kick1= new KeyCombo(new string[] {"Fire1", "Fire1", "Fire2"});
private KeyCombo combo1= new KeyCombo(new string[] {"Fire1", "Fire1", "Fire2", "Fire1"});
void Awake()
{
keycom = GetComponent<KeyCombo>();
}
void Update () {
if (punch1.Check() && punch1.keysPressed == 1)
{
Debug.Log("punch1");
}
if (punch2.Check() && punch2.keysPressed == 2)
{
Debug.Log("punch2");
}
if (punch2kick1.Check() && punch2kick1.keysPressed == 3)
{
Debug.Log("punch2kick1");
}
if (combo1.Check() && combo1.keysPressed == 4)
{
Debug.Log("combo1");
}
}
}
Now here’s what I’m trying to do: I want basic attacks to branch out into combos through this input system, I feel that using this method of input combination would allow me to easily assign attack events/co-routines and other variables to each individual attack and combo possibilities.
This is the problem: let’s say that the ‘&& keycom.currentIndex == x’ conditional was not in the update() function and I did a ‘combo1’ attack (Fire1, Fire1, Fire2, Fire1) the combo would actually generate three ‘punch1’ events (and if done quick enough, two ‘punch2’ events) because it’s meeting the loose criteria of one Fire1 input for that event, now clearly this isn’t what you’d want to happen. So here is what I tried to do counter this: I went through the Keycombo script and looked at the logic of the currentIndex variable, from here I assumed (again, my skills with programming are rusty at best) that the currentIndex would increment by 1 every time the specified inputs were detected and would return to zero after a set amount of time after the last input. So therefore under this assumption I used this to ensure that a set amount of inputs had to be present (as measured through currentIndex) so that certain button combinations would not trigger off during other combo events. But it would appear that currentIndex seems to remain at zero the whole time, I’ve not seen it’s value increase in the inspector during testing in unity when entering combo inputs. This prevents me from executing combos as long as the AND statement is present which I need to prevent falling back to the original problem.
So I’m left wondering where am I going wrong here? the logic should be solid but it clearly doesn’t seem that way
Thanks
Edit: Ok, so I’ve directly referenced the actual combo instance’s variables as per Hexagonius’s answer (thanks) and assigned a dedicated variable to the Keycombo called ‘keysPressed’ which increases every time an assigned input is detected and will revert back to zero if the time is greater than the time the last button pressed and greater than the time allowed between buttons. But it only seems to increment to 1 and nothing more meaning I can only execute the punch1 event. Any ideas?