- Home /
Placing markers over SceneView by mouse-clicking
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.
Answer by Fran-Martin · Feb 27, 2015 at 04:06 PM
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();
Answer by LaireonGames · Feb 24, 2015 at 09:14 PM
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 ;)
There is a terrain object below. The ray intersects, but not in the expected coordinates.
To sum up: I'm grabbing the click in the TOP2D-SceneView and launching a ray from this screenpoint to the 3D terrain.
I supose that the mistake is in this sentence:
Ray ray =SceneView.lastActiveSceneView.camera.ScreenPointToRay(Event.current.mousePosition);
The following image represents with GREEN TRIANGLES the screen coordinates where I made the click and the RED STARS where the markers were positioned
That line won't be your problem its what you do with it thats important. $$anonymous$$G that gives you the direction but not the distance.
After it you should do a standard Physic.RayCast and then make sure it looks at the right layers etc etc
Your answer
![](https://koobas.hobune.stream/wayback/20220613184221im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Find TextureCoordinates from plane with out raycast only using mouse position on plan 0 Answers
How to achieve a more accurate Mouse to WorldCoordinates & faster updating of object follow? 2 Answers
Raycast, ScreenToWorldPoint accuracy problem 4 Answers
Camera.main.ScreenToWorldPoint not outputting expected results. 1 Answer