Search Unity

How can i place a tile in a tilemap by script

Discussion in '2D' started by memosu2, Dec 10, 2017.

  1. memosu2

    memosu2

    Joined:
    Dec 10, 2017
    Posts:
    3
    I am very new to unity and c# and i am creating my first 2d game right now. I am trying to place a highlight sprite over my ground sprite. I basically want that the ground block that is in front of my player is highlighted so that the player could dig. Thats the second thing i would like help with: How can i Destroy specific tiles?
     

    Attached Files:

  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    EleDe83 likes this.
  3. memosu2

    memosu2

    Joined:
    Dec 10, 2017
    Posts:
    3
    Thanks, but how do let it know which tile i want to place ? And do I put the script in on to the tilemap I created?
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    As long as you have references to the tile, tilemap, and player, this script could live anywhere. But since it's based on your player's position, I would put the script on the player.

    Give your script a reference to the relevant tilemap, and then you can use Tilemap.WorldToCell to convert the player's position to the current cell they occupy. Then depending on which way your player is facing, you add one to that Cell position, and then use that position to SetTile.

    Something like this maybe:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Tilemaps;
    3.  
    4. public class HighlightInFront : MonoBehaviour
    5. {
    6.     public Tile highlightTile;
    7.     public Tilemap highlightMap;
    8.  
    9.     private Vector3Int previous;
    10.  
    11.     // do late so that the player has a chance to move in update if necessary
    12.     private void LateUpdate()
    13.     {
    14.         // get current grid location
    15.         Vector3Int currentCell = highlightMap.WorldToCell(transform.position);
    16.         // add one in a direction (you'll have to change this to match your directional control)
    17.         currentCell.x += 1;
    18.  
    19.         // if the position has changed
    20.         if(currentCell != previous)
    21.         {
    22.             // set the new tile
    23.             highlightMap.SetTile(currentCell, highlightTile);
    24.  
    25.             // erase previous
    26.             highlightMap.SetTile(previous, null);
    27.  
    28.             // save the new position for next frame
    29.             previous = currentCell;
    30.         }
    31.     }
    32. }
     
  5. memosu2

    memosu2

    Joined:
    Dec 10, 2017
    Posts:
    3
    Thank you so much!! I have tried this for several days now and now I finally understand. Thanks!
     
    LiterallyJeff likes this.
  6. Rafarel

    Rafarel

    Joined:
    Jul 21, 2017
    Posts:
    199
    LiterallyJeff likes this.
  7. UltraGaym

    UltraGaym

    Joined:
    Mar 29, 2019
    Posts:
    3
  8. UltraGaym

    UltraGaym

    Joined:
    Mar 29, 2019
    Posts:
    3
    @jeffreyschoch bro how can we select a tile that has to be replaced in the tilemap in game
     
  9. UltraGaym

    UltraGaym

    Joined:
    Mar 29, 2019
    Posts:
    3
    @jeffreyschoch please reply
     
  10. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You're better off starting a new thread with your question, I'm not available all the time and there are plenty of people here who can answer your question.

    Use the same methods as above to find a tile. Get a position (mouse position? under the player? selected using arrow keys?), convert it to a cell position which is a Vector3Int.

    Once you have that position, use SetTile to change the tile.
    https://docs.unity3d.com/ScriptReference/Tilemaps.Tilemap.SetTile.html
    https://docs.unity3d.com/ScriptReference/Tilemaps.Tilemap.html
     
    DotusX likes this.
  11. Deleted User

    Deleted User

    Guest

    Is there a way to erase a tile with script?
     
  12. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @Bilion

    That is not related to original question... which is "How can i place a tile in a tilemap by script" - ask it in your own question. That way it is also possible to find the question... so everyone can benefit.
     
    jordangrant3d and JustCarty like this.
  13. Deleted User

    Deleted User

    Guest

    Ok
     
  14. eduardofacunha

    eduardofacunha

    Joined:
    Feb 23, 2020
    Posts:
    1
    Code (CSharp):
    1. // erase previous
    2. highlightMap.SetTile(previous, null);
     
    DotusX likes this.
  15. Ottobed

    Ottobed

    Joined:
    Jul 6, 2020
    Posts:
    3
    thankk you so helpful