Hello Guest

Author Topic: Get tile position under mouse in the scene view.  (Read 6102 times)

Artheus

  • Newbie
  • *
  • Posts: 19
    • View Profile
Get tile position under mouse in the scene view.
« 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:

Code: [Select]
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.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Get tile position under mouse in the scene view.
« Reply #1 on: November 06, 2013, 12:02:02 pm »
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 -

Code: [Select]
SceneView sceneview = SceneView.lastActiveSceneView;
if (sceneview != null) {
Camera sceneCam = sceneview.camera;

Artheus

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Get tile position under mouse in the scene view.
« Reply #2 on: November 07, 2013, 09:31:21 pm »
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.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Get tile position under mouse in the scene view.
« Reply #3 on: November 07, 2013, 09:45:57 pm »
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

Artheus

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Get tile position under mouse in the scene view.
« Reply #4 on: November 07, 2013, 10:22:13 pm »
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.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Get tile position under mouse in the scene view.
« Reply #5 on: November 09, 2013, 09:58:30 am »
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.

Artheus

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Get tile position under mouse in the scene view.
« Reply #6 on: November 09, 2013, 08:32:50 pm »
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?
« Last Edit: November 09, 2013, 08:45:05 pm by Artheus »