# How do I make a simple button mashing mechanic script?

Hello, everyone. New member here.

I want to make a simple arm wrestling game where people would need to mash as much as they can to defeat the other.

At first my idea for the script was something like this:

The idea being that the when a player presses the mash button it will be added to a counter, and then it will be subtracted by the other player’s mash counter.

The problem with this is that it doesn’t seem optimal. For example, if both players stop pressing the buttons, then the player with the highest mash counter will win because there will be nothing to subtract from.

TL;DR: How do I make a good button mashing script?

Well, it is clear that you must hate keyboards.

How about (for each player) calculating the time since the last button press, and then based on how long it has been, give this player that much arm progress.

I would use something non-linear, like perhaps 1.0f divided by the time between mashes, as long as you make sure that time is nonzero.

That way really fast mashing is rewarded at lot more.

Take that produced number and add it to the one player’s bar and remove it from the other player’s bar, and whoever reaches zero first, they lose.

To make it smoother, store up the “progress” each player accumulates and then “apply” it slowly based on the difference between the stored values, to keep the arm from jerking too much with each tap.

Kurt

1 Like

Use Mathf.MoveTowards to move a player’s meter slowly towards 0. Every time they press the button, up it by a specific amount. If both players stop button mashing, their meters will both go to zero and it will be a draw.

If you want to get fancy, add less to a player’s meter the closer it is to being full. You can pretty much make it impossible to keep the meter full to encourage more button mashing.

1 Like

Destroying a keyboard sounded kinda fun so I decided to try my hand at making an Arm Wrestling button mashing script.

Attached please find what I came up with. It’s a full Unity3D project, but the interesting stuff is two scripts, a game script and a player script, and you just have to hang the game script on your camera and off you go.

Kurt

1817917–116322–forum275355.zip (12.5 KB)

1 Like