Search Unity

Hexagonal TileMap Movement Issue (using New Tilemap System)

Discussion in '2D Experimental Preview' started by RichardWyeth, Apr 7, 2019.

  1. RichardWyeth

    RichardWyeth

    Joined:
    Apr 7, 2019
    Posts:
    1
    Hello guys,

    I'm new to Unity 2D, and I'm completely stuck with my experiment creating movement on my hexagonal tilemap. So far I managed to create a movement for the player, which works on mouse clicks. For short, I click to a tile, and the player goes there, exactly to the middle. This is what I wanted to achieve, and so far so good.

    Next I wanted to create something of a path finding system for the player, and I tried to look for an example using this new system, but I couldn't find any. Right now, I have something of a half solution. My player goes to the tile, finds his way, but only if clicked as much on the tile, as much steps the player needs to do.

    In short, the movement's not going trough, only step by step.

    I realized, that probably my starting position not updateing correctly, (right now it updates on mouseclick, provideing the solution above), but if I update it anywhere else ( f.e. at the begining of void Update,) the player will go to the tile automatically, but not to the exact middle, only to the edges.

    I only tested the pathfinding, if we are going to a destination of the same x but different y value.

    Can you help me please?

    And if you can give me a tutorial for pathfinding like this, I would be grateful. I now there is an A* algorithm or what, but I didn't really understand it first, and now about catlikecoding, and quill youtube series, but they use their own presets, and the new tilemap system.

    Thank you.

    Here is my code:

    Code (CSharp):
    1.  public GameObject tileObject;
    2.     private Tilemap hexagon;
    3.  
    4.     //Player mozgatás destination vektora
    5.     public Vector3Int moveFinal;
    6.     //Lépés per kör
    7.     public Vector3 move;
    8.  
    9.     Vector3Int starting_Tile;
    10.  
    11. void Start()
    12.     {
    13.         //Tilamap
    14.         hexagon = tileObject.GetComponent<Tilemap>();
    15.     }
    16.  
    17.  
    18. // Update is called once per frame
    19.     void Update()
    20.     {
    21.         // Pathfinding();
    22.  
    23.         if (Input.GetMouseButtonUp(0))
    24.         {
    25.             //Where I clicked
    26.             Vector3 clicked = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    27.             clicked.z = 0;
    28.  
    29.  
    30.  
    31.             //Where I want to go
    32.             Vector3Int destination_Tile = hexagon.WorldToCell(clicked);
    33.             moveFinal = destination_Tile;
    34.  
    35.            // The Y distance of the two tiles
    36.             int distance_y = destination_Tile.y - starting_Tile.y;
    37.            // The X distance of the two tiles
    38.             int distance_x = destination_Tile.x - starting_Tile.x;
    39.  
    40.             Debug.Log("Kezdő Tile: " + starting_Tile + "Destination Tile: " + destination_Tile + "Y távolság: " + distance_y + "X távolság: " + distance_x);
    41.  
    42.             //Starting position of the player
    43.             starting_Tile = hexagon.WorldToCell(transform.position);
    44.  
    45.  
    46.         }
    47.  
    48.         //Neighbour Tiles of player position, if Y is even number
    49.         Dictionary<int, Vector3Int> szomszedok = new Dictionary<int, Vector3Int>();
    50.         szomszedok.Add(1, new Vector3Int(starting_Tile.x - 1, starting_Tile.y, starting_Tile.z));
    51.         szomszedok.Add(2, new Vector3Int(starting_Tile.x - 1, starting_Tile.y + 1, starting_Tile.z));
    52.         szomszedok.Add(3, new Vector3Int(starting_Tile.x, starting_Tile.y + 1, starting_Tile.z));
    53.         szomszedok.Add(4, new Vector3Int(starting_Tile.x + 1, starting_Tile.y, starting_Tile.z));
    54.         szomszedok.Add(5, new Vector3Int(starting_Tile.x, starting_Tile.y - 1, starting_Tile.z));
    55.         szomszedok.Add(6, new Vector3Int(starting_Tile.x - 1, starting_Tile.y - 1, starting_Tile.z));
    56.  
    57.         //Neighbour Tiles of player position if Y is odd number
    58.         Dictionary<int, Vector3Int> szomszedok_paratlan = new Dictionary<int, Vector3Int>();
    59.         szomszedok_paratlan.Add(1, new Vector3Int(starting_Tile.x - 1, starting_Tile.y, starting_Tile.z));
    60.         szomszedok_paratlan.Add(2, new Vector3Int(starting_Tile.x, starting_Tile.y + 1, starting_Tile.z));
    61.         szomszedok_paratlan.Add(3, new Vector3Int(starting_Tile.x + 1, starting_Tile.y + 1, starting_Tile.z));
    62.         szomszedok_paratlan.Add(4, new Vector3Int(starting_Tile.x + 1, starting_Tile.y, starting_Tile.z));
    63.         szomszedok_paratlan.Add(5, new Vector3Int(starting_Tile.x + 1, starting_Tile.y - 1, starting_Tile.z));
    64.         szomszedok_paratlan.Add(6, new Vector3Int(starting_Tile.x, starting_Tile.y - 1, starting_Tile.z));
    65.  
    66.  
    67.  
    68.         //Movement ???
    69.         for (int i = 0; i < 3; i++)
    70.         {
    71.          
    72.  
    73.             if (moveFinal.x == starting_Tile.x)
    74.             {
    75.                 if (moveFinal.y > starting_Tile.y)
    76.                 {
    77.                     Vector3 moveing = hexagon.CellToWorld(szomszedok[3]);
    78.                     transform.position = Vector3.MoveTowards(transform.position, moveing, Time.deltaTime * (float)0.5);
    79.                 }
    80.             }
    81.         }
    82.  
    83.     }
     
    Last edited: Apr 7, 2019
  2. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    303
    You can find old code for A* pathfinding with a rectangle/isometric Tilemap here at https://bitbucket.org/Unity-Technol...s?at=PreviewR401&fileviewer=file-view-default with weights set in the Tiles. Hopefully, the project there can serve as a guide for what you are doing.

    For hexagonal Tilemaps, there are 3 axes where the player can go if you are walking from Tile to Tile in the center (eg. top, top right and bottom right). The code above can be modified to give directions in these 3 axes instead of 2 (x and y).