- Home /
How to get the 3d mouse position in the editor window
I know that I can use Event.current.mousePosition to get the mouse position, but the problem is it only returns a Vector2, and I need a Vector3 to cast a ray. I am trying to place objects in a x,z grid in the scene view.
Answer by 3ddave · May 26, 2015 at 04:02 AM
Unity has built in functions for casting a ray into the scene based on a screen point. See the example code on Input.mousePosition.
You can get more information on the hit by providing a RaycastHit object to the function call like so
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit)){
//ray hit object, info stored in hit
Debug.Log("Hit obj: " + hit.transform.gameObject);
}else{
//nothing hit by ray
Debug.Log("Click missed");
}
The problem is that input.mousePosition only works in the game view when the game is running. I am trying to do it in the scene view with out the game running. I have a grid laid out on the x,z plane, the current code that I am using works in the x,y plane. It is similar to the code you presented except it uses Event.current.mousePosition ins$$anonymous$$d of input.mousePosition. since these are the only too ways i might have to do my editing in the x,y plane and then rotate it to the x,z plane when the game starts, or maybe try to see if there is a way to make z be up in the physics system, or just completly turn gravity off and use my own gravity code.
O$$anonymous$$, I see. It appears that might be a bit trickier. I'm having trouble visualizing exactly what you are trying to do but perhaps the following resources will guide you. OnSceneGUI Scripting - This is an old post, but uses code similar to $$anonymous$$e only calling HandleUtility.GUIPointToWorldRay for the ray casting. It sounds like this person has achieved essentially what you are trying to do (position things in the scene view in 3D). If that doesn't work, you might try your second approach which is to modify the orientation of the scene view. This can be done but seems to utilize undocumented features (so use at your own risk). See Orthographic scene view and $$anonymous$$anipulating the Scene View from script. Hope that helps.
Answer by ktmarine1999 · May 30, 2015 at 11:31 AM
Basically what I am trying to do is build a 3D map builder for the scene view. I have a bunch of modular pieces, i.e 2 different types of floor tiles, 2 Wall types, a door. I have a grid drawing on the screen that is 20 tiles by 20 tiles, with each tile being the same width and length of my floor tile. I originally used Input.mousePosition, like you suggested but it was not working. After some research found out that Input.mousePosition only works when the game is running I then came across the 2D Tilemap Starter Kit which is close to what I was doing and found that they used Event.current.mousePosition to get the mouse position, my editor was working, sort of, the x, and y worked perfect but my selection box wasn't moving in the z direction, Back to the documentation I went, maybe I wasn't using it correctly. Come to find out it returns a vector2 not a vector3 which is why it worked on the x,y dug through the GUI Scripting guide and was left with no solution, doing an internet search gives you Input.mousePosition, I did stumble across a post where someone was using a different api stack and sending it the window handle and got it working, no details. so it is to the community with this question hoping someone some place might know of a way to to do 3D picking in the s Scene View while not in play mode. I know there are some things that are not in the scripting api reference, i.e I am using UnityEditor.SceneView.currentDrawingSceneView.camera.ScreenPointToRay there is nothing for currentDrawingSceneView but the camera works like a normal camera, when you do a search for SceneView you get results for the MouseCursor and that's it. I was really hoping that there was something similar for getting a vector3 for the mouse position. If there is not then I think I will just rotate everything 90 degrees on the x axis when the game starts. Kinda of a hack but should work, there is all kinds of documentation about rotating the transform of objects when the game is up and running.
Your answer
Follow this Question
Related Questions
Why does the scene view camera move too quickly? 0 Answers
Scene Editor: panning with mouse pops when cursor goes offscreen 0 Answers
Known issue with Unity Editor on Surface Pro? 3 Answers
How to transform objects along last-used axis using middle mouse and drag? 0 Answers
Using mouse for realistic Top-Down space ship yaw control 1 Answer