- Home /
How can I trigger booleans in a specific order?
I have 3 booleans which are each triggered by a different key press setup via the Input settings.
When the W or Up Arrow key is pressed, tapW = true
When the A of Left Arrow key is pressed, tapA = true
If both tapW & tapA are true the third bool, tapAW = true
This is good and all but I would like the tapAW bool to become true only if the A key is pressed first followed by the W key (hence the bool name "tapAW"). At the moment tapAW returns true whether the combination W then A is pressed, or A then W is pressed, and I would like to trigger tapAW only when A is first followed by W. I've attached the relevant snippet of my simple script below. How could I do this?
public bool tapW = false;
public bool tapA = false;
public bool tapAW = false;
public bool tapWA = false;
//In the Update function
if (Input.GetButtonDown ("Walk") && !tapW) {
tapW = true;
}
if (Input.GetButtonDown ("WalkLeft") && !tapA) {
tapA = true;
}
if (tapA && tapW) {
tapA = false;
tapW = false;
tapAW = true;
}
Answer by suresh.murugesan · Nov 01, 2012 at 05:48 AM
you check the time difference of first and second button pressed then you can solve this problem . . .
Yes that makes sense. I am using a timer for the double tap functionality, but that is a different implementation all together. Could you provide an example of what you mean?
Answer by swatmaster69 · Nov 01, 2012 at 05:48 AM
You could put the buttons in an array, then search it. Of course, that would get ugly fast if you plan to use more than two keys. Something like this:
ArrayList taps = new ArrayList();
void Update()
{
tapW = tapA = tapWA = tapAW = false;
if (Input.GetButtonDown("W"))
taps.Add('W');
else if (Input.GetButtonDown("A"))
taps.Add('A');
if (taps.Count == 1)
{
if (taps[0] == 'W')
tapW = true;
else
tapA = true;
}
else if (taps.Count == 2)
{
if (taps[0] == 'W' && taps[1] == 'A')
tapWA = true;
else
tapAW = true;
}
else if (taps.Count == 3)
taps.Clear();
}
Yes I am planning to use more than two keys down the track. Thanks for the suggestion anyway ;)
So are you trying to decide whether the player entered a valid combo or something? If so, you could make a list of $$anonymous$$eyCodes and run one or more for loops comparing that list to lists of valid combos.
If not, what are you trying to do? Some more details might be helpful.
Your answer
![](https://koobas.hobune.stream/wayback/20220613083157im_/https://answers.unity.com/themes/thub/images/avi.jpg)