Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Tilemap driving me crazy, help please!

Discussion in '2D' started by Tyrannastrasz, Nov 28, 2018.

  1. Tyrannastrasz

    Tyrannastrasz

    Joined:
    May 8, 2017
    Posts:
    8
    Hello everyone, I'm really reluctant to post questions in the forums, as I consider myself a noob at coding and can feel dismayed asking something which might in the end turn out to be a minor oversight, the pointing out of which might annoy some experienced game developers I suppose. Hence the reason behind my basically non-existent forum activity. Nevertheless, for the past few days now I've been hitting my head against a wall trying to make this work, and I'd appreciate any help that anyone could provide me. Anyways, here goes:
    I've started working on my first project ever, decided I want to make it a 2d game using the tilemap feature to build the world and then have certain tile types be interactable in some way. After what feels like days of reading up on tilemap documentation, watching tutorials (Brackeys <3) and seemingly endless cycling of frustrated quitting and taking reinvigorated new shots at solving my issue, I've decided to ask here.

    This is essentially my code, all I'm trying to get it to do for now is check which tile was clicked, and then log it into the console.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Tilemaps;
    5.  
    6. public class CellController : MonoBehaviour
    7. {
    8.     public Tilemap map;
    9.     public TileBase newTile;
    10.     private TileBase tileClicked;
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         if (Input.GetMouseButtonDown(0))
    16.         {
    17.             Vector3 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    18.             Vector3Int v3intworld = Vector3Int.FloorToInt(worldPoint);
    19.             tileClicked = map.GetTile(v3intworld);
    20.  
    21.             Debug.Log(v3intworld);
    22.             Debug.Log(map.HasTile(v3intworld));
    23.             Debug.Log(tileClicked);
    24.  
    25.         }
    26.     }
    27. }
    Nothing complex, just a simple script to check whether the cell that was clicked contains a tile. However, the HasTile and tileClicked debugs report false and null, respectively, which puzzles me to no end cause I've used the palette feature to draw a tilemap in the scene view and just fill the entire camera scope with tiles, so it should debug true and the object type into console instead. The only reference I could find on this was Unity's scripting reference online, there's not much other info on this I was able to find. But as far as I know, I've referenced the variables and used methods correctly, so why is it not printing the results correctly?

    EDIT: If I add a map.SetTile() method that would use the newTile variable to instantiate that specific tile into a cell, HasTile and GetTile start functioning properly, which just adds to my conundrum.

    Thanks in advance for any help you can provide. Much love and respect!
     
  2. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    556
    Unfortunately, the TileMap system seems to have incomplete documentation.

    I don't have Unity in front of me right now, but I believe the Z-coord in v3intworld might be causing the issue. For my own tilemaps, the Z-coordinate is always zero. I believe you may be passing in a Z-value that will not match a tile position on the tilemap. My apologies if this is incorrect as I can only provide conjecture at the moment
     
    Tyrannastrasz, rakkarage and vakabaka like this.
  3. Tyrannastrasz

    Tyrannastrasz

    Joined:
    May 8, 2017
    Posts:
    8
    Oh my god! You are a lifesaver! xDD Guided me in the right direction.. You're right, that sneaky Z-axis was causing the issue. For anyone with a similar issue in the future, I will detail what was causing it.
    In 2d mode, main camera goes into 0,0,-10 by default. ScreentoWorldPoint will actually mimic X and Y values as you move the mouse around left and right, but it will take camera's Z-value, which will make the console print 'false' and 'null' on my methods because the actual tilemap is at 0,0,0. One way to offset it just at the top of my mind is to use a new Vector3Int to add a positive z-value equal to main camera's negative one which will bring the total z-value back to 0 (z-position of the tilemap).

    Thank you, spryx <3
     
    spryx likes this.