is_target_reachable() massive lags
Asked Answered
P

1

0

Hey! is_target_reachable() in $NavigationAgent3D causes powerful lags. FPS down to 2-4 when monster can't reach me. Is this a problem with the engine, or am I using this function incorrectly?

My code:

extends CharacterBody3D
var player = null
var starting = false

func _ready():
	add_to_group("monsters")

func _physics_process(delta: float) -> void:
	if !starting: return
	$NavigationAgent3D.set_target_position(player.global_transform.origin)
	reached()

func _on_navigation_agent_3d_velocity_computed(safe_velocity):
	velocity = velocity.move_toward(safe_velocity, 0.25)
	set_up_direction(Vector3.UP)
	move_and_slide()

func set_player(p):
	player = p

func _on_timer_timeout():
	starting = true

func reached():
	var origin = global_transform.origin
	if $NavigationAgent3D.is_target_reachable():
		prints ("YES", randf())
		var target = $NavigationAgent3D.get_next_path_position()
		var velocitym = (target - origin).normalized() * 5
		$NavigationAgent3D.set_velocity(velocitym)
Pious answered 19/2, 2023 at 7:47 Comment(0)
H
0

This performance drop has more to do with your properly very large navigation mesh with a lot of polygons and edges.

You may not notice this performance problem in a normal path query when the position is close and reachable because in that case the pathfinding will move from the start position in the direction of the target position and exit as soon as it finds a path.

Now, when the target position is unreachable this becomes difficult. In order to know that a position is unreachable the pathfinding still needs to attempt to find a path. It tries until it runs out of polygons / edges to search before it gives up. Depending on your navigation mesh layout it may spend a lot of time searching through a large amount of available polygons and edges in your navigation mesh which causes the performance drop.

Herbivorous answered 2/3, 2023 at 0:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.