Hi all. Once I’ve set up a Custom Inspector, I want to grab left-clicks and place a marker there. I use the Top-Ortographic view, then I throw a raycast from the ScreenPoint(Vector2) downward and use the hit point of the raycast as the desired marker position. Unfortunately, it’s not working.
It looks like the hit position is inverted.
public void OnSceneGUI()
{
RaycastHit hit;
Ray ray = SceneView.lastActiveSceneView.camera.ScreenPointToRay(Event.current.mousePosition);
if (Physics.Raycast(ray, out hit))
{
GameObject cube =(GameObject) Instantiate(Resources.Load("Marker"));
cube.transform.position = hit.point;
}
break;
}
Thanks in advance.
Finally I did it by replacing the class SceneView with the HandleUtility one, which performs perfectly my purpose of extending the editor
if (Event.current.type == EventType.MouseUp)
{
Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(worldRay, out hitInfo, 10000))
{
myCity.AddMarker(hitInfo.point);
}
}
Event.current.Use();
You probably want a groud for reference to make your results more reliable, or perhaps a distance to project into. Think of project a ray into space with nothing, there is no way to know how far into that space to stop and thats what I guess is causing your issues.
I usually setup a quad with the layer of ground for these sort of checks simply because its easier.
Otherwise if that doesnt help the explanation of your problem is a little vague so would be a good idea to elaborate on whats actually happening 