Stuck on Navigation with TileMaps

I am on Godot 4.2.1 and also confused.
I’m using TileMap navigation layers with a NavigationAgent2D.
Here’s the result of my suffering:

Now, that isn’t really working. At all.
As far as I understand the end of that red line should be on my character. However, it isn’t. The tiles aren’t actually blue, they’re yellow, it’s just that I turned on visible navigation. But it kinda looks like the navigation of each tile overlaps with another? What? The Navigation Polygon of the tile doesn’t look like it’s going out of bounds:
So I have no idea what’s that about.
As for the red line, it kinda looks like the NavigationAgent2D sets the target_position locally, instead of globally like it says in the documentation.
Here’s my script for the enemy:

@onready var nav_agent: NavigationAgent2D = $NavigationAgent2D
@export var target: Vector2
const SPEED: int = 5000

func _physics_process(delta):
	velocity = velocity_to_target(target) * SPEED * delta

# Returns Vector2.ZERO when error
func velocity_to_target(target_in_func: Vector2) -> Vector2:
	# Required to be set so the is_navigation_inished() call works.
	nav_agent.target_position = target_in_func
	if not ai_active:
		return Vector2.ZERO
	if not target:
		push_error("velocity_to_target() called without target")
		return Vector2.ZERO
	if nav_agent.is_navigation_finished():
		return Vector2.ZERO

	return global_position.direction_to(nav_agent.get_next_path_position())

To me, it looks like the script should work, but, well, it doesn’t.
It’s my first time trying out navigation and I have no freaking idea what’s wrong.

1 Like

Indeed it looks like your TileMap cell navigation mesh has overflow.

This in turn makes the NavigationServer and navigation map unable to connect the navigation mesh edges to a combined surface. This makes your agent stuck “local” to a cell because nothing is connected, or due to the caused overlap, connected to the wrong edges.

The most likely cause for this is that you did draw an unprecise size with the Editor tool that has decimal points. Enable snapping and zoom in.

When the mesh is baked from your drawings it is rasterized to integer coordinates. So having a cell size of e.g. Vector2(10.5, 10.5) will not work, it will end up rounded which could also explain why you have what looks like a 1 pixel mismatch.

Huh, doesn’t really seem like that’s the problem.
I enabled the snapping and redrew the navigation poly, and the same thing happens.
I’m assuming the baking is done automatically, right? I’ve seen that you can bake things with a NavigationRegion2D, but that isn’t required with a TileMap, right?

The TileMap editor bakes a navigation mesh for a cell in the editor, and at runtime creates copies of this mesh and places it at each tile that uses it. It does not rebake or care about runtime changes like the NavigationRegion2D does. A NavigationRegion2D can bake a combined navigation mesh for many parsed objects, included an entire TileMap (Layer0). You need to change the entire cell with a TileMap if you want to change its build-in navigation mesh at runtime.

Okay, good to know! I wasn’t planning on changing the mesh at runtime, just wanted to clarify what baking means in this context.
Do you have any other idea what might be causing this?

I think I have found the issue.
It’s a bug in Godot itself. When using pixel art textures that are not divisible by 2 (like here, the sand is 25x25) Godot goes “hippity hoppity, I said NO!” and rounds the numbers up, resulting in an overflow beyond the tile. There is an open issue here. Also that’s exactly what’s happening in the code and I definitely didn’t understand something wrong in the issue.

And I’m not going to accuse @smix8 of trying to make me think I’m crazy, but assuming their username on GitHub is also smix8, they have literally commented on the GitHub issue. They knew the whole time. And yet, they decided to keep this a secret from me. They wanted me to go mad, they wanted people to think I have lost my mind, to hate me, to never accept me into society because I couldn’t draw a polygon in a 25x25 texture. It was a conspiracy all along! /s

That was a joke. Thanks for helping me anyways @smix8! I’m assuming you just forgot about the issue.

I think you are crazy but I dont mind you joking. I still need to (partially) correct you and add to it as other users might read this (years) later and grab wrong information or jump to false conclusions.

As mentioned in my first comment at the begin, the baking rounds because it runs on an integer “1 pixel” grid. This is not a bug as stated in the linked issue and other places.

The navmesh baking does not force any factor of 2 sizes. It is not a rendering texture. It is also not a TileMap cell or follows any restrictions and quirks of the TileMap editor and tile layouts (more to that below).

This can be easily verified by creating a 1 pixel source geometry for the baking, or drawing a 1 pixel sized outline with a NavigationRegion2D at zero agent radius, it will bake just fine.

The problem with a 25x25 pixel tile is not the uneven pixel size. You can bake such “tiles” all day with a NavigationRegion2D.

The problem with an 25x25 pixel tile is that a TileMap rearranges the geometry data for such an uneven pixel size internally around a tile center point that depends on the used tile layout. So depending on the center point things will end up with half pixel positions and those get feed to the navmesh baking.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.