Created
June 9, 2023 18:37
-
-
Save kurtdekker/f4a8c01f3aad5562275099c4821f211d to your computer and use it in GitHub Desktop.
Simple reporter of mouse movement speed in pixels.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
// @kurdekker - reports mouse speed in screen pixels per second. | |
// | |
// To use: | |
// - drop on an empty GameObject | |
// - make sure the console will be visible | |
// - press PLAY | |
// - wiggle your mouse | |
// - watch your console | |
// | |
// Use Mathf.Lerp() or your own filter window to reduce signal noise/jitter. | |
public class SimpleMouseSpeed : MonoBehaviour | |
{ | |
Vector2 previous; | |
Vector2 ReadMouse() | |
{ | |
// this is the ONE place you can change if you wish to get your mouse from somewhere else | |
Vector3 rawPosition = Input.mousePosition; | |
return new Vector2( rawPosition.x, rawPosition.y); | |
} | |
void Start() | |
{ | |
previous = ReadMouse(); | |
} | |
void Update () | |
{ | |
// read and age position (differentiate with respect to frame) | |
Vector2 current = ReadMouse(); | |
Vector2 delta = current - previous; | |
previous = current; | |
// how far was that in pixels? | |
float distance = delta.magnitude; | |
// how long in seconds was the last frame? | |
float timeDelta = Time.deltaTime; | |
// compute! | |
float pixelsPerSecond = distance / timeDelta; | |
// and report! | |
Debug.Log( System.String.Format( "Speed is {0:000.0} pixels / second.", pixelsPerSecond)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment