Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Albert-han · Oct 19, 2014 at 03:37 PM · tap

Count Taps per second

Hi guys im trying to implement character speed into my game by determining the amount of taps per second the user does.

Since i cant find anything i could i put together at the docs i went to google it and only found 1 result.

 private List<float> taps = new List<float> ();
 public float tapsPerSecond;
  
 void Update () {
     if (<tap touch idk, but u know>) {
         taps.Add(Time.timeSinceLevelLoad);
     }
     for (int i = 0, i < taps.Count; i++) {
         if (taps[i] <= Time.timeSinceLevelLoad-1) {
             taps.Remove(i);
         }
     }
     tapsPerSecond = taps.Count;
 }

Could someone help me explain this script to me or help me make a simple script that i could work with.I will try to help as much as i can.Thanks

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image richyrich · Oct 19, 2014 at 03:51 PM 0
Share

You could add a check at the beginning of each Update to see if the time had elapsed...

If not enough time had passed, 'if (Input.any$$anonymous$$ey)' would detect a key press

If enough time had passed, you could then output the number of taps

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by robertbu · Oct 19, 2014 at 03:55 PM

To start with, replace line 5 with:

  if (Input.GetMouseButtonUp(0)) {

Now you can play with this code in the Editor. For basic use in touch, you can replace this line by:

   if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Ended)) {

But this line does not handle multiple touches. It does not distinguish between a tap, a press, or a swipe. So how much additional work you need to do will depend on 1) your definition of touch, and 2) how you want to deal with multiple touches.

As for how this code works, each time it detects a touch, it puts the time of that touch in a list. Lines 8 through 12 comb through that list and remove any that are over one second old. The count of the remaining entries in the list will be the touches per second.

Here is your code modified to handle multiple touches, but it does not distinguish between tap, press, swipe, etc.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 class Example : MonoBehaviour {
     private List<float> taps = new List<float> ();
     public float tapsPerSecond;
     
     void Update () {
         foreach (Touch touch in Input.touches) {
             if (touch.phase == TouchPhase.Ended)
                 taps.Add(Time.timeSinceLevelLoad);
         }
         for (int i = 0; i < taps.Count; i++) {
             if (taps[i] <= Time.timeSinceLevelLoad-1) {
                 taps.Remove(i);
             }
         }
     tapsPerSecond = taps.Count;
     }
 }

To distinguish between tap, press, swipe, etc. would require data structures that tracked the fingerId of the touches and saved the position and time of events.

Also your original code has a bug in the for() line where a ',' should be a ';'.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image christianstrang · Nov 24, 2015 at 06:03 PM 0
Share

I had to change lin 16 to the following for it to work: taps.RemoveAt(i);

avatar image $$anonymous$$ · Apr 07, 2020 at 08:19 AM 0
Share

Basically there is a bug in your code too. taps.Remove(i); may not work since "i" is an int and "taps" is a float list. So either you must write it this way taps.Remove(taps[i]); or as somebody sugested here taps.RemoveAt(i);

avatar image
0

Answer by Sethhalocat · Oct 19, 2014 at 04:10 PM

You could set up that when 1 tap is entered then a yield for seconds is activated and then have the amount of taps = the amount of speed, but if you don't tap fast enough then you will go back to average speed.

I hope this helps :)

I know most people are looking for a script themselves but with this information it would be fun to test out your own scripts and use mono develop to debug it. It sounds a little complicated but I think it's worth a shot for you.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

tap and hold function to keep flying in c# 1 Answer

Tap/Click for Points 0 Answers

destroy gameObject on touch 0 Answers

Tap&Hold Android Game - weird problem with touch 0 Answers

Killing monster in a clicker game 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges