How does pathfinding work in gadot 4 stable.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Master0987

I read the gadot docs but i could understand it as the navigation demo on github is outdated every video on youtube is almost outdated or completely outdated Ive been trying to from serveral weeks now but cant figure it out.
so please help
all and any help is appreciated

You’ve posted 3 questions about the same topic in the span of 4 hours.
Please don’t spam the Q&A.
If somebody knows, they will eventually give an answer. If nobody knows, they would have to go through the same trial and error as you to give you an answer.

zhyrin | 2023-03-16 20:15

:bust_in_silhouette: Reply From: zhyrin

I’ve created a demo for you, hope this helps: GitHub - zhyrin0/godot-2D-navigation-for-qa

Updated the project with an important comment.

zhyrin | 2023-03-16 21:39

thank you very much.

Master0987 | 2023-03-17 10:12


Thank you for a sample! I’ve been playing around with your sample for movement on the x axis.

I made a few changes to the script and was able to convert the movement to be on the x axis like below, but I imagine there is a better way than setting the enemies self as the target.

What would be an efficient way for an enemy to move back and forth on the x axis instead of targeting the player?

extends CharacterBody2D

@export var target: Node2D
@export var speed: float
@onready var agent := $NavigationAgent2D as NavigationAgent2D

var moving = false

func _ready() -> void:

func _physics_process(_delta: float) -> void:
	var direction = to_local(agent.get_next_path_position()).normalized()
	velocity = direction * speed
	if moving:

func _on_timer_timeout() -> void:
	moving = !moving

jukafah | 2023-03-18 18:14

You could place two Marker2Ds in your scene, each denoting the end of a path the enemy has to patrol. NavigationAgent2D has signals for when it finished/reached target, in a callback to one of those signals you can figure out which marker has been reached, and set the other marker as the new target.

zhyrin | 2023-03-19 10:54