0
\$\begingroup\$

I need a simple script which does the following:

When I left-click somewhere in the Editor-Scene:

(doesnt matter if there is an object under the cursor- or not)

Do NOT deselect the current selection in the Inspector. Do not select the object under the cursor. and

Just print the mouse X and Z interception with the Y-Plane in the Debug.Log();

i stumbled on many solutions but somehow i couldnt get this to work.

i tried:

HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));

and

Tools.current = Tool.None;

both seem to do nothing ..... X.x

EDIT:

Happens that "HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));" needs to be inside: public void OnSceneGUI() and called every frame - to "block" the mouse clicks on the editor. i thought it toggles it off.

still the problem of the mouse coordinates persists.

\$\endgroup\$

1 Answer 1

-1
\$\begingroup\$

I solved my little problem. This is the solution I came up with:

Ray ray;
RaycastHit hit;

ray = Camera.current.ScreenPointToRay(new Vector3(Event.current.mousePosition.x, SceneView.currentDrawingSceneView.camera.pixelHeight - Event.current.mousePosition.y));

if (Physics.Raycast(ray, out hit))
{
    x1 = Input.mousePosition.x;
    y1 = Input.mousePosition.y;
    z1 = Input.mousePosition.z;
    x2 = hit.point.x;
    y2 = 0;
    z2 = hit.point.z;
}

This script gets the mouse coordinates on the y-plane if there is a collider on the y-plane.

x2,x2,z2 are the result coordinates.

\$\endgroup\$
1
  • \$\begingroup\$ For custom editor code you should use HandleUtility.GUIPointToWorldRay() instead of ScreenPointToRay(). \$\endgroup\$
    – Droppy
    May 12, 2018 at 15:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .