Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Giantbean · Dec 26, 2013 at 09:47 PM · mouseclickinput.getaxisinput.mousepositionholding

mouse Down+Hold and double click events

I am trying to get an object to register different events based on click, double click and a hold event. Currently I can click and hold in the scene to move a camera (Great!) and I can double click an object to register a hit on the object (Great!) but I can't click and hold on the object to to open a pop up (not so great.)

this is how I have been trying:

     private float lastClick = 0;
     private float waitTime = 1.0f; //wait time befor reacting
     private float downTime; //internal time from when the key is pressed
     private bool isHandled = false;
 
 
     //WIP: marking menu code. Find if the mouse is held in place for x amount of time with less than x amount of movement.
     //then stop the camera script and fire a script from OnMouseUp to call the markup and reactivate the camera
     void OnMouseDown () {
         //start recording the time when a key is pressed and held.
         downTime = Time.time;
         isHandled = false;
 
         //look for a double click
         if(Time.time-lastClick < 0.3){
             
             // do something
             Debug.Log("You double clicked the target.")
             }
             //open a menu when the key has been held for long enough.
         }
         if((Time.time > downTime + waitTime) && !isHandled){
             isHandled = true;// reset the timer for the next button press
             //add markup menue script here.
             Debug.Log ("MouseKey was pressed and held for over " + waitTime + " secounds.");
         }
         lastClick = Time.time;
     }

I can get the double click to work and have had the debug.log show up for the hold but its not timing it right and its not resetting. It would be great if I could also register the amount of mouse movement so if I click and move it still drags the camera rather than opening the menu.

maybe encompass the whole thing in something like:

 if(Input.GetAxis("Mouse X") < transform * 0.5f; //?


any ideas?

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

1 Reply

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

Answer by T27M · Dec 26, 2013 at 11:06 PM

I would use the OnMouseDrag() function which is called after OnMouseDown if the mouse button is still held down. OnMouseDrag is called regardless of the mouse being over the collider, so you may want to check that.

 private float lastClick = 0;
 private float waitTime = 1.0f; //wait time befor reacting
 private float downTime; //internal time from when the key is pressed
 private bool isHandled = false;
 
 
 //WIP: marking menu code. Find if the mouse is held in place for x amount of time with less than x amount of movement.
 //then stop the camera script and fire a script from OnMouseUp to call the markup and reactivate the camera
 void OnMouseDown () {
     //start recording the time when a key is pressed and held.
     downTime = Time.time;
     isHandled = false;
     
     //look for a double click
     if(Time.time-lastClick < 0.3){
         // do something
         Debug.Log("You double clicked the target.");
     }

     lastClick = Time.time;
 }

 void OnMouseDrag(){
     //open a menu when the key has been held for long enough.
     if((Time.time > downTime + waitTime) && !isHandled){
         isHandled = true;// reset the timer for the next button press
         //add markup menue script here.
         Debug.Log ("MouseKey was pressed and held for over " + waitTime + " secounds.");
     }
 }
Comment
Add comment · Show 7 · 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 Giantbean · Dec 27, 2013 at 03:45 PM 0
Share

I tested that this morning and it didn't work. The On$$anonymous$$ouseDrag is definitely the way to go though. Thanks for sharing. Now I just need to fix the rest of my code.

avatar image T27M · Dec 27, 2013 at 04:04 PM 0
Share

It didn't work? How so? I tested it when I wrote it with no issues.

avatar image Giantbean · Dec 27, 2013 at 04:11 PM 0
Share

It never called the debug log. I added a Debug.Log("Dragging right after the On$$anonymous$$ouseDown and that gets called (a lot) but the "$$anonymous$$ouse$$anonymous$$ey was pressed..." never gets called.

EDIT: Never-$$anonymous$$d I just tested again, I had a typo that the editor didn't catch and it works now!

I just need to make it only fire when the object is under the mouse or cancel the mouse drag if the mouse position changes by to large of a degree. Any idea how to do that?

Thanks again for the On$$anonymous$$ouseDrag! it works great.

avatar image T27M · Dec 27, 2013 at 04:13 PM 0
Share

You are holding the mouse button down for at least the wait time right?

avatar image T27M · Dec 27, 2013 at 04:22 PM 1
Share

Add this

 private bool mouseOver = false;

 void On$$anonymous$$ouseEnter(){
     mouseOver = true;
 }

 void On$$anonymous$$ouseExit(){
     mouseOver = false;
 }

and change the On$$anonymous$$ouseDrag to:

 void On$$anonymous$$ouseDrag(){
     //open a menu when the key has been held for long enough.
     if((Time.time > downTime + waitTime) && !isHandled && mouseOver){
         isHandled = true;// reset the timer for the next button press
         //add markup menue script here.
         Debug.Log ("$$anonymous$$ouse$$anonymous$$ey was pressed and held for over " + waitTime + " secounds.");
     }
 }
Show more comments

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

19 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

Related Questions

How do I correct no response from input keys 0 Answers

How can I Take axis and turn wheel using pointer event from UI button for mobile platform..? 0 Answers

What script should I write for RotationHelper for my code to work? 0 Answers

Can't move game object left of center 2 Answers

Raycast calling both if and else statments 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