Move a KinematicBody to a target using a Navigation.get_simple_path:

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

Hi,
How I can move a KinematicBody to a target using Navigation.get_simple_path function and GDScript in a 3D space?
Thanks in advanced.

:bust_in_silhouette: Reply From: timothybrentwood

Here is a 2D analog for what you’re trying to accomplish:

onready var nav = $Navigation2D
var destination = null
var navigation_path = []

func _physics_process(delta: float) -> void:
	if destination:
		move_towards_position(destination, delta)
		# if we're close enough, call it good
		if self.position.distance_squared_to(destination) < 5:
			self.position = destination
			destination = null
	elif not navigation_path.empty():
		destination = navigation_path.pop_front()
		
func _input(event: InputEvent) -> void:
	var start_position = self.position
	var end_position = get_end_position(event)
	var path = Array(nav.get_simple_path(start_position, end_position))
	if path.empty():
		return
	else:
		var always_starting_position = path.pop_front()
		navigation_path = path

Obviously, you’ll need to implement move_towards_position() and get_end_position(). Something to keep in mind: get_simple_path() wants and returns points in local coordinate space, if i recall correctly, moving in 3D generally wants points in global space.