2D Toolkit Forum
2D Toolkit => Support => Topic started by: Artheus on November 05, 2013, 11:42:37 pm
-
Subject says it all. How do I get the tile position that's under the mouse in the scene view?
My current attempt looks like this:
void OnSceneGUI(SceneView sceneView)
{
Event e = Event.current;
int xx, yy;
if (e.type == EventType.MouseDrag && e.button == 0 && e.isMouse)
{
tileMap.GetTileAtPosition(tk2dCamera.Instance.camera.ScreenToWorldPoint(e.mousePosition), out xx, out yy);
}
}
Unfortunately, the world position from the camera changes anytime I pan and is completely different from the tile map world position. For example, even though my mouse is over tile (0,0), xx and yy depends on the position of the camera.
-
If you're trying to get it in scene view, that isn't going to work obviously, as you're reading the tk2dCamera.Instance. You will need to get the scene view camera -
SceneView sceneview = SceneView.lastActiveSceneView;
if (sceneview != null) {
Camera sceneCam = sceneview.camera;
-
How do you invert the Y axis for the scene view? The tile map origin is the bottom left and the scene cam origin is the top left so when I mouse over tile (0, 0), sceneCam.ScreenToWorldPoint(e.mousePosition) will return the (0, tile near top of camera) instead.
-
You don't need to do that. Once you get the point, ray cast into the tile map plane and then feed the intersection point into the tile map system.
Check tk2dTileMapSceneGUI.cs, UpdateCursorPosition
-
Ok, so I managed to jerry rig it to work but I had to comment out anything to do with layerDepthOffset...is that good or bad? I have no idea what tk2dTileMapEditorData editorData is or where to get it.
-
LayerDepth offset shouldn't make any difference, and you don't need editorData at all. You only need the ray cast + the tile map.GetTileAtPosition functions, nothing else.
-
Once again, thanks alot. I finally got my units to snap to the grid inside the editor window.
Btw, how does UpdateCursorPosition work? Are you basically creating a temporary plane over the tile map and then using a raycast hit to detect the mouse position?