Search Unity

Detecting when player taps a certain amount of times in quick succession?

Discussion in 'Scripting' started by fazraz, Dec 7, 2017.

  1. fazraz

    fazraz

    Joined:
    Feb 27, 2015
    Posts:
    14
    I'm developing a platformer game where each screen tap moves the player forward one space. I'd like to be able to detect when a user moves forward many times in quick succession so that I can activate a bonus multiplier for them. E.g. the user makes 10 moves very quickly one after another. How could I go about scripting this?

    Many thanks,
    Freddy
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Have an int that you increment for each tap. Start/restart a timer after each tap. If the timer expires, then reset the tap count. Otherwise, check the tap count after you increment it to see if you have the desired number to do a multiplier.

    And of course, reset the multiplier if the timer expires or whatever else you need to do.

    As for scripting, start with the first step. Register tap counts. Then add the check to see if you should give a multiplier. Then add the timer. One step at a time and you'll get there.
     
    fazraz likes this.
  3. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    here is an idea I came up with to help you out.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class BonusClick : MonoBehaviour
    4. {
    5.  
    6. public float timeForBonus = 5f;
    7. public int clicksNeededForBonus = 10;
    8. private float timeHasRun = 0f;
    9. private int numberOfClicks = 0;
    10.  
    11.    
    12.         void Update()
    13.     {
    14.         if (Input.GetMouseButtonDown(0))
    15.         {
    16.             numberOfClicks++;
    17.             if(numberOfClicks == 1)
    18.             {
    19.                 StartCoroutine(MultiClickTimer());
    20.             }
    21.            
    22.             //do some movement code
    23.         }
    24.     }
    25.     IEnumerator MultiClickTimer()
    26.     {
    27.         while(timeHasRun < timeForBonus)
    28.         {
    29.             timeHasRun = timeHasRun + Time.deltaTime;
    30.            
    31.             //check for number of clicks
    32.             if(numberOfClicks == clicksNeededForBonus )
    33.             {
    34.                 //put code here that that gives the bonus
    35.                 Debug.Log("Bonus !!!");
    36.             }
    37.            
    38.             yield return null;
    39.         }
    40.         //reset timer
    41.         timeHasRun = 0;
    42.         numberOfClicks = 0;
    43.     }
    44. }
     
    fazraz likes this.