- Home /
 
[C#] Both button check with delay.
Does anybody know how to check if both buttons are pressed?
Where if one button is pressed, it waits for a short time until the other button is pressed before knowing that both buttons are pressed, otherwise, that button press is going to be registered as a single button press.
It's mainly for timing issues in a mobile game, the buttons used are UI buttons.
Thanks in advance!
Answer by SmokeyWitch · Apr 04, 2020 at 09:42 AM
@it_master you can use flag ( bool ) to check.
   public class Example : MonoBehaviour
     {
         public Button btn1;
         public Button btn2;
        bool btn1Pressed = false;
        bool btn2Pressed = false;
         void Start()
         {
             btn1.AddListener(MyAction1);
             btn2.AddListener(MyAction2);
         }
     
         void MyAction2()
         {
         btn2Pressed  = true;
             if(btn1Pressed )
            {//both pressed
     
             btn1Pressed = btn2Pressed = false;
             }
         }
     
         void MyAction1()
         {
            btn1Pressed  = true;
             if(btn2Pressed )
            {//both pressed
     
             btn1Pressed = btn2Pressed = false;
             }
         }
 
     }
 
              But I may need to introduce a delay, say once a press a button, I only have a short amount of time before I press the other button so that it is recognized as I have pressed both buttons.
@it_master add a delay float and IEnumerator
  public class Example : $$anonymous$$onoBehaviour
 {
              public Button btn1;
              public Button btn2;
             public float delay = 5;
             bool btn1Pressed = false;
             bool btn2Pressed = false;
              void Start()
              {
                  btn1.AddListener($$anonymous$$yAction1);
                  btn2.AddListener($$anonymous$$yAction2);
              }
          
              void $$anonymous$$yAction2()
              {
              btn2Pressed  = true;
              StartCoroutine(WaitRoutine());
              }
          
              void $$anonymous$$yAction1()
              {
                 btn1Pressed  = true;
                  StartCoroutine(WaitRoutine());
              } 
     
             IEnumerator WaitRoutine()
            {
                      float timer = 0;
                        while(true)
                          {
                               timer+= Time.deltaTime / delay; 
                               if(btn1Pressed   && btn2Pressed )
                                 { 
                                      //both pressed in time.
                                       btn1Pressed  = btn2Pressed = false;
                                       yield break;
                                  } 
                                if(timer >= 1)
                                 {
                                   // times up
                                   btn1Pressed  = btn2Pressed = false;
                                    yield break;
                                  }
     
     
                               }
                        }
      
 }
 
                  Your answer
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Need help in Stone throwing mechanic with aim in unity 3d C# 0 Answers
Need help in the stone throwing mechanic with aiming for mobile in Unity3d 1 Answer
Need help in the stone throwing mechanic with aiming for mobile in Unity3d 1 Answer
Can anyone help with loading issues when button is clicked? 0 Answers