#How to make NavigationAgent2D bypass corners in TileMap?
7 messages · Page 1 of 1 (latest)
extends KinematicBody2D
class_name Enemy
export var default_speed:float = 60
onready var target:KinematicBody2D
func _physics_process(delta):
if(target == null): return
move_towards_goal(delta)
func move_towards_goal(delta):
if($NavigationAgent2D.is_navigation_finished()): return
var points := $NavigationAgent2D.get_path()
var center:Vector2 = Vector2.ZERO
var point:Vector2 = $NavigationAgent2D.get_next_location()
var direction = global_position.direction_to(point)
$NavigationAgent2D.set_velocity(default_speed * direction * delta)
move_and_slide(default_speed * direction)
func _on_Area2D_body_entered(body):
if(body is Player): target = body
func _on_Timer_timeout():
if(target == null): return
$NavigationAgent2D.set_target_location(target.global_position)
A navigation mesh / polygon describes the traversable safe area for an agent with its center position at zero radius. If you want pathfinding to account for an agent's (collision) size you need to shrink the navigation mesh / polygon accordingly.
i use tilemaps
how do I shrink navigation polygon there?
difficult, tilemap or 2D in general has no really good way to merge all those little mini tile polygons together to a larger polygon. It does not make much sense to shrink / offset the tile polygon cause you only want the margin for your agent size between real geometry like walls and you walkable polygons and not everywhere on every single tile. That said there is work done on a 2D baking feature same as 3D already has that will automate this more or less but it will only arrive with Godot 4.1
if you have square tiles some uses try to work around this by reducing the size of the navigation polygon so it is small enough for their actors to not collide and then increase the navigation map edge connection margin so the polygons can still connect over distance but that only works with simple polygons.