Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. Unity 2017.2 is now released.
    Dismiss Notice
  3. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  4. If you're a macOS 10.13 High Sierra user take be sure to read this before updating Unity.
    Dismiss Notice
  5. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  7. Unity 2017.3 beta is now available for download.
    Dismiss Notice

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:
    6
    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:
    2,880
    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:
    677
    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.