Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Justaway · Mar 18, 2016 at 12:45 PM · c#gamecombatcombo

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 :)

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Justaway · Mar 19, 2016 at 04:46 PM 0
Share

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.

unity-personal-64bit-rpgunity-new-unity-project-1.jpg (247.8 kB)
avatar image Kamil1064 Justaway · Mar 19, 2016 at 05:26 PM 1
Share

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

avatar image Justaway Kamil1064 · Mar 19, 2016 at 05:33 PM 0
Share

@$$anonymous$$amil1064, Yeah, it triggers fine now, but all of my buttons appear ins$$anonymous$$d of one by one

Show more comments
avatar image
0

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 :)

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Justaway · Mar 18, 2016 at 08:54 PM 0
Share

Thanks for the tips, I'll try it out when I can (probably tomorrow)

avatar image Justaway · Mar 19, 2016 at 09:40 AM 0
Share

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.

avatar image Justaway · Mar 19, 2016 at 01:05 PM 0
Share

@$$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);
         }
 
     }
 }
avatar image Kamil1064 Justaway · Mar 19, 2016 at 02:23 PM 1
Share

Hi, @Justaway, I'm making an example now :)

avatar image Justaway Kamil1064 · Mar 19, 2016 at 02:31 PM 0
Share

@$$anonymous$$amil1064, thanks!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges