Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by xXCastigamattiXx · Jun 04, 2018 at 12:34 PM · draggingclickingclick and drag

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");
     }
 }


Comment
Add comment
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

2 Replies

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

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;
     }
 }



Comment
Add comment · Show 1 · 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 tormentoarmagedoom · Jun 06, 2018 at 10:49 AM 1
Share

Nice!

Than accept your answer so other players can easy find the best solution!

Bye! :D

avatar image
1

Answer by tormentoarmagedoom · Jun 05, 2018 at 10:26 AM

Do you know about....

MonoBehaviour.OnMouseDrag() ?

Xd :D

Bye

Comment
Add comment · Show 1 · 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 xXCastigamattiXx · Jun 05, 2018 at 10:58 AM 0
Share

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

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

141 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What causes the audio clicking & how do I get a clean sound? 11 Answers

Checking if over UI during onEndDrag - EventSystem.isPointerOverGameObject is not working as expected 1 Answer

Orbit for touchscreen 0 Answers

I have this Drag script it works fine but i want it so when you click and drag it follows the mouse like a trail instead of being underneath it, can someone help me please thanks. 0 Answers

How to drag multiple object simultaneously??? 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