Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Button Combos

Discussion in 'Scripting' started by HolBol, Jul 22, 2011.

  1. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    Well, I've been searching for this, and couldn't find an answer. How would I go about making button combos? Like one button/trigger pulled and then another button pressed?

    And as a side Q: How could I go about scripting the xbox 360's triggers separately? I know they return -1 and 1, depending on the trigger, and I've got them to work separately, I just need to find out how to get both numbers at the same time.
     
  2. Akinon93

    Akinon93

    Joined:
    Jan 13, 2011
    Posts:
    185
    I'm not very good with scripting in unity yet myself, but I have an idea for the first part of your question.

    You could make a variable (like isDown or w/e) and set to false, and run an if statement (in your update function, of course) to check if the modifier button (say RT or LT or w/e you want), and if it is pressed, change isDown to true. when released, change isDown to false. then when you have your variables for attacking (say the X button is attack), you do an if else statement to change what that button does depending on whether the variable isDown is true or not.

    for example

    Code (csharp):
    1.  
    2.  
    3. var isDown = false;
    4.  
    5. function Update(){
    6.  
    7. if(Input.GetButton("AttackModifier")){
    8. isDown = true;
    9. }else if(Input.GetButtonUp("Shift")){
    10. isDown = false;
    11. }
    12.  
    13. if(Input.GetButtonDown("Attack")){
    14. if(isDown){
    15. //Fire a ranged weapon
    16. }else{
    17. //Hit with a melee weapon
    18. }}
    19.  
    20. }
    21.  
    22.  
    say AttackModifier is set to Shift and Attack is set to Left Mouse Button.

    idk if this would work as you'd like but it's what I thought of, I put comments in there because idk what you want to change when the modifier button is pressed down

    Edit: or if I misunderstood the question, sorry, lol. kinda tired sometimes my brain doesn't work right when I am.
     
  3. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Do you mean like this?

    Code (csharp):
    1.  
    2. function Update(){
    3.     if( Input.GetButton("Modifier")  Input.GetButtonDown("Fire1") ){
    4.         //I am holding down modifier and pushed Fire1
    5.     }
    6. }
    7.  
     
  4. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    This isn't what I need, as Legend411 does it in a similar way. However if you were to use this script, use Legend411's, or change your GetButtonUp line to
    Code (csharp):
    1.  
    2. }esle if(!Input.GetButton("AttackModifier")){
    3. //the button ISN'T down
    4.  
    Well, not really. I am able to do that part, I'm just stuck on pressing one, then another, and then the action plays, rather than both at the same time. Also, any thought on the 360 triggers?
     
    Last edited: Jul 22, 2011
  5. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    The long way is to write in your Update to catch when the player presses button 1. Then registers the amount of time it takes him to press 2 and again for 3 (if you have a 3)

    So write your keys out and say button1down=Input.GetButtonDown("Fire1") and so on

    then write some code that checks for combos.

    if(button1down) combo1button1downtime=Time.time;
    if(button2down combo1button1downtime-Time.time > 0.2) combo1button2downtime=Time.time;
    if(button3down combo1button2downtime-Time.time > 0.2) StartCoroutine(doCombo1());

    This way, you press button1 it holds the time, button2, it holds it's time and waits to see if you have pressed button3 down within another 0.2 seconds.. after that it starts the action.

    If you press 1... then 3, it will not have held button2's time... so it wont work.
     
  6. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    ok, I kinda get it. I'm not actually sure how to order and implement it in a working scenario. Do you mind elaborating for me?
     
  7. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    Boing EDIT: Again.
     
    Last edited: Jul 23, 2011
  8. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    again
     
  9. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    COme on people, a little help loL!

    Wait, I may have it....
     
  10. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Sorry, I see what your saying. I think something like this would work?

    Code (csharp):
    1.  
    2.  
    3. public var timeAllowedToChain : float = 0.5;
    4. private var lastPressFire1 : float = 0.0;
    5.  
    6. function Update(){
    7.    
    8.     if(Input.GetButtonDown("Fire1")){
    9.         lastPressFire1 = Time.time;
    10.     }
    11.    
    12.     if(Input.GetButtonDown("Fire2")  (Time.time <= (lastPressFire1 + timeAllowedToChain))){
    13.         print("successfully chained.");
    14.     }
    15.    
    16. }
    17.  
    18.  
    Capture the timestamp of the first button, then check to see if the second button was pressed within a certain window of time after the timestamp to know if the user chained the keypresses in time.
     
  11. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Wait wait... I think I may have a better solution... After thinking about it some. (and seeing another thread where someone asked something very obscure got me to to hit this right on the head)

    OK, think of it this way. You press a key that is part of what could be a combo. Say the down arrow. it writes a letter to a string... "D" and keeps the time pressed by the user. Say 1.0. The user presses a new key, and it checks to see if it is within the check time. Say 0.5 seconds. If it is, it adds the new key say, up.. or "U" to the string. Again, the user presses a key and it is within the timeframe and it adds it to the string. Say, right or "R". After each keypress, it reads the last "how many ever" keys and checks them against your combo. If it has one it does the combo and clears the string.

    Code (csharp):
    1.  
    2. var keyPressTimer : float = 0.3;
    3. private var cur : String = "";
    4. private var lastPress : float = 0.0;
    5.  
    6. function Update(){
    7.     if(Input.GetKey("w"))CheckKey("w");
    8.     if(Input.GetKey("a"))CheckKey("a");
    9.     if(Input.GetKey("s"))CheckKey("s");
    10.     if(Input.GetKey("d"))CheckKey("d");
    11. }
    12.  
    13. function CheckKey(k : String){
    14.     if(Time.time - lastPress <= keyPressTimer){
    15.         cur+=k;
    16.     } else {
    17.         cur=k;
    18.     }
    19.     lastPress=Time.time;
    20.    
    21.     CheckCombos();
    22. }
    23.  
    24. function CheckCombos(){
    25.     if(cur.SubString(cur.Length-3)=="was")
    26.         StartCoroutine(DoCombo1());
    27. }
    28.  
     
  12. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    This might be interesting:
    http://answers.unity3d.com/questions/9299/how-could-i-implement-cheat-codes-in-my-game.html

    Otherwise, here's another example which should have a bit broader use (untested):
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class KeyCombo
    6. {
    7.     public float maxPause = 1.0f;
    8.     public string[] keys;
    9.     public string message;
    10.    
    11.    
    12.     private int progress = 0;
    13.    
    14.    
    15.     public void Check (MonoBehaviour owner)
    16.     {
    17.         if (progress == 0  Input.GetKey (keys[0]))
    18.         // Start tracking keys if the first key is pressed
    19.         {
    20.             owner.StartCoroutine (Run (owner));
    21.         }
    22.     }
    23.    
    24.    
    25.     IEnumerator Run (MonoBehaviour owner)
    26.     {
    27.         float start;
    28.         while (Application.isPlaying)
    29.         {
    30.             if (progress >= keys.Length)
    31.             // If we typed all the keys, send the message and return
    32.             {
    33.                 owner.SendMessage (message);
    34.                 progress = 0;
    35.                 return;
    36.             }
    37.             else if (Input.GetKey (keys[progress]))
    38.             // If the next key was pressed, move forward and reset the timer
    39.             {
    40.                 progress++;
    41.                 start = Time.time;
    42.             }
    43.             else if (Time.time - start > maxPause ||
    44.             // Reset and return if we time out
    45.                 (
    46.                     Input.anyKey
    47.                     !(
    48.                         Input.GetMouseButton (0) ||
    49.                         Input.GetMouseButton (1) ||
    50.                         Input.GetMouseButton (2)
    51.                     )
    52.                 )
    53.                 // Or if an incorrect key was typed
    54.                 // NOTE: Might want to expand what is allowed here
    55.             )
    56.             {
    57.                 progress = 0;
    58.                 return;
    59.             }
    60.            
    61.             yield return null;
    62.         }
    63.     }
    64. }
    65.  
    66.  
    67. public class ComboKeys : MonoBehaviour
    68. {
    69.     public KeyCombo[] combos;
    70.    
    71.    
    72.     void Update ()
    73.     {
    74.         foreach (KeyCombo combo in combos)
    75.         {
    76.             combo.Check ();
    77.         }
    78.     }
    79. }
     
  13. Bloodyem

    Bloodyem

    Joined:
    Mar 26, 2013
    Posts:
    8
    Adding to a string can work, like bigmisterb wrote, but make sure to use input.getkeyDOWN, (minus the caps). Otherwise you get screwy results.
     
  14. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    thanks captain necro
     
    Oliveram1 and HolBol like this.