- Home /
OnSceneGui, How do you convert mouseposition in worldposition?
I am trying to draw a line from a handle to the mouse, but the returned world position of the conversion matrices is completely off.
Short example:
// btn is reflected upon gameobject
Vector3 mousePosition = Event.current.mousePosition;
mousePosition = SceneView.currentDrawingSceneView.camera.ScreenToWorldPoint(mousePosition);
Handles.DrawLine(btn.transform.position + Vector3.up, mousePosition);
I've read many forum posts and threads, all solutions didn't work.
Answer by Socapex · Dec 27, 2016 at 06:59 PM
Well it seems I was in the wrong direction, the HandleUtility class provides a raycast helper and you can extract the world position from the ray origin. Simple :)
Here is a working solution:
Vector3 mousePosition = Event.current.mousePosition;
Ray ray = HandleUtility.GUIPointToWorldRay(mousePosition);
mousePosition = ray.origin;
Handles.DrawLine(btn.transform.position + Vector3.up, mousePosition);
Cheers
Oh and if the drawing is laggy, you can use HandleUtility.Repaint();
You finaly get the answer that I am seraching since 5hours ;-; (I am making a tools to spawn tiles in editor mode and all the forum that I found, none help me (over 20 web page XD )) THX !!
Answer by RobAnthem · Dec 27, 2016 at 07:43 PM
This is what I use for painting objects onto Terrain, its not much different than what Socap said, but it works flawlessly for accurate raycasting.
Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(worldRay, out hitInfo, 10000))
{
if (hitInfo.collider.gameObject != null)
{
Handles.DrawLine(btn.transform.position + Vector3.up, Event.current.mousePosition);
}
}
Your answer
Follow this Question
Related Questions
Editor.OnSceneGUI() Does not produce results without object selection 0 Answers
Automatically update prefab instance 1 Answer
How does conrolID work with ShowObjectPicker? 1 Answer
stackoverflow on editor script. 0 Answers
Custom inspector editor - how to put new editor fields in a specific place 1 Answer