i’m trying to write a small script to place the selected object using getting the mouse coordinates, transforming it into a ray using the editor cam, casting a ray in the scene and placing it to the hitresult if existing.
my main problem here is that Input.mousePosition always gives me 0/0/0 - anyone has any idea why this doesn’t work in editor or how to get the correct mousePosition?
thx very much
manni
here is my current code
@MenuItem (“GameObject/Place %i”)
static function DoSomething () {
if (Selection.activeGameObject)
{
var cPosition = Selection.activeGameObject.transform.position;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
// get intersection between ray and physics
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 1000.0))
{
Selection.activeGameObject.transform.position = hit.point;
print(“Placed Object to” + hit.point.x + “/” + hit.point.y + “/” + hit.point.z);
}
Debug.Log (“Perform operation”);
}
}
okay, my script was trash anyway, i’ve updated it now to place the selected element into the middle of the current view, casting a ray into the scene. this works fine, but still the previous problem of getting the mouse coordinates exists, seems i’m too stupid to check how the those…
// Input.mousePosition not working, how can i get that mouseposition in editor mode?
// var ray = Camera.current.ScreenPointToRay (Input.mousePosition);
var ray = Camera.current.ScreenPointToRay (mousePosition);
// get intersection between ray and physics
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 1000.0))
{
Selection.activeGameObject.transform.position = hit.point;
print(“Placed Object to” + hit.point.x + “/” + hit.point.y + “/” + hit.point.z);
}
else
print(“no collision found”);
}
else
print(“no selected gameobject or current camera found”);
}
To get the mouse position you have to listen for an event inside of an OnInspectorGUI, which can be accessed through an editor script (there is information how to do this).
Then get your mouse coordinates by using Event.mousePosition.
To convert these to your editor’s window, you have to account for the y axis flip, and the extra space around your editor window.
SceneView.onSceneGUIDelegate += OnSceneGUI;
where should i write this ?
My editor script doesnt have a Start or awake function allowed …
inside the Editor Constructor?