Hello,
I am using HandleUtility.ClosestPointToPolyLine() to get the projected mouse position on a 3D polyline.
But the output point doesn’t seem to be exact when the scene view uses perspective projection.
For example, with this simple editor:
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(MyScript))]
public class MyScriptEditor : Editor
{
Vector3[] polyLine = new Vector3[] {
new Vector3 (0.0f, 0.0f, 0.0f),
new Vector3 (3.0f, 0.0f, 0.0f),
new Vector3 (4.0f, 2.0f, 2.0f),
new Vector3 (0.0f, 3.0f, 6.0f),
};
void OnSceneGUI ()
{
Handles.DrawPolyLine (polyLine);
Vector3 mouse = HandleUtility.ClosestPointToPolyLine (polyLine);
Handles.DrawSolidDisc (mouse, -Camera.current.transform.forward, 0.1f);
HandleUtility.Repaint ();
}
}
With the scene view in isometric projection, the disc is drawn under the mouse cursor (expected behavior):
But in perspective projection mode the disc is not under the mouse cursor (wrong behavior):
Is there a way to get HandleUtility.ClosestPointToPolyLine() working correctly with perspective projection?
I know there are HandleUtility.PushCamera(), HandleUtility.PopCamera() and Handles.SetCamera() but I don’t really understand how to use them and if those methods can help to solve my problem.