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 Serpentice · Feb 23, 2013 at 07:57 PM · inputmouse

How can I detect the mouse is moving through code?

I just want to be able to play certain animations if the mouse is moving in a certain direction. somethng like this

if(mouse is moving to the left){ animation.Play("WhateverIWant"); }

I'm not sure what I need to enter for unity to detect my mouse is moving in a certain direction.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Henrik Poulsen · Feb 23, 2013 at 08:23 PM

The easiest way would probably be to store the mouse location and then compare it to the current mouse coordinate to see if it is moving to the left. For example:

 Vector3 lastMouseCoordinate = Vector3.zero;
 void Update()
 {
     // First we find out how much it has moved, by comparing it with the stored coordinate.
     Vector3 mouseDelta = Input.mousePosition - lastMouseCoordinate;
 
     // Then we check if it has moved to the left.
     if(mouseDelta.x < 0) // Assuming a negative value is to the left.
         animation.Play("WhateverIWant");

     // Then we store our mousePosition so that we can check it again next frame.
     lastMouseCoordinate = Input.mousePosition;
 }

Depending on how you want it to work, but that would be a simple example.

Makes sense?

Comment
Add comment · Show 2 · 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 Serpentice · Feb 23, 2013 at 08:28 PM 0
Share

this probably wouldnt work if the cursor was locked right?

avatar image Henrik Poulsen · Feb 23, 2013 at 08:35 PM 0
Share

Ah yes of course. Then you would want to take a look at the Input$$anonymous$$anager (http://docs.unity3d.com/Documentation/Components/class-Input$$anonymous$$anager.html) There already are a bunch of default values set in there. And for what you are looking for you can use:

 float mouseX = Input.GetAxis("$$anonymous$$ouse X");

It returns a negative value when it is towards the left and positive when it is to the right.

avatar image
0

Answer by robertbu · Feb 23, 2013 at 08:29 PM

You will need to sample the mouse position periodically and compare that position to the previous position. Sampling every frame is likely too frequently. If the difference in the magnitude of the x move is above some threshold, then you would call it a directional move. You can get the current mouse position using Input.mousePositon. You can create a sampling rate by either creating your own timer or by using InvokeRepeating().

Note if you are targeting multiple platforms with your code, you may want to convert your mouse coordinates into viewport coordinates.

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
avatar image
0

Answer by DannyMen · Oct 26, 2017 at 03:00 PM

C# code // THIS IS A CODE TO DETECT MOUSE MOVING AND AFTER SOME TIMER TIME THE CURSOR DESAPPEAR // C# code

 float timeLeft;
     float visibleCursorTimer = 10.0f;
     float cursorPosition;
     bool catchCursor = true;

     void Update(){

         if(catchCursor){
             catchCursor = false;
             cursorPosition = Input.GetAxis("Mouse X");
         }

         if(Input.GetAxis("Mouse X") == cursorPosition)
              {
                  print("Mouse stop");
                  timeLeft -= Time.deltaTime;
                  if ( timeLeft < 0 )
                      {                
                      timeLeft = visibleCursorTimer;
                       Cursor.visible = false;
                       catchCursor=true;
                      }

              }else{
                  timeLeft = visibleCursorTimer;
                  Cursor.visible = true;
              }            

     }
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

12 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

Related Questions

Input System Can't Catch Event on Update 0 Answers

Character Zoom in and out 1 Answer

OnTouch function? 3 Answers

Input.GetAxisRaw("Mouse ScrollWheel") 4 times 0 Answers

Left Click While Holding Right Click 1 Answer


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