2

So, I'm trying to make my unity game go from movement using the keyboard to movement using onscreen ui buttons. I used the built in ui buttons in unity, linked the button to the object I'm trying to move, and had it trigger a section of a script to move the object. The problem is it only detects the input of pressing the button at first, how do you detect the button being held down?

2 Answers2

1

You can check button being held down by check when button is released

First add the Event Trigger component to your button, add two event PointerDown and PointerUp, then set a bool to true on PointerDown, and false on PointerUp, that bool is whether button being held down, e.g:

 public class CheckHeldDown : Monobehaviour
 {
     public bool isHeldDown = false;
 
     public void onPress ()
     {
         isHeldDown = true;
         Debug.Log(isHeldDown);
     }
 
     public void onRelease ()
     {
         isHeldDown = false;
         Debug.Log(isHeldDown);
     }
 }
Mr. For Example
  • 4,173
  • 1
  • 9
  • 19
-1

If you want to build your game on mobile, you can Detect it with the amount of touches. For example this is some code I just used to make a Nitro button in a racing game:

public class NitroButton : MonoBehaviour
{
    [HideInInspector]
    public bool pushed;

    public void Update()
    {
        if (Input.touchCount >= 2)
        {
            pushed = true;
        }
        else
        {
            pushed = false;
        }
    }
}

This also allows for two finger pushes and such.