How to make a UI button for movement?

Hello, I’m trying to make a simple mobile game of a ball that rolls across a scenery. But I’m having trouble making a UI movement button.
The button I made worked, making the ball roll to the left and right using AddForce to Rigidbody in the script, but it’s a ‘‘click’’ button, not a ‘‘hold’’ one like in normal games. To make the ball move constantly the player has to keep taping the move button, and that’s a bit strange.
The question is: How can I make a button that when it’s pressed it keeps moving the ball?

Here’s a image of what I got in the game:

The screenshot is not helpful without the code that moves the ball. It would seem the script only adds force once when the button is pressed and not continuously while the button is pressed. If you post the script that does the adding of force it would be easier to tell you exactly whats happening. I too have chosen a roll a ball for mobile as my first entry into unity, seems overly common. I have made it a little farther into the control scheme than you I think and although im a complete noob I should have fresh memory of your exact problems. Hope I can help. BTW have you started with the jump script yet and if so how does your ball decide which way to jump? I have gone round with several ideas and methods for that and finally settled on one. I dont know if its the best from a programmers perspective but it works great in game.

Edit spelling because I’m awful in that department.

Don’t populate the OnClick() event in the inspector for the buttons. Instead, add an Event Trigger component for each button, and for each event trigger, click the Add New Event Type button and add a PointerDown and a PointerUp event. In the function that you supply to the PointerDown event, you could set a ‘move’ bool to true, and do the opposite for the PointerUp event. In your FixedUpdate function, make the ball move or stop depending on the value of the move variable.

1 Like