- Home /
Trying to make touch/holding screen not toggle menu.
Hi folks
I'm trying to make a simple mobile app that that'll toggle a menu when the user only "taps" the screen, but not when exceeding a tap. The debug logs tell me that the time exceeding a "tap" and a hold (3 seconds) is at least being recognised.
I've tried using "if (menuStatus == true && acumTime <= nonTapHoldTime)" but it's like Unity doesn't even acknowledge these two conditions, the menu continues to be toggled when the screen is tapped but also on release from a hold.
Could someone work out what it is I am doing wrong? Any help is appreciated.
My Code...
{ private bool menuStatus = true; public GameObject Menu; private float shutdownHoldTime = 3f; // Time to trigger a hold private float nonTapHoldTime = 0.15f; // When a touch exceeds a tap, but is not a "hold" private float acumTime = 0; // Time to start/restart hold
private void Update ()
{
// Record a screen tap.
if (Input.touchCount > 0)
{
acumTime += Input.GetTouch(0).deltaTime;
Touch touch = Input.GetTouch(0);
// Handle finger movements based on touch phase.
switch (touch.phase)
{
// Record initial release.
case TouchPhase.Began:
break;
// Record touch release.
case TouchPhase.Ended:
acumTime = 0;
menuStatus = !menuStatus;
break;
}
if (acumTime >= nonTapHoldTime && acumTime <= shutdownHoldTime && menuStatus == true) // If screen is touched longer than a tap
{
//Not a tap
Debug.Log("Not a tap");
}
else if (acumTime >= shutdownHoldTime && menuStatus == true) // If screen is held while menu is up
{
//Long tap
Debug.Log("Hold");
}
}
// Menu appears when hidden
if (menuStatus == true && acumTime <= nonTapHoldTime)
{
Menu.gameObject.SetActive(true);
}
// Menu dissappears when shown
else if (menuStatus == false && acumTime <= nonTapHoldTime)
{
Menu.gameObject.SetActive(false);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612181258im_/https://answers.unity.com/themes/thub/images/avi.jpg)