+1 vote

I have a chase script for my enemy, kinematicbody2d, to chase the player and im using nav2d for its navigation, every time it calls the get_simple_path method the game lags for a bit and with more enemies it gets worse. I check to see if my script cause any lag by just setting a single path to follow and it went smoothly. When i check the get_simple_path it lagged even though im not using the variable i used to store it in.

Here is the chase script

onready var player = get_node("../Player")
onready var nav2d = get_node("../Navigation2D")

func chase():
    # if the path has more than one point
    if points.size() > 0:
        var distance = get_global_position().distance_to(points[0])
        var direction = points[0] - get_global_position() # direction of movement
        if distance > 5:
            movement(direction, SPEED)
        else:
            points.remove(0)
            movement(Vector2.ZERO, 0) # close enough - stop moving

here is how i get my path, i set it on a timer cause just placing it in _process caused to much lag.

func _on_Update_path_timeout():
points = nav2d.get_simple_path(global_position, player.global_position)

I dont know what is wrong cause when I try my script it in a new project it runs fine until there are too many enemies loaded in.

Im using a nav polygon for my path.

if any extra info is need please ask.

asked Feb 6 in Projects by Newby (383 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.