• 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 Showken · Oct 22, 2010 at 08:12 AM · mousemoving

are there some way to determine whether the mouse is moving or not.

are there some way to determine whether the mouse is moving or not.

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

Answer by Rasmus Schlnsen · Oct 22, 2010 at 08:16 AM

Here is some sample code that works:

private Vector3 tmpMousePosition;

void Start(){ tmpMousePosition = Input.mousePosition;

}

void Update(){

if (tmpMousePosition != Input.mousePosition){ Debug.Log("Mouse moved"); tmpMousePosition = Input.mousePosition }

}

Comment
Add comment · Show 3 · 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 Showken · Oct 23, 2010 at 02:06 AM 0
Share

how can I make the code(calss) to a function?

avatar image Showken · Oct 23, 2010 at 02:36 AM 0
Share

I want to deter$$anonymous$$e whether the mouse is moving or not every frame.

avatar image Showken · Oct 23, 2010 at 04:02 AM 0
Share

3Q!I have already understanded your code ,thk you very much!!

avatar image
1

Answer by zannghast · Oct 22, 2010 at 08:48 AM

Yes there is. Here's a simple way of detecting mouse movement (and direction of movement while you're at it):

Set the appropriate Axes values first

1) Navigate to Edit > Project Settings > Input. You should see the Inspector tab change and display the Axes values.

2) Look for the Axes named "Horizontal" and "Vertical" near the bottom of the list of Axes names (remember, the bottom, not the top)

3) Expand each one and you should see the "Type" property. Change the value on that to "Mouse Movement"

Now you're ready to use code to detect the change

These are really basic stuff. I'm just showing you how you could potentially use it okay?

1) Detecting a change in horizontal mouse movement:

// calculation variables float current_x = 0.0f;

// states bool x_changed = false;

// string references to speed string manipulation/calculation string x_axis = "Horizontal";

//initialize your variables here void Awake() { // initialize your current x with the current change in x axis current_x = Input.GetAxis(x_axis); }

void Update() {

 if (DidMove()) {
     Debug.Log("Mouse moved horizontally.");
 }

}

bool DidMove() { // get the current change in horizontal mouse movement current_x = Input.GetAxis(x_axis);

 // check if the current x has a value of 0. if not, return true;
 if (current_x == 0)
     return false;
 else
     return true;

}

What happens in the code above is that every Update, the game checks if mouse moved horizontally. This check is done by function DidMove(), which returns a true if the position of the mouse along the x axis changes. This position is further determined by the built-in function Input.GetAxis("Horizontal"), or in this case Input.GetAxis(x_axis) - we simply cached in the string "Horizontal" to avoid any sort of overhead resulting from using string literals.

This built-in function returns a value in the range of -1 to 1, depending on the actual mouse movement.

Note that this code will not work had we forgotten to change the Axes values under Edit > Project Settings > Input.

Here's a link to the scripting reference page for this function: Link

I wasn't able to test the code by the way, but that would be a pretty simple way of detecting change in x. By the way, I highly recommend that you look to the scripting reference page first. You may find your answer there before you post your questions here. ;)

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 Showken · Oct 23, 2010 at 02:16 AM 0
Share

thk you very much!!it's very useful

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

My mouse won't move in the Unity player at all. 0 Answers

Movement In A Mobile Game 2 Answers

Moving a object around with mouse, rotating it with arrow keys and placing it. 1 Answer

Dragging Objects with the mouse 1 Answer

Character moving to mouse in third person camera 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