I have started working on this simple Adventure RPG Game and came across the Navigation issue.
When Multiple NPC’s are placed and wants to go random places it quite tricky when one get behind the other one, they won’t seek alternative route around. Is there some setting in NavigationAgent3D which I could use to make them wait or go around to avoid blocking each other?
It’s build out of components so posting exactly relevant code would be
Finding valid random point on nav_mesh
class_name NPCWanderComponent extends Node
@export var nav_agent: NavigationAgent3D
var _nav_regions: Array[NavigationRegion3D] = []
func _ready() -> void:
for node in get_tree().get_nodes_in_group("nav_regions"):
if node is NavigationRegion3D:
_nav_regions.append(node)
_pick_new_target()
nav_agent.navigation_finished.connect(_pick_new_target)
func _pick_new_target() -> void:
if _nav_regions.is_empty():
return
var region: NavigationRegion3D = _nav_regions.pick_random()
var point := NavigationServer3D.region_get_random_point(region.get_rid(), 1, false)
nav_agent.target_position = point
and
turn this into move_dir( which is repurposed from Player Input used WASD to do it )
class_name NPCInputComponent extends InputComponent
@export var nav_agent: NavigationAgent3D
@export var body: Node3D
func update() -> void:
if nav_agent == null or body == null:
return
if nav_agent.is_navigation_finished():
move_dir = Vector2.ZERO
return
var next_pos := nav_agent.get_next_path_position()
var to_target := next_pos - body.global_position
move_dir = Vector2(to_target.x, to_target.z).normalized()
Tinkered with Agent Pathfinding values and Avoidance values, but couldn’t find sweet spot to make NPC use both ways, and stop from blocking each other.
trouble with checking travel direction is when target is at area where player should go from other way but NavigationServer calculated it from opposite Z direction, is there maybe way to add custom rules along different NavigationLayers ?