Search Unity

How do I make a simple button mashing mechanic script?

Discussion in 'Scripting' started by Arm_Wrestling, Oct 22, 2014.

  1. Arm_Wrestling

    Arm_Wrestling

    Joined:
    Oct 22, 2014
    Posts:
    1
    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?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    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
     
    Arm_Wrestling likes this.
  3. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    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.
     
    Arm_Wrestling likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    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
     

    Attached Files:

    Arm_Wrestling likes this.