How does this work? HandleUtility.FindNearestVertex

I am trying to use vertex snapping in the scene GUI. I found this method in this documentation but I cannot get it to function properly. I input a GUI point, given by the example given in this GUI Point documentation, but the output Vector3 vertex seems very random to me.

This is the code I used to test this method

    private void OnSceneGUI()
    {
        //Set sphere color
        Handles.color = Color.red;

        //Get the mouse position
        Vector2 mousePos = Event.current.mousePosition;
       
        //Convert it to a gui point
        Vector2 guiPoint = GUIUtility.ScreenToGUIPoint(mousePos);

        //Find the nearest vertex to that gui point
        bool found = HandleUtility.FindNearestVertex(guiPoint, out Vector3 vertex);
       
        //Draw a sphere on that vertex
        Handles.SphereHandleCap(0, vertex, Quaternion.identity, HandleUtility.GetHandleSize(vertex) * .2f, EventType.Repaint);

        //Log results
        Debug.Log(found + ": " + vertex);
    }

This is what it looked like in the Scene View

7256504--874997--8xCz8E21ys.gif

I expected the Sphere to snap to vertices close to the mouse cursor, but it does not do that.

Could someone maybe explain to me how to use this properly?

Thanks!

Okay so I already solved it.
This method does exactly work as advertised. However, the Scene View wasn’t repainting as fast as I expected it to do. Because of this, the Sphere did not follow the mouse properly: it lagged behind. If I add Sceneview.RepaintAll() at the end of the code, it all works fine!