Hi. I’m making a android 2D pong game for practice. I used UI buttons as arrows to move the paddle. The game is working except that when i click the button, the paddle moves in that direction continuously. How to move the paddle only a small distance per UI button click?
This is my script attached to the buttons.
public class MoveRacketDown : MonoBehaviour
{
[SerializeField]
private GameObject racket;
public float speed = 10f;
void Start()
{
gameObject.GetComponent<Button>().onClick.AddListener(MoveRacketOnClick);
}
void MoveRacketOnClick()
{
racket.GetComponent<Rigidbody2D>().velocity = new Vector2(0, -1) * speed;
}
}