Having trouble getting Custom Editor to get a world point.

Alright, I have a Grid script with a public Vector3 variable called mouseWorldPoint, and I want to update its position as I move about the Sceneview window.

Here is my code so far in the OnInspectorGUI() method:

if(SceneView.lastActiveSceneView.camera != null)
{
  Raycasthit hit;
  Ray worldRay = SceneView.lastActiveSceneView.camera.ViewportPointToRay(Event.current.mousePosition);
  Physics.Raycast(worldRay, out hit);
  grid.mouseWorldPoint = hit.point;
}

I wopuld have thought this would have done the trick, but it does not seem to be working. Any thoughts?

Ray ray = HandleUtility.GUIPointToWorldRay(e.mousePosition);

It works perfectly, I spent half a day to figure this out!

To get the current mouse position in world coordinates in the scene view I do the following:

Vector2 mousePos = Event.current.mousePosition;
mousePos.y = sceneView.camera.pixelHeight - mousePos.y;
Vector3 position = sceneView.camera.ScreenPointToRay(mousePos).origin;

While mousePosition reads coords from bottom to top, world coordinates are read top to bottom. Hence the use of the pixemHeight - mousePositionY.

Also note the use of pixelHeight rather than Screen.Height. This is due to the fact that, in the scene view, Screen.Height takes into account the bar above the actual window, causing a little deviation on the actual position on the screen.

Looks like that SceneView class isn’t in the official docs, but some quick Googling turns up a number of people trying to use it to access the scene camera; some people have also tried Camera.current, which I believe I’ve done once or twice in the past.

In this question thread, someone was trying to raycast from the scene camera and getting some very unusual results. You might check to make sure that you are firing a ray, and that it’s going roughly where you expect.

If you really can’t get this to work, I’ve had some luck before using Handles.PositionHandle() to get and set a point in scene.

Some other reading that might be useful:

Necro, but important because pops-up in google search.


You use ViewportPointToRay incorrectly - it should accept a vector with components between 0 and 1, for example new Vector2(0.015f, 0.95f);. Notice, that Event.current.mousePosition will have values like (1249, 123) etc, which clearly won’t fit into the 0-to-1 range.

You can squash it into that range, by comparing it to the width and height of your window (or your sceneView). For example (eventCurrMousePos.x / sceneView.position.width) and (eventCurrMousePos.y / sceneView.position.height)


Also, you need to flip Event.current.mousePosition before using it. Notice, SceneView has a ribbon-menu which occupies 30 to 18 pixels depending on your screen. This makes many people freak out and blame sceneView for producing incorrect values. Here is a fix:
https://forum.unity.com/threads/mouse-position-in-scene-view.250399/#post-3760579