Using delta to find mouse movement

I am trying to get the relative movement of the cursor. Currently, this script works but only when you click and drag. I want it to activate when the mouse has been moved. Also, it activates twice for some reason, even though it’s only set to one object.

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    void OnGUI() {
        if (Event.current.delta.x > 0 || Event.current.delta.y > 0)
            Debug.Log(Event.current.delta);
        
    }
}

Looks like the current event only reacts when the button is pressed. Because Mouse move events are never sent in the games.

But you can do something like this instead:

	private Vector2 oldMouePosition = Vector2.zero;

	void OnGUI()
	{
        Vector2 currentMousePosition = Event.current.mousePosition;
		float xMovement = Mathf.Abs(oldMouePosition.x - currentMousePosition.x);
		float yMovement = Mathf.Abs(oldMouePosition.y - currentMousePosition.y);
		oldMouePosition = currentMousePosition;	
		
		if(xMovement != 0.0f || yMovement != 0.0f)
		{
			Debug.Log(xMovement + ", " + yMovement);
		}
	}

So you take the current mouse position and you calculate the difference between the last time you updated the mouse position. I use Abs to always get a positive float. If you want to know if the mouse goes to the left or to the right (positive or a negative float) just can just ignore the Abs function.

And if the xMovement or the yMovement is not 0, that means that the mouse pointer has moved since the last GUI update.

If you want to avoid having a strange variable at the very first GUI update (since the old mouse positio is 0, 0 and the mouse position is whatever it is), you might want to set a little “check” to avoid that issue.

For instance, set the oldMousePosition to a value that will never appear:

  private Vector2 oldMouePosition = new Vector2(-999.99f, -999.99f);

And then in the GUI function, make a small check to see if the oldMousePosition vector has this specific value:

   void OnGUI()
   {
	if(oldMouePosition.x == -999.99f && oldMouePosition.y == -999.99f)
	{
	 oldMouePosition = Event.current.mousePosition;
	}

    // Rest of the function here...
   }

And if it does have this value, then set the vector to the current mouse position.

I hope this example helps you.

Good luck!