Best practice distinguishing simple click from drag
I know the functions OnMouseDown and OnMouseUp, between which passes a time interval, more or less short (depends on user interaction).
I could assume that between the moment when the user presses the button (OnMouseDown) and releases it (OnMouseUp) is dragging, but this is not always true: the user could click and hold the object but for some reason is having a rethink and releases the button (and this would trigger the dragging mechanism that is not desired).
In this example I would like the whole operation to be considered as a click and not as a drag, because the two events are associated with two different functions.
In your opinion, what is the best way in Unity3D to distinguish a real drag from a complete click (more or less long)? Maybe I should distinguish how many pixels are "dragged" to distinguish a click from a drag? What would you propose?
Update
I add the following code below to get more details
public class Flip : MonoBehaviour {
private Animator anim;
public enum cs_e {covered, uncovered};
public cs_e coveringState;
public bool covered;
// Use this for initialization
void Start () {
covered = true;
anim = this.GetComponent<Animator>();
coveringState = cs_e.covered;
}
// Update is called once per frame
void Update () {
}
private void OnMouseUp()
{
Debug.Log("Mouse up");
if (coveringState == cs_e.uncovered)
Cover();
else
Uncover();
}
public void Cover() {
anim.Play("Cover");
coveringState = cs_e.covered;
covered = true;
}
public void Uncover() {
anim.Play("Uncover");
coveringState = cs_e.uncovered;
covered = false;
}
private void OnMouseDown()
{
Debug.Log("Mouse down");
}
private void OnMouseDrag()
{
Debug.Log("Mouse drag");
}
}
Answer by xXCastigamattiXx · Jun 05, 2018 at 12:15 PM
I found the solution that suits me. Maybe it's not the best solution, but at the moment it works well and does what I need. I use an approach that relies on the management and measurement of the time between the click down and the click up. I simply defined a ClickDeltaTime which is a time interval (in seconds) that I personally consider as a simple click, if the time spent between the click down and the click up is greater than the time I have defined, then it is considered a drag . In this case it was not necessary to use the OnMouseDrag() function.
The code below does what I need:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Flip : MonoBehaviour {
private Animator anim;
public enum cs_e {covered, uncovered};
public cs_e coveringState;
public bool covered;
// Time management
private float downClickTime;
private float ClickDeltaTime = 0.2F; // Intervallo di tempo che indica un click e basta, oltre il quale si considera un trascinamento
// Use this for initialization
void Start () {
covered = true;
anim = this.GetComponent<Animator>();
coveringState = cs_e.covered;
}
// Update is called once per frame
void Update () {
}
private void OnMouseUp()
{
Debug.Log("Mouse up");
if(Time.time - downClickTime <= ClickDeltaTime) {
if (coveringState == cs_e.uncovered)
Cover();
else
Uncover();
}
}
public void Cover() {
anim.Play("Cover");
coveringState = cs_e.covered;
covered = true;
}
public void Uncover() {
anim.Play("Uncover");
coveringState = cs_e.uncovered;
covered = false;
}
private void OnMouseDown()
{
Debug.Log("Mouse down");
downClickTime = Time.time;
}
}
Nice!
Than accept your answer so other players can easy find the best solution!
Bye! :D
Answer by tormentoarmagedoom · Jun 05, 2018 at 10:26 AM
The problem of On$$anonymous$$ouseDrag is that it is always invoked, both when I actually want a drag and when I do not want it (ie when I would like it to be just a simple click). In fact, making only one click, even very fast, is still triggered On$$anonymous$$ouseDrag.
Your answer
Follow this Question
Related Questions
What causes the audio clicking & how do I get a clean sound? 11 Answers
Orbit for touchscreen 0 Answers