- Home /
Unity Touch Controls, Drag and DoubleTap
Greetings,
I've been bashing my head over this one for the past two days, and I just can't get it to work... I've been using the mouse controls for single touch controls on mobile devices. However I've noticed some hiccups here and there and would rather use the touch controls. I've checked the entire web for a solution but none worked for me for some reason... I've pasted the code I have atm for mouse, I need help converting that for touch controls.
THANK YOU in advance!
void OnMouseDown()
{
distance = Vector3.Distance (transform.position, Camera.main.transform.position);
dragging = true;
}
void OnMouseUp()
{
dragging = false;
if ((Time.time - doubleClickStart) < 0.3f)
{
this.OnDoubleClick();
doubleClickStart = -1;
}
else
{
doubleClickStart = Time.time;
}
void Update()
{ if (dragging)
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
Vector3 rayPoint = ray.GetPoint (distance);
transform.position = rayPoint;
}
}
}
Answer by misher · Mar 01, 2017 at 08:03 AM
///Here is a simple and tested component script that you can use on any 3d
///(or even UI) object in order to detect Single or Double Tap gestures
///(You will see in inspector 2 events). For 3d object there must be a collider,
///also make sure there is Physics Raycaster component attached to the Camera,
///if not - add one.
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class TapsEvents : MonoBehaviour, IPointerDownHandler, IBeginDragHandler {
// You can add listeners in inspector
public UnityEvent OnSingleTap;
public UnityEvent OnDoubleTap;
float firstTapTime = 0f;
float timeBetweenTaps = 0.2f; // time between taps to be resolved in double tap
bool doubleTapInitialized;
public void OnPointerDown(PointerEventData eventData)
{
if (!doubleTapInitialized)
{
// invoke single tap after max time between taps
Invoke("SingleTap", timeBetweenTaps);
// init double tapping
doubleTapInitialized = true;
firstTapTime = Time.time;
}
else if (Time.time - firstTapTime < timeBetweenTaps)
{
// here we have tapped second time before "single tap" has been invoked
CancelInvoke("SingleTap"); // cancel "single tap" invoking
DoubleTap();
}
}
void SingleTap()
{
doubleTapInitialized = false; // deinit double tap
// fire OnSingleTap event for all eventual subscribers
if(OnSingleTap != null)
{
OnSingleTap.Invoke();
}
}
void DoubleTap()
{
doubleTapInitialized = false;
if(OnDoubleTap != null)
{
OnDoubleTap.Invoke();
}
}
public void OnBeginDrag(PointerEventData eventData)
{
CancelInvoke("SingleTap");
doubleTapInitialized = false;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613194556im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Handling drag of an UI element in a scroll view over the whole canvas 1 Answer
Drag Object, same speed as mouse/touch 3 Answers
Touch - drag different objects 1 Answer
Multiple Cars not working 1 Answer