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

How can i add a little delay my button when i click it?

Discussion in '2D' started by Cansix1, Oct 15, 2020.

  1. Cansix1

    Cansix1

    Joined:
    Apr 30, 2020
    Posts:
    2
    Hi. I change Input.GetKeyDown(KeyCode.E) to a bool for my created button in public void GetKeyE() function in my script and i add Event Trigger and add a Pointer Down and Pointer Up then association GetKeyE() function. but i have a problem. When i click to button it repeat itself one or two more time. It detects to click too fast. First i've tried StartCoroutine in my script but it's not perform well. I set up my button in Inspector, Here is my script.
    Code (CSharp):
    1.       public GameObject Player2;
    2.       public Transform itemBtransfom;
    3.       public GameObject itemB;
    4.       public GameObject itemA;  
    5.       public void GetKeyE()
    6. {
    7.      StartCoroutine(GetKeyEButton());
    8. }
    9. IEnumerator GetKeyEButton()
    10. {
    11.      yield return new WaitForSeconds(0.2f);
    12.      eKeyIsPressed = !eKeyIsPressed;
    13. }
    14. void OnTriggerStay2D(Collider2D other)
    15.       {
    16.  
    17.  
    18.  
    19.           if (eKeyIsPressed && other.tag == "Player1")
    20.           {
    21.  
    22.               other.gameObject.SetActive(false);
    23.               Player2.SetActive(true);
    24.               itemA.transform.position = itemB.transform.position;
    25.               itemB.transform.position = itemBtransform.transform.position;
    26.  
    27.  
    28.           }
    29.  
    30.       }
    After i've tried this in my script but this is not work too.

    Code (CSharp):
    1.      float time;
    2.      float delay = 2f;
    3.      float buttontime;
    4. public void GetKeyE()
    5.      {
    6.          time = Time.unscaledTime;
    7.          buttontime =  Time.unscaledTime + delay;
    8.          if (time> buttontime)
    9.          {
    10.                  eKeyIsPressed = !eKeyIsPressed;  
    11.          }
    12.    
    13.      }
    My question is how can i add a little delay my button when i click it? Thanks for answer in advance.
     
  2. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    Can you use button up instead? If not then mark the button clicked and use a timer to count a certain amount of time before registering.
    Code (CSharp):
    1. bool buttonPressed = false;
    2. float timer  = 0;
    3. float duration = 0.25f; //quarter second
    4. void Update()
    5. {
    6.     if(buttonPressed)
    7.     {
    8.         timer+=Time.deltaTime;
    9.         if(timer>=duration)
    10.         {
    11.             timer = 0;
    12.             buttonPressed = false;
    13.             //Run button press
    14.         }
    15.     }
    16. }
     
  3. Cansix1

    Cansix1

    Joined:
    Apr 30, 2020
    Posts:
    2
    It's work. Thanks so much