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: https://youtu.be/4qiFchj6BBc?t=631
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.
Answer by Kamil1064 · Mar 19, 2016 at 03:50 PM
@Justaway, here is video showing how it's working: https://www.youtube.com/watch?v=aZWMV-C-180&feature=youtu.be 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. http://i.imgur.com/qtp4Kf6.jpg
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 :)
That's a really detailed answer, even a video! Thank you so much, but... Sorry for my stupidity, the one thing not working for me is showing the keys. When I press Q, I see all of my buttons appear, not only those that needed for combo. $$anonymous$$y buttons are simple images with no animation like yours, so maybe this is the reason? But when I press buttons I can see log in console so it's definitely working. Now I need to figure out how to make only needed buttons appear (and disappear), and only on collision with enemy. Edit: I made buttons filled and it's working, now the only thing I need is to change trigger from button to collision with enemy.
That was example to show time is runing out, I could say how to achieve this but you made it so it's ok :)
Here I made this with button but you can make it with trigger just modify first script and add it to trigger element:
public void OnTriggerEnter(Collider col)
{
if(col.tag == "Player" && comboPanel.activeSelf == false) // if you enter trigger 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
}
}
or collider:
public void OnCollisionEnter(Collision col)
{
if(col.tag == "Player" && comboPanel.activeSelf == false) // if you enter trigger 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
}
}
This will work only if player enter (not enemy), just be sure player have got tag "Player". P.S. if this is correct answer for you please accept this answer as correct, this will hepl other people if they gonna have the same problem ;) To make appear only needed button turn fill amount to zero as default, script will appear right as you may see in video
@$$anonymous$$amil1064, Yeah, it triggers fine now, but all of my buttons appear ins$$anonymous$$d of one by one
Answer by Kamil1064 · Mar 18, 2016 at 01:44 PM
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 :)
Thanks for the tips, I'll try it out when I can (probably tomorrow)
Sorry @$$anonymous$$amil1064, I'm still new to Unity, how do I add these variables in script? I added buttons to the project(as UI image), but don't know how to add them in script.
@$$anonymous$$amil1064, I have dont something different from your method, because I didn't quite understood what I have to do.
What I have now is 2 scripts. One is making selected buttons visible (they're already here, but disabled), and the other is making them disappear after successful combo.
But the problem is buttons disapper only when full combo is completed, rather than disappearing one by one after pressing the right button.
using UnityEngine;
using System.Collections;
using kaiyum.control;
using UnityEngine.Scene$$anonymous$$anagement;
using System;
using UnityEngine.UI;
public class doCombo : $$anonymous$$onoBehaviour {
public Image Button_W, Button_A, Button_S, Button_D;
private comboSystem niceCombo= new comboSystem(new $$anonymous$$eyCode[] {$$anonymous$$eyCode.W,$$anonymous$$eyCode.A,$$anonymous$$eyCode.S});
void Update ()
{
if(niceCombo.check())
{
Button_W.CrossFadeAlpha (0, .1f, false);
Button_A.CrossFadeAlpha (0, .2f, false);
Button_S.CrossFadeAlpha (0, .3f, false);
}
}
}
Your answer
Follow this Question
Related Questions
Setting up a attack button 0 Answers
Anyone wanna help? 0 Answers
I can't make transition from actual animation to previous one 1 Answer
Score won't work 1 Answer
I don't know how to create a highscore gui text and display it at death screen 0 Answers