How to make units align with center of Tiles?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By System_Error

Hi all.

So I’ve got a scene where there are a handful of tanks, an obstacle, and a TileMap.
The tanks only move when they are individually selected, and given a destination.
What I want is for them to, when their destination doesn’t have anything else in it, move to the center of the tile at that (global) position. Otherwise, if they bump into another tank, or the obstacle, they go back to the previous empty tile.

The tiles for my TileMap are hexagons 256 pixels wide, and 223 pixels tall. Obstacles are centered on their tiles, and I want the tanks to be centered when they aren’t moving.

:bust_in_silhouette: Reply From: PepijnWe

This question seems a bit brought, this video might give you an idea what you can do:
In the second part there is grid-based pathfinding with collision avoidance.

I am using this grid as well and I think it has to be noted that, due to the way the movement is calculated, the path gets “smoothed out”. The arrival to a point in the path is validated by the distance of the entity to the point. Therefore the entity never really “arrives” at the point.

To achieve a more grid-like movement, you should decrease the MASS and ARRIVE_DISTANCE properties. I found that the following settings work well for me:

var MASS = 1.0

To “snap” the entity’s position to the grid, you could set the position after the entity arrives at the final point:

func _process(delta):
if not _state == STATES.FOLLOW:
var arrived_to_next_point = move_to(target_point_world)
if arrived_to_next_point:
    # Remember next_point before removing it
	var next_point = path[0]
	if len(path) == 0:
        # Set position to point in path
		position = next_point
	target_point_world = path[0]

ainab | 2020-06-28 18:11