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 0lachlan0 · Dec 22, 2013 at 04:21 AM · movementmousedetect

Detecting which way my mouse is moving the most

Hello my question is how can i detect which way my mouse is moving the most ? I already have this script -

Vector3 lastMouseCoordinate = Vector3.zero;

 void Update()
 {
     
     Vector3 mouseDelta = Input.mousePosition - lastMouseCoordinate;
     
     if(mouseDelta.x < 0)
         animation.Play(Swing1);
     if(mouseDelta.x > 0) 
         animation.Play(Swing2);
     if(mouseDelta.y > 0) 
         animation.Play(Swing3);
     if(mouseDelta.y < 0) 
         animation.Play(Swing4);
     
     
     lastMouseCoordinate = Input.mousePosition;
 }

This detects if the mouse is moving up,down,left, or right but the problem with this is sometimes when i would move my mouse left it would also detect it moving up or down, i know this is because my mouse is by only a few mm but still how can i detect which direction it is moving in the most.

Thanks

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

Answer by Benproductions1 · Dec 22, 2013 at 04:43 AM

Hello,

Assuming you only want the directions up, down, left and right, I'll give a few suggestions:

Check which axis has the highest magnitude:

 //first we find whether the x or the y axis have a higher magnitude
 
 float xmag = Mathf.Abs(mouseDelta.x);
 float ymag = Mathf.Abs(mouseDelta.y);
 
 if (xmag > ymag) {
     //mouse is moving mostly along the x axis
     
     if (mouseDelta.x > 0) {
         //RIGHT
     }
     else {
         //LEFT
     }
 }
 else if (ymag < xmag) {
     //mouse is moving mostly along y axis
     
     if (mouseDelta.y > 0) {
         //UP
     }
     else {
         //DOWN
     }
 }
 else {
     //NO MOVEMENT
     //OR PERFECTLY SIDEWAYS
 }

Use the dot product to find which axis the direction of mouseDelta is closest to:

 //will get a 0/0 error if mouse does not move
 Vector3 direction = mouseDelta.normalized;
 
 float dot = Vector3.Dot(direction, Vector3.up);
 if (dot > 0.5) { //can be >= for sideways
     //UP
 }
 else if (dot < -0.5) { //can be <= for sideways
     //DOWN
 }
 else {
     dot = Vector3.Dot(direction, Vector3.right);
     if (dot > 0.5) { //can be >= for sideways
         //RIGHT
     }
     else if (dot < -0.5) { //can be <= for sideways
         //LEFT
     }
 }

As you can see, both methods are quite compact, while one handles sideways movement more easily and the other handles no movement more easily. The latter method can also be expanded to support more directions while the upper method is restricted to the number of dimensions.

Hope this helps,
Benproductions1

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 Nuno_Barros · Apr 18, 2017 at 10:17 AM 0
Share

On your line 16 from the first bit of a code you placed a "<" ins$$anonymous$$d of a ">" Just a small mistake, but for beginners like me it may take them a while to figure out.

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

20 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Detect Mouse Movement 1 Answer

c# scripting navigation problems 1 Answer

Move object towards mouse in full 3D space 2 Answers

Hiccup between player movement and mouse control after game start. 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