Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

2017 Tilemap System - Select tile with mouse (ingame)

Discussion in '2D' started by Wannabeuk, Nov 26, 2017.

  1. Wannabeuk

    Wannabeuk

    Joined:
    Feb 19, 2013
    Posts:
    62
    I'm trying to work out how to use the mouse ingame to click and get the tile that was clicked on, usually I would raycast to the collider and work it out from there, however my tilemap only has a collider for impassable blocks, the majority of the tilemap has no collider.

    Is there a way to do this without a collider, or would i need to create colliders for the full Tilemap and then do some checking for passable/impassable when collision happens?

    or am i missing some understanding that makes this a silly question?

    Thanks
     
  2. Wannabeuk

    Wannabeuk

    Joined:
    Feb 19, 2013
    Posts:
    62
    Ok, turns out it was the third option, I was overthinking it quite a bit. For those interested here is what i ended up with (Just a bit of debug for testing atm)

    Code (CSharp):
    1.  if (Input.GetMouseButtonDown(0))
    2.         {
    3.             Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    4.             Debug.Log(string.Format("Co-ords of mouse is [X: {0} Y: {0}]", pos.x, pos.y));
    5.             TileData tile = world.Tile((int)pos.x, (int)pos.y);
    6.  
    7.             if (tile != null)
    8.             {
    9.                 Debug.Log(string.Format("Tile is: {0}", tile.TileType));
    10.             }
    11.         }
     
  3. error911

    error911

    Joined:
    Jul 21, 2013
    Posts:
    6
    how to get the <world> component in line 5?
     
  4. Wannabeuk

    Wannabeuk

    Joined:
    Feb 19, 2013
    Posts:
    62
    world is not a component there, it's simply the name of the class i store an array of tiledata objects in.
     
  5. neuroTrophy

    neuroTrophy

    Joined:
    Jan 22, 2015
    Posts:
    11
    Hey, I'm a bit rusty and completely new to 2D and the new tilemap setup. I am trying to get it to where the tile underneath the cursor highlights (either placing a sprite atop that tiles coordinates or replacing the tile temporarily). I have the following code, though am unsure how to proceed. Would you be able to make any suggestions?

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Tilemaps;
    6.  
    7. public class TileSelect : MonoBehaviour {
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.        
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update () {
    16.         if (Input.GetMouseButtonDown(0))
    17.         {
    18.             Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    19.             Debug.Log(string.Format("Co-ords of mouse is [X: {0} Y: {0}]", pos.x, pos.y));
    20.             TileData tile = world.Tile((int)pos.x, (int)pos.y);
    21.  
    22.             if (tile)
    23.             {
    24.                 Debug.Log(string.Format("Tile is: {0}", tile.TileType));
    25.             }
    26.         }
    27.     }
    28. }
    29.  
    30. internal class world
    31. {
    32.     internal static TileData Tile(int x, int y)
    33.     {
    34.         throw new NotImplementedException();
    35.     }
    36. }
    I have gotten this far thanks to VS suggestions ha. I know. I have some reading and refreshing to do.
     
  6. SavedByZero

    SavedByZero

    Joined:
    May 23, 2013
    Posts:
    71
    This doesn't work all that accurately for me -- the (int) rounds the wrong way and often just misses the tile location. OF course, I have no idea what world is or how you retrieve TileData directly, so the one line different in my version is that I'm just getting the regular tileBase from the TileMap instead of that world.Tile call.

    General commentary: overall, I find the Tilebase class extremely frustrating; I expect it to act like an individual tile, but anything I change or add to it when I extend it ends up working like a static variable for every tile.
     
  7. Wannabeuk

    Wannabeuk

    Joined:
    Feb 19, 2013
    Posts:
    62
    Please note this was not for the Unity tilemap system, this was a 100% custom system. So it wont help you.
     
  8. Jackyjjc

    Jackyjjc

    Joined:
    Apr 12, 2015
    Posts:
    4
    Code (CSharp):
    1.  
    2.         public Grid grid; //  You can also use the Tilemap object
    3.         public void Update() {
    4.             Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    5.             Vector3Int coordinate = grid.WorldToCell(mouseWorldPos);
    6.             Debug.Log(coordinate);
    7.         }
    8.  
     
  9. ItsRhetorical

    ItsRhetorical

    Joined:
    Jun 16, 2017
    Posts:
    3
    Jackyjjc has it here. The earlier post by Wannabeuk isn't wrong, but it's overly specific. This will directly answer the question: "How do I get Tilemap cell coordinates from a mouse click?"
     
unityunity