0 votes

I Weird nav2d path loop

On Godot3.1
Keeps changing its mind about which path to take
not sure if this is a bug.
doesn't happen if the simple path has optimize=true
but I don't like the look of that path as much :(

func _ready():

    get_parent().get_parent().connect("placetile", self, "updatepath")

    self.position = get_parent().get_node("spawn").global_position
    goal = get_parent().get_node("end").global_position
    nav  = get_parent().get_node("Navigation2D")
    path = nav.get_simple_path(self.global_position, goal, false)
    get_parent().get_node("Line2D").points = PoolVector2Array(path)
    get_parent().get_node("Line2D").show()


func _process(delta: float) -> void:
    if !path:

        return
    if path.size() > 0:
        var d: float = self.global_position.distance_to(path[0])
        print(path)
        if d > 10:
             self.position =  self.global_position.linear_interpolate(path[0], (speed * delta)/d)
        else:
            path.remove(0)
in Engine by (15 points)

1 Answer

0 votes
Best answer

I think it might happen due to float precision, however I would suggest you don't recalculate the path every frame, just remember it in a variable so you have it once and for all. If it needs to update, just do it a few times per seconds instead, or when the character or its target moved by a minimum distance.

by (28,876 points)
selected by

Yeah I came to the same conclusion. Still it would be interesting to know what was behind such a strange behavior.

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.