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 GenericUserName · Jun 30, 2015 at 04:31 PM · inputmouseevents

Input: Missing mouse button events

I'm trying to create an input system that will catch all left mouse button events.

With the current code I'm missing about 1 in every 35 left mouse button events.

The target platform is a Windows Standalone created in Windows 5 Pro running on a machine with Windows 10. The mouse input is being driven by launching toy ammunition at a touchscreen, at a rate of about 2-ish per second. I'm testing on a scene with no real overhead, just a plane and instantiating a sphere locked at 60 frames per second with vsync. My code is using OnGUI to put mouse events into a queue and Update to process them. I've tried the popular TouchScript and Rewired plug-ins and the result is similar.

Any advice would be greatly appreciated.

 import System.Collections.Generic;
 
 var queue = new List.<Event>();  
 var currentEvent: Event;
 var mouseNumber: int;
 var mouseTextBox: GameObject;
 var mouseSphere: GameObject;
 var mouseVisibleNumber : int; 
 
 
 function OnGUI() {
           currentEvent = Event.current;   
            //find left mouse button clicks
             if (currentEvent.type == EventType.MouseDown && currentEvent.button == 0){
                       //adds to back of queue
                       queue.Add(currentEvent);
                       } 
 }
      
      
 function Update() {
      var hit: RaycastHit;
       //if the queue has anything in it
       if (queue.Count > 0){
            //references front of queue   
             var processingEvent = queue [0];
             var flippingEvent = processingEvent.mousePosition;   
             //flips y coordinate, as putting it in the queue causes it to be inverted somehow
             var flippingY = Screen.height-processingEvent.mousePosition.y;
             var ray = Camera.main.ScreenPointToRay(Vector3(processingEvent.mousePosition.x, flippingY, 0));
                            //if the mouseclick hits something, add number to textbox            
                          if (Physics.Raycast(ray, hit)){
                              mouseNumber ++;   
                              Instantiate (mouseSphere, hit.point, transform.rotation);
                              mouseVisibleNumber ++;
                              var mouseInt = mouseVisibleNumber; 
                              var mouseStr = mouseInt.ToString();
                              mouseTextBox.GetComponent(UI.Text).text = mouseStr;
                                  for (var queuedEvents in queue){
                                  Debug.Log (queue.Count + "," + queuedEvents);
                                  }      
                             queue.RemoveAt(0);
                             }
                             }
                             }
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
0

Answer by mysokskycz · Jun 30, 2015 at 05:56 PM

Do you have Mouse buttons in Edit>Project Settings>Input ? Mouse buttons are called "Fire1" and "Fire2".

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 GenericUserName · Jun 30, 2015 at 06:13 PM 0
Share

Yes, $$anonymous$$ouse0 is showing up as the Alt. Positive Button of Fire1, and $$anonymous$$ouse1 is showing up as the Alt. Positive Button of Fire2.

avatar image
0

Answer by guavaman · Jul 02, 2015 at 07:05 PM

60 frames per second with vsync

Disable v-sync and see what happens.

Unity input and Rewired are single-threaded. Mouse messages are delivered to that thread at the beginning of every update. If mouse messages are received in the middle of the frame, Windows will only deliver the last message.

https://msdn.microsoft.com/en-us/library/windows/desktop/ms645601(v=vs.85).aspx

The mouse generates an input event when the user moves the mouse, or presses or releases a mouse button. The system converts mouse input events into messages and posts them to the appropriate thread's message queue. When mouse messages are posted faster than a thread can process them, the system discards all but the most recent mouse message.

If you’re updating at 60 fps and your click lasts for 0.005s (1/3 of a frame, which is likely since you're shooting an object at a screen), the last mouse event Windows will deliver will be mouse UP, not down. That means you miss the click event.

One way to be sure to catch all those events would be to update input at a much higher rate. The easiest way would be to disable v-sync. A better way would be to have input update on a separate thread so the refresh rate is much higher than 60hz and buffer click events.

If you have any control over the data produced by the touchscreen, you could tweak that so it lasts at least 1/30th of a second or so.

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

EventSystem - detect any click NOT on object 1 Answer

OnMouse menu 1 Answer

Check if mouse is down while in Edit Mode 1 Answer

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

Raycast with strange Behaviour 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