Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by starbreaker772 · Feb 17, 2015 at 01:21 PM · inputcomboincrementconditional

Key Combo system. where I am going wrong with this?

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?

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

Answer by hexagonius · Feb 17, 2015 at 01:33 PM

the problem is, that you created a 5th instance of KeyCombo besides your existing 4 and check its index variable, but instead you should check the currentindex on the ones you're using.

So instead of

 if (punch1.Check() && keycom.currentIndex == 0)

do

 if (punch1.Check() && punch1.currentIndex == 0)
Comment
Add comment · Show 1 · 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 starbreaker772 · Feb 17, 2015 at 02:32 PM 0
Share

I can see what you're getting at with referencing the actual instance but even that doesn't seem to work, sorry. Unless the currentindex is used for something else within the code I can't see why the value isn't incrementing.

avatar image
0

Answer by J0hn4n · Jan 20, 2017 at 04:54 PM

Maybe its late but you should use StateMachineBehaviour, when you enter on atack anim, starts a timer, if you dont hit atack button on that the time of the atack state, timer = 0;

Comment
Add comment · 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

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

Help me with the script on doing combo atk 3 Answers

What is the best way to store User Input and use it later with the new input system? 0 Answers

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

transform.rotation increment slows to a stop 1 Answer

multiple inputs 2 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