Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

HandleUtility.ClosestPointToPolyLine and PerspectiveView

Discussion in 'Immediate Mode GUI (IMGUI)' started by Xaurrien, Apr 29, 2015.

  1. Xaurrien

    Xaurrien

    Joined:
    Jul 2, 2012
    Posts:
    20
    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:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [CustomEditor(typeof(MyScript))]
    5. public class MyScriptEditor : Editor
    6. {
    7.  
    8.     Vector3[] polyLine = new Vector3[] {
    9.         new Vector3 (0.0f, 0.0f, 0.0f),
    10.         new Vector3 (3.0f, 0.0f, 0.0f),
    11.         new Vector3 (4.0f, 2.0f, 2.0f),
    12.         new Vector3 (0.0f, 3.0f, 6.0f),
    13.     };
    14.  
    15.  
    16.     void OnSceneGUI ()
    17.     {
    18.         Handles.DrawPolyLine (polyLine);
    19.  
    20.         Vector3 mouse = HandleUtility.ClosestPointToPolyLine (polyLine);
    21.  
    22.         Handles.DrawSolidDisc (mouse, -Camera.current.transform.forward, 0.1f);
    23.  
    24.         HandleUtility.Repaint ();
    25.  
    26.     }
    27. }
    With the scene view in isometric projection, the disc is drawn under the mouse cursor (expected behavior):
    ClosestPointToPolyLine Iso.png

    But in perspective projection mode the disc is not under the mouse cursor (wrong behavior):
    ClosestPointToPolyLine Pers.png

    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.
     

    Attached Files:

  2. Xaurrien

    Xaurrien

    Joined:
    Jul 2, 2012
    Posts:
    20
    I have finally decided to fill a bug report, the case number is 694657.