- Home /
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
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;
}
}
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.
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.
I've been searching for this solution for two days. Thank you.
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");
Your answer
Follow this Question
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