Detect how long a tap is held
I am making a mobile game, and I need to figure out how long a tap is held. Can someone please help me?
also, if you can give me code to detect how long a tap is held and detect if it is a swipe up, tap, or swipe down, then I will give you 20 reputation.
Answer by rushikesh988 · Feb 23, 2016 at 01:14 PM
@AndrewBilotti You can use something like event Trigger for that with the combination of StopWatch from System.Diagnostics . http://docs.unity3d.com/ScriptReference/EventSystems.EventTrigger.html
Here is an example how you can detect tap duration.
Add EventTrigger Component on a GameObject where you want to detect the tap.
Add Event Types as Pointer down and Pointer Up.
Refer this to the respective public methods in following script.
using UnityEngine; using System.Collections; using System.Diagnostics;
public class TouchSystems : MonoBehaviour { Stopwatch stopwatch; void Start() { stopwatch = new Stopwatch(); } void Update() { } public void OnPointerUp() { stopwatch.Start(); } public void OnPointerDown() { stopwatch.Stop(); if (stopwatch.ElapsedMilliseconds > 20 * 1000) { //Do Something } else { //upto you } stopwatch.Reset(); } }
@rushikeshi988
Wow... your quite the touch expert! just curious, why did you do '20 1000' ins$$anonymous$$d of 20000? This worked very well [no lag on my iPad!], and normal unity games make my iPad lag xD. $$anonymous$$uch better than using time.deltatime. Thanks for $$anonymous$$ching me how to use the stopwatch... now I don't have to use time. and reinventing the wheel. Do you, by any chance, also know how to detect if the tap is a swipe up, a tap, or a hold? If you could help me with that, then my game development would be ahead months :). It is so convenient when a pro gives me such a good answer!
Help! I did what you said to do, but it doesn't recognize the onpointerdown and on pointerup!
Your answer
Follow this Question
Related Questions
Network Transport Layer API does not work with iOS to PC???? 0 Answers
Cant get Touch Position.x 0 Answers
Touch screen Input Help 1 Answer
Mobile Optimization question 1 Answer