- Home /
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.
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?
this probably wouldnt work if the cursor was locked right?
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.
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.
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;
}
}
Your answer
Follow this Question
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