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
1
Question by CodePingu · Dec 21, 2020 at 09:59 PM · mousemouse position

Find mouse Velocity

Hello. So, I've been trying to find the velocity of the mouse pointer in unity. Is there a way to find the position of the mouse pointer for this frame and for the frame before?

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 Eno-Khaon · Dec 21, 2020 at 11:05 PM

The simplest approach would be to store the current frame's mouse position just after comparing it to the previous frame's position:

 Vector3 lastMousePos;
 public Vector3 mouseDelta
 {
     get
     {
         return Input.mousePosition - lastMousePos;
     }
 }

 void Start()
 {
     // Initialize the value to avoid an anomalous first-frame value
     lastMousePos = Input.mousePosition;
 }

 void Update()
 {
     // Use mouseDelta as needed, then update lastMousePos at
     // the end of your Update() loop
     
     lastMousePos = Input.mousePosition;
 }


If you want viewport coordinates instead, you can divide the X and Y values by Screen.width and Screen.height respectively.

If you want them relative to your camera's viewable area, you can also factor in Camera.rect values to scale the resulting values further (when an even greater degree of normalization is desired).

Edit: Changed "currentMousePos" to "Input.mousePosition" -- It was technically an artifact of rewriting it halfway through the process

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 CodePingu · Dec 30, 2020 at 12:07 AM 0
Share

I tried copying this and it just gave me errors

avatar image CodePingu · Dec 30, 2020 at 12:12 AM 0
Share

It says "The name 'current$$anonymous$$ousePos' does not exist in the context"

avatar image Bunny83 CodePingu · Dec 30, 2020 at 12:29 AM 1
Share

current$$anonymous$$ousePos should be Input.mousePosition. Though this should be quite obvious. He explained the concept in an abstract pseudo code way that would work anywhere when you have access to the current mouse position. You should try to understand the concept and not just copy and paste example code :)

avatar image CodePingu · Dec 31, 2020 at 08:10 PM 0
Share

Thank you for letting me know. I am sort of a unity noob, so I don't really know all too much about coding in this

avatar image
1

Answer by Bunny83 · Dec 30, 2020 at 12:26 AM

Are you actually interested in the mouse speed or the speed of the mouse pointer. If you want the speed at which the mouse is moved, that's directly returned by Input.GetAxis("Mouse X") and Input.GetAxis("Mouse Y"). It tells you how much the mouse has moved since the last frame. Of course if you really are interested in the velocity of the mouse pointer, measured in pixel, you have to use something like what Eno showed in his answer.

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 CodePingu · Dec 31, 2020 at 07:14 PM 0
Share

Ok, thank you!

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

120 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 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 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 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 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 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

Normalized Coordinates are Exponentially inccorect 2 Answers

Having a first person player look at the mouse on the y axis 1 Answer

transform.LookAt gives a twist 2 Answers

Mobile controls, gui mouse problem. 0 Answers

troubles with look at mouse script 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