Godot Version
4.6
Question
Navigation agents dont steer around each other properly, heres the code for the character
extends CharacterBody3D
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
@onready var navigation_agent_3d: NavigationAgent3D = $NavigationAgent3D
@onready var model: MeshInstance3D = $model
var curr_delta :float= 0.0
var activate = false
#=============================================================================================
func rand_pos_in_zone(min:float, max:float):
return Vector3( randf_range(min,max), 0.0, randf_range(min,max))
#=============================================================================================
func compute_lookat_basis(targ:Vector3):
var vecTo = targ - global_position
var len = vecTo.length()
if len < 0.05:
return
var look_targ = vecTo*2000+global_position
model.look_at(look_targ)
#=============================================================================================
func _unhandled_input(event: InputEvent) -> void:
if Input.is_action_just_pressed("ui_accept") :#and is_on_floor():
navigation_agent_3d.target_position = rand_pos_in_zone(-90.0, 90.0)
activate = true
#=============================================================================================
func _process(delta: float) -> void:
curr_delta = delta
# Handle jump.
if activate:
var next_pos = navigation_agent_3d.get_next_path_position()
var vecTo = next_pos - global_position
compute_lookat_basis(next_pos)
if navigation_agent_3d.avoidance_enabled:
velocity = -model.global_basis.z*SPEED
else:
_on_navigation_agent_3d_velocity_computed(velocity)
else:
navigation_agent_3d.target_position = rand_pos_in_zone(-90.0, 90.0)
velocity = Vector3.ZERO
activate = true
if not is_on_floor():
velocity += get_gravity()*delta
#=============================================================================================
func _physics_process(delta: float) -> void:
if not is_on_floor():
velocity += get_gravity()*delta
move_and_slide()
#=============================================================================================
func _on_navigation_agent_3d_target_reached() -> void:
activate = false
#=============================================================================================
func _on_navigation_agent_3d_velocity_computed(safe_velocity: Vector3) -> void:
velocity = velocity.move_toward(safe_velocity,curr_delta)
And the scene
extends Node3D
@onready var packed_scene = preload("uid://cy8ymn4lwyl3j")
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
for i in range(0,300):
var obj = packed_scene.instantiate()
add_child(obj)
obj.global_position.x = randf_range(-90.0, 90.0)
obj.global_position.z = randf_range(-90.0, 90.0)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
Can you define what that means? Your description is very vague.
yeah no problem. The navigation agents don’t avoid each other with a radius of 3m like specified in the avoidance settings for the navigation agent. I don’t understand how to configure the navigation agents or the navigation region. They are all finding paths perfectly, and they use simplified paths. I am hoping to get some information or a link to relevant information offering intuition on the topic of setting up avoidance so that they avoid each other.
As a note, they do seem to slightly wiggle sometimes when they are near. In the scene shown i have the camera following one of the agents, when I press the ‘enter’ key they find another path. I did not bother to check that the path was not inside an obstacle. They also find another path if they reach the end of their path. Also. the checkbox for 3d avoidance doesn’t seem to make any difference.
extends CharacterBody3D
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
@onready var navigation_agent_3d: NavigationAgent3D = $NavigationAgent3D
@onready var model: MeshInstance3D = $model
var curr_delta :float= 0.0
var activate = false
#=============================================================================================
func rand_pos_in_zone(min:float, max:float):
return Vector3( randf_range(min,max), 0.0, randf_range(min,max))
#=============================================================================================
func compute_lookat_basis(targ:Vector3):
var vecTo = targ - global_position
var len = vecTo.length()
if len < 0.05:
return
var look_targ = vecTo*2000+global_position
model.look_at(look_targ)
#=============================================================================================
func _unhandled_input(event: InputEvent) -> void:
if Input.is_action_just_pressed("ui_accept") :#and is_on_floor():
navigation_agent_3d.target_position = rand_pos_in_zone(-90.0, 90.0)
activate = true
#=============================================================================================
func _process(delta: float) -> void:
curr_delta = delta
# Handle jump.
#=============================================================================================
func _physics_process(delta: float) -> void:
if activate:
var next_pos = navigation_agent_3d.get_next_path_position()
var vecTo = next_pos - global_position
compute_lookat_basis(next_pos)
velocity = -model.global_basis.z*SPEED
else:
navigation_agent_3d.target_position = rand_pos_in_zone(-90.0, 90.0)
velocity = Vector3.ZERO
activate = true
if not is_on_floor():
velocity += get_gravity()*delta
navigation_agent_3d.velocity = velocity
#=============================================================================================
func _on_navigation_agent_3d_target_reached() -> void:
activate = false
#=============================================================================================
func _on_navigation_agent_3d_velocity_computed(safe_velocity: Vector3) -> void:
velocity = safe_velocity#velocity.move_toward(safe_velocity,curr_delta*60.0)
navigation_agent_3d.velocity = safe_velocity
move_and_slide()