- Home /
Raycasting in Unity Editor
I want to make sure that it's possible to ray-cast inside an editor script. I'm trying to debug this code; just in case someone sees an obvious mistake.
public class MouseEvent : Editor { public GameObject newTile;
void OnSceneGUI()
{
if (Event.current.type == EventType.MouseDown)
{
Ray ray = Camera.main.ScreenPointToRay(Event.current.mousePosition);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit, 1000.0f)) {
Debug.Log(Event.current.mousePosition);
Vector3 newTilePosition = hit.point;
Instantiate(newTile, newTilePosition, Quaternion.identity);
}
}
}
}
EDIT: Not sure what changed, but it started working. One thing I'm noticing is that because it's going off of the main camera instead of the editor camera I think it's missing just a little bit. How would I refer to the editor camera to do a ScreenPointToRay?
Answer by DoubleDouble · May 05, 2011 at 07:40 PM
It's fine to do Raycasting in the editor; remember if you do screen point to ray to use Camera.current instead of Camera.main and it will use the editor camera as long as the editor screen has focus.
Answer by sbsmith · May 26, 2016 at 07:53 PM
In Unity 5, I found that Camera.current didn't quite work (I saw the y coordinate flipped). Instead I used HandleUtility.GUIPointToWorldRay
I found this unusual since the documentation says that it uses the current camera to do the calculation.
// Called from OnSceneGUI in a subclass of Editor
// Camera.current did not work
//Ray ray = Camera.current.ScreenPointToRay( Event.current.mousePosition );
Ray ray = HandleUtility.GUIPointToWorldRay( Event.current.mousePosition );
RaycastHit hit;
if( Physics.Raycast( ray, out hit ) )
{
// do stuff
}
Thank you. I tried getting the ray through ScreenPointToRay()
on the sceneView camera, but both gave weird results. This, however, worked perfectly.
ScreenSpace has its origin at the bottom left corner while GUI space has its origin at the top left. In addition GUI spaces can be nested. So the event information may be relative to a sub area. "GUIPointToWorldRay" does "unclip" the local gui position and transforms it internally into screenspace and finally to a ray.
You may want to have a look at the HandleUtility as it has several other utility methods for editor program$$anonymous$$g including several ClosestPoint methods, "PickGameObject", "PickRectObjects" as well as the RaySnap method. Note that Physics.Raycast can only hit "colliders". The selection / pick methods can pick up any object based on its renderer or other things. If you're implementing an editor tool you usually want to use those ins$$anonymous$$d of Physics.Raycast unless you specifically need a collider and the hit information.
If you're interested in casting rays in the editor without needing a collider, check out this post https://forum.unity.com/threads/editor-raycast-against-scene-meshes-without-collider-editor-select-object-using-gui-coordinate.485502/
Answer by IgorAherne · Oct 07, 2018 at 08:33 PM
You could also do it through SceneView, but you need to flip the y-coordinate upside down.
SceneView also has an issue with the top ribbon-menu, which can therefore offset your coord down by 18 pixels, making it appear as an incorrect, unexpected value.
Here is the fix:
https://forum.unity.com/threads/mouse-position-in-scene-view.250399/#post-3760579
Your answer
Follow this Question
Related Questions
"add selected" editor script 1 Answer
Draw specific Object Inspector into Rect 1 Answer
Editor Window Views 0 Answers
Cancel build in PostProcessScene? 0 Answers