Random keys combo

Here’s the code of combo itself:

public class doCombo : MonoBehaviour {
	
	private comboSystem niceCombo= new comboSystem(new KeyCode[] {KeyCode.W,KeyCode.A,KeyCode.S});

	void Update () 
	{
		if(niceCombo.check())
		{
			SceneManager.LoadScene ("fight");
		}

	}
}

And here’s the main method:

public class comboSystem
		{
			public KeyCode[]keys;
			public int index;
			public float inBetweenTime;
			public float lastKeyPressTime;
			public comboSystem (KeyCode[] k)
			{
				index=0;
				inBetweenTime=1.5f;
				lastKeyPressTime=0.0f;
				keys=k;
			}

			public bool check()
			{
				if(Time.time>lastKeyPressTime+inBetweenTime)
				{
					index=0;
					lastKeyPressTime=Time.time;
					return false;
				}
				else
				{
					if(index<keys.Length)
					{
						if(Input.GetKeyDown(keys[index]))
						{
							lastKeyPressTime=Time.time;
							index++;
							if(index>=keys.Length)
							{
								index=0;
								return true;
							}
							else
							{
								return false;
							}

						}
						else
						{
							return false;
						}
					}
					else
					{
						return false;
					}
				}
			}
		}

What I need is a combination of random keys from selected pool (e.g. WASD), and if possible some kind of visible indication of what the combo is.
It’s something like this: Naruto Shippuden: Ultimate Ninja Storm 3: Madara vs The 5 Kages Boss Battle (Best Quality!) - YouTube

You see what buttons you need to press, you press them, something happens.

What I need is a way to make it random and visible for user.

Hi @Justaway, you need to add UI image and Sprite[] buttonImages variables. Then just swap them depends on which key you need to press from your Keycode[]k It’s also good to add some timer to press every QTE and something if player miss it. To use keycodes ramdomly please try:

int o = Random.Range(0, k.length);
// change sprite to buttonImages[o];
//if(timer fewSeconds)//f.e. Time.time<timer
    If(input.getKeyDown(k[o]))
// do your stuff

But I’m not able to test it now so something may be wrong :slight_smile:

@Justaway, here is video showing how it’s working:

One script to enable combo and set timer and combo length, another script for rest, is’t checking the right key, if you press other - game over, if you are too slow - game over.

using UnityEngine;
using System.Collections;

public class EnableCombo : MonoBehaviour {

	// author: Kamil104
	// http://answers.unity3d.com/users/225838/kamil1064.html

	public GameObject comboPanel;
	public KeyCode startCombo;
	public float _timer;
	public int _comboLength;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetKeyDown(startCombo) && comboPanel.activeSelf == false) // if you press key and combo game isn't already started
		{
			comboPanel.SetActive(true);
			// you may use Random.range() here for borh variables
			comboPanel.GetComponent<fight_combo>().SetUpGame(_timer, _comboLength); // sending timer and cobmo length data
		}
	}
}

another script

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class fight_combo : MonoBehaviour {

	// author: Kamil104
	// http://answers.unity3d.com/users/225838/kamil1064.html
	public Image[] images; // imgaes to show which buton you need to press
	public KeyCode[] keys; // keys you need to press
	private float timer; // time which you have to press
	private float sorryTimer = 0;
	private int current;
	private int comboLength; // how many times you need to press correct key
	private int currentCombo = 0;

	// Update is called once per frame
	void Update () {
		if(Time.time > sorryTimer)
		{
			SetTimer(false);
		}
		if(Input.anyKeyDown)
		{
			if(Input.GetKeyDown(keys[current]))
			{
				SetTimer(true);
			}else
			{
				print("wrong key was pressed");
				gameObject.SetActive(false);
			}
		}

		images[current].fillAmount = (sorryTimer - Time.time) / timer;
	}
	public void SetTimer(bool fastEnough)
	{
		if(currentCombo >= comboLength)
		{
			images[current].fillAmount = 0;
			print("you won :)");
			// do some stuff here
			gameObject.SetActive(false);
		}
		else
		{
			sorryTimer = Time.time + timer;
			images[current].fillAmount = 0;
			current = Random.Range(0, images.Length);
			if(!fastEnough)
			{
				print("You are too slow, you died");
				// and do die stuff here
				gameObject.SetActive(false);
			}else
				print("Nice");
			currentCombo ++;
		}
	}
	public void SetUpGame(float my_timer, int my_comboLength)
	{
		timer = my_timer;
		comboLength = my_comboLength;
		sorryTimer = Time.time + timer;
		currentCombo = 1;
	}
}

Just drag and drop them right and set up like in screenshot :slight_smile: