On the touchscreen, you can easily measure the speed of a finger.

foreach(Touch th in input.touches) { th.deltaPosition.x / Time.deltaTime } 

How to do it for a mouse? It is desirable with a similar code so that you can make a universal method of measuring speed for a mouse and a wheelbarrow.

  • Does it exactly work? You need to take the previous coordinate (Mouse / finger) to take the current coordinate and divide it into Time.deltaTime. - Valera Kvip
  • @ValeraKvip Works great! docs.unity3d.com/ScriptReference/Touch-deltaPosition.html It even says that you can use it for speed. The previous coordinate from the current is the obvious solution. For tacha it happens in automatic mode, apparently because Input.touches returns an array. But with a mouse of another option except how to maintain the position of n and n-1 and see the difference between them? - Dmitrii

4 answers 4

In addition to the author's answer (I can not comment, I do not have enough reputation points):

 Vector2 lastMousepos = Input.MousePosition; void Update() { AxisX = ((Input.MousePosition.x - lastMousepos.x) / Time.deltaTime) / Screen.Width; AxisY = ((Input.MousePosition.y - lastMousepos.y) / Time.deltaTime) /Screen.Height; lastMousepos = Input.MousePosition; } 

minus one condition check every frame

    You can calculate the difference (delta) between the starting and ending position, this will be your distance, which needs to be divided by time.

    • Perhaps you meant between the position in the previous frame and the current one? Can you paint it in more detail? - Dmitrii
    • Offhand - you can get the coordinates of the cursor in Update / FixedUpdate (better in Update, of course), write them to some Vector2 LastCoordinates, and calculate the difference, then read the speed. - eastwing
    • Remarking "better in Update, of course" please consider invalid - it depends on what exactly you need :) - eastwing

    Do so

     Vector2 lastMousepos; void Update(){ if (lastMousepos == Vector2.zero) { lastMousepos = Input.MousePosition; } else { AxisX = ((Input.MousePosition.x - lastMousepos.x) / Time.deltaTime) / Screen.Width; AxisY = ((Input.MousePosition.y - lastMousepos.y) / Time.deltaTime) / Screen.Height; lastMousepos = Input.MousePosition; }} 

    AxisX and AxisY gives the movement speed from 0 to 1 on each axis. 1 - the speed at which the mouse flies the entire screen per frame.

      What about Input.GetAxis() ?

       Input.GetAxis("Mouse X"); Input.GetAxis("Mouse Y"); 

      For more information:

      Returns the value along axisName of the virtual axis.

      For input from the keyboard or joystick, the value will lie in the range -1 ... 1. The range is not -1 ... 1.

      It does not depend on the frame rate. When using this value, there is no need to worry about changing the frame rate.

       using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { public float horizontalSpeed = 2.0F; public float verticalSpeed = 2.0F; void Update() { float h = horizontalSpeed * Input.GetAxis("Mouse X"); float v = verticalSpeed * Input.GetAxis("Mouse Y"); transform.Rotate(v, h, 0); } } 

      Source of

      (Just in case, you can adjust the sensitivity in Edit → Project Settings → Input.)

      • one
        The answer is extremely uninformative. Can you paint a little more? - Dmitrii
      • Is it a measurement of speed? We just get the axis values - Dmitrii
      • one
        @Dmitrii, no, for a mouse, this is speed. - Surfin Bird