Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[C#] Both button check with delay.

Discussion in 'Scripting' started by it_master, Apr 4, 2020.

  1. it_master

    it_master

    Joined:
    Jul 25, 2015
    Posts:
    13
    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!
     
  2. panim0_unity

    panim0_unity

    Joined:
    Nov 13, 2018
    Posts:
    47
    I think you can use OnPointerDown() for this

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.EventSystems;
    4. public class MyButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
    5. public bool buttonPressed;
    6. public void OnPointerDown(PointerEventData eventData){
    7.      buttonPressed = true;
    8. }
    9. public void OnPointerUp(PointerEventData eventData){
    10.     buttonPressed = false;
    11. }
    12. }
     
  3. it_master

    it_master

    Joined:
    Jul 25, 2015
    Posts:
    13
    But I kinda need a time delay for this, since the case is when I press a button, I have some small amount of time before I press the other button so that it can be registered as a both
     
  4. panim0_unity

    panim0_unity

    Joined:
    Nov 13, 2018
    Posts:
    47
    well this isn't a good way but just a quick thought of mine, you can try coroutine, start it when pressed first button and inside coroutine check for another bool from second button and execute line based on that condition. I might be wrong tho