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.
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): var isDown = false; function Update(){ if(Input.GetButton("AttackModifier")){ isDown = true; }else if(Input.GetButtonUp("Shift")){ isDown = false; } if(Input.GetButtonDown("Attack")){ if(isDown){ //Fire a ranged weapon }else{ //Hit with a melee weapon }} } 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.
Do you mean like this? Code (csharp): function Update(){ if( Input.GetButton("Modifier") Input.GetButtonDown("Fire1") ){ //I am holding down modifier and pushed Fire1 } }
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): }esle if(!Input.GetButton("AttackModifier")){ //the button ISN'T down 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?
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.
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?
Sorry, I see what your saying. I think something like this would work? Code (csharp): public var timeAllowedToChain : float = 0.5; private var lastPressFire1 : float = 0.0; function Update(){ if(Input.GetButtonDown("Fire1")){ lastPressFire1 = Time.time; } if(Input.GetButtonDown("Fire2") (Time.time <= (lastPressFire1 + timeAllowedToChain))){ print("successfully chained."); } } 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.
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): var keyPressTimer : float = 0.3; private var cur : String = ""; private var lastPress : float = 0.0; function Update(){ if(Input.GetKey("w"))CheckKey("w"); if(Input.GetKey("a"))CheckKey("a"); if(Input.GetKey("s"))CheckKey("s"); if(Input.GetKey("d"))CheckKey("d"); } function CheckKey(k : String){ if(Time.time - lastPress <= keyPressTimer){ cur+=k; } else { cur=k; } lastPress=Time.time; CheckCombos(); } function CheckCombos(){ if(cur.SubString(cur.Length-3)=="was") StartCoroutine(DoCombo1()); }
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): using UnityEngine; using System.Collections; public class KeyCombo { public float maxPause = 1.0f; public string[] keys; public string message; private int progress = 0; public void Check (MonoBehaviour owner) { if (progress == 0 Input.GetKey (keys[0])) // Start tracking keys if the first key is pressed { owner.StartCoroutine (Run (owner)); } } IEnumerator Run (MonoBehaviour owner) { float start; while (Application.isPlaying) { if (progress >= keys.Length) // If we typed all the keys, send the message and return { owner.SendMessage (message); progress = 0; return; } else if (Input.GetKey (keys[progress])) // If the next key was pressed, move forward and reset the timer { progress++; start = Time.time; } else if (Time.time - start > maxPause || // Reset and return if we time out ( Input.anyKey !( Input.GetMouseButton (0) || Input.GetMouseButton (1) || Input.GetMouseButton (2) ) ) // Or if an incorrect key was typed // NOTE: Might want to expand what is allowed here ) { progress = 0; return; } yield return null; } } } public class ComboKeys : MonoBehaviour { public KeyCombo[] combos; void Update () { foreach (KeyCombo combo in combos) { combo.Check (); } } }
Adding to a string can work, like bigmisterb wrote, but make sure to use input.getkeyDOWN, (minus the caps). Otherwise you get screwy results.