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
2
Question by LukaKotar · Mar 13, 2013 at 11:35 PM · mousespeedaxis

Getting mouse movement speed (all axis)

Hi.

Title pretty much says it all. I'd like to know how fast the mouse is moving, in all the axis as one value. Mouse input returns either x or y axis, but I only need the speed. How can I calculate it?

Thanks,

Luka

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
3
Best Answer

Answer by AlucardJay · Mar 14, 2013 at 02:56 AM

Check my answer Here : http://answers.unity3d.com/questions/332013/calculating-change-in-mouse-position.html

This script will Debug the changes in the mouse position between frames when you click or hold the mouse button. These values are stored in delta.x and delta.y :

 #pragma strict
 
 public var delta : Vector3 = Vector3.zero;
 private var lastPos : Vector3 = Vector3.zero;
 
 function Update()
 {
     if ( Input.GetMouseButtonDown(0) )
     {
         lastPos = Input.mousePosition;
     }
     else if ( Input.GetMouseButton(0) )
     {
         delta = Input.mousePosition - lastPos;
 
         // Do Stuff here
         
         Debug.Log( "delta X : " + delta.x );
         Debug.Log( "delta Y : " + delta.y );
         
         Debug.Log( "delta distance : " + delta.magnitude );
 
         // End do stuff
 
         lastPos = Input.mousePosition;
     }
 }
Comment
Add comment · Show 4 · 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 LukaKotar · Mar 14, 2013 at 03:43 AM 0
Share

Thanks, but I need one value, not two. I'd like to see the speed in one value, for any direction the mouse is going.

avatar image AlucardJay · Mar 14, 2013 at 04:29 AM 0
Share
 delta.magnitude

http://docs.unity3d.com/Documentation/ScriptReference/Vector3-magnitude.html

I have edited the answer.

As a point, speed is distance over time :

 speed = distance / time;

There are other considerations. What if the player has a different screen resolution to you?

 delta.x = delta.x / Screen.width;
 delta.y = delta.y / Screen.height;

this way you'll get the same outputs on different resolution. $$anonymous$$eep in $$anonymous$$d that this will return normalized values (between -1 and 1).

magnitude will also be a small normalized number, but remain in the positive as it is a distance regardless of direction.

avatar image LukaKotar · Mar 20, 2013 at 11:18 AM 0
Share

Sorry for late reply, thank you, it works perfect!

avatar image BillyBobBeavis · Nov 09, 2018 at 08:05 PM 0
Share

I've been searching for this solution for two days. Thank you.

avatar image
2

Answer by sami1592 · Dec 22, 2014 at 08:59 PM

you can get the result from the following function, I think that's why they did not provide anything like "Input.mousePosition.Delta()"

 Input.GetAxis("Mouse X");
 Input.GetAxis("Mouse Y");
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

13 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

Related Questions

How do I determine mouse speed on the x and z axis? 0 Answers

How to properly convert mouse pos to world pos 1 Answer

Make the gameobject flip to the direction it is being dragged to? 1 Answer

Charging up the speed of an game object through a mouse click 3 Answers

Mouse move with specific speed? 2 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