- Home /
How can I achieve movement on hextile?
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:
public GameObject tileObject;
private Tilemap hexagon;
//Player mozgatás destination vektora
public Vector3Int moveFinal;
//Lépés per kör
public Vector3 move;
Vector3Int starting_Tile;
void Start()
{
//Tilamap
hexagon = tileObject.GetComponent<Tilemap>();
}
// Update is called once per frame
void Update()
{
// Pathfinding();
if (Input.GetMouseButtonUp(0))
{
//Where I clicked
Vector3 clicked = Camera.main.ScreenToWorldPoint(Input.mousePosition);
clicked.z = 0;
//Where I want to go
Vector3Int destination_Tile = hexagon.WorldToCell(clicked);
moveFinal = destination_Tile;
// The Y distance of the two tiles
int distance_y = destination_Tile.y - starting_Tile.y;
// The X distance of the two tiles
int distance_x = destination_Tile.x - starting_Tile.x;
Debug.Log("Kezdő Tile: " + starting_Tile + "Destination Tile: " + destination_Tile + "Y távolság: " + distance_y + "X távolság: " + distance_x);
//Starting position of the player
starting_Tile = hexagon.WorldToCell(transform.position);
}
//Neighbour Tiles of player position, if Y is even number
Dictionary<int, Vector3Int> szomszedok = new Dictionary<int, Vector3Int>();
szomszedok.Add(1, new Vector3Int(starting_Tile.x - 1, starting_Tile.y, starting_Tile.z));
szomszedok.Add(2, new Vector3Int(starting_Tile.x - 1, starting_Tile.y + 1, starting_Tile.z));
szomszedok.Add(3, new Vector3Int(starting_Tile.x, starting_Tile.y + 1, starting_Tile.z));
szomszedok.Add(4, new Vector3Int(starting_Tile.x + 1, starting_Tile.y, starting_Tile.z));
szomszedok.Add(5, new Vector3Int(starting_Tile.x, starting_Tile.y - 1, starting_Tile.z));
szomszedok.Add(6, new Vector3Int(starting_Tile.x - 1, starting_Tile.y - 1, starting_Tile.z));
//Neighbour Tiles of player position if Y is odd number
Dictionary<int, Vector3Int> szomszedok_paratlan = new Dictionary<int, Vector3Int>();
szomszedok_paratlan.Add(1, new Vector3Int(starting_Tile.x - 1, starting_Tile.y, starting_Tile.z));
szomszedok_paratlan.Add(2, new Vector3Int(starting_Tile.x, starting_Tile.y + 1, starting_Tile.z));
szomszedok_paratlan.Add(3, new Vector3Int(starting_Tile.x + 1, starting_Tile.y + 1, starting_Tile.z));
szomszedok_paratlan.Add(4, new Vector3Int(starting_Tile.x + 1, starting_Tile.y, starting_Tile.z));
szomszedok_paratlan.Add(5, new Vector3Int(starting_Tile.x + 1, starting_Tile.y - 1, starting_Tile.z));
szomszedok_paratlan.Add(6, new Vector3Int(starting_Tile.x, starting_Tile.y - 1, starting_Tile.z));
//Movement ???
for (int i = 0; i < 3; i++)
{
if (moveFinal.x == starting_Tile.x)
{
if (moveFinal.y > starting_Tile.y)
{
Vector3 moveing = hexagon.CellToWorld(szomszedok[3]);
transform.position = Vector3.MoveTowards(transform.position, moveing, Time.deltaTime * (float)0.5);
}
}
}
}
Your answer
Follow this Question
Related Questions
Find Tilemap Player is Currently At 0 Answers
how to shoot bullets from circumference of a circle ? 1 Answer
Still doing Running Animation? 1 Answer