Player moving at different speeds depending on axis
Ok so I have implemented A* pathfinding on a isometric Y as Z tilemap. I use the algorithm to make a Stack which is my path, then I peek the first item an move towards it. Once there or basically there (I am using a distance check to see when I am very close (0.005f) to my destination) then I set my position as the target position, pop the stack and loop. I loop until the stack(path) is empty. This all works as expected but while moving my player the travel speed between tiles is either normal (what I expect) along the x axis, or almost instantaneous when traveling along the y axis. Just to be extra clear when I say X axis, it is the GRIDS x axis. As this is an isometric y as z map the x axis is diagonal (upper left to lower right) and the y axis is the opposite (lower left to upper right). I am using this code to do the move...
public void Move()
{
if (path.Count > 0)
{
Vector3Int target = path.Peek();
if (Vector3.Distance(tilemap.WorldToCell(transform.position), target) >= 0.005f)
{
transform.position = Vector3.MoveTowards(transform.position, tilemap.CellToWorld(target), 1f * Time.deltaTime);
}
else
{
transform.position = tilemap.CellToWorld(target);
Debug.Log(path.Count);
Debug.Log(path.Peek());
path.Pop();
}
}
else
{
Debug.Log("Path Empty");
}
}
All the debugging spits out the expected data such as path count, target location, transform location, etc.. and I check this every tile moved and it always correct in pointing at the next tile. But for some reason unknown to me, the player moves properly along the x axis (slides slowly from tile to tile) then along y axis almost teleports super fast from tile to tile. If anyone has run into this before or has any ideas I would love to hear from you. Cheers
Answer by tylerdtrudeau · Mar 19, 2021 at 03:47 AM
Well I figured it out.....kinda. If I use world coordinates right from the start instead of converting from tilemap coordinates to world coordinates, it seems to work just fine. Same movement speed in all directions. I still have no idea why it was doing different movement speeds on the x and y axis with grid coordinates but whatever.....its working. Here's what I changed.
if (path.Count > 0)
{
Node n = path.Peek();
Vector3 target = n.worldPosition;
if (Vector3.Distance(transform.position, target) >= 0.005f)
{
transform.position = Vector3.MoveTowards(transform.position, target, 2f * Time.deltaTime);
}
else
{
transform.position = target;
path.Pop();
}
}
If anyone actually figures out why it was being funky with grid coordinates I would love to hear why just out of curiosity.