2D pathfinding with KinematicBody2D using move_and_slide

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

Hi,

I am currently working on a 2D project, and would like to implement pathfinding to my point and click movement. I am having trouble however implementing it, and can’t figure out how to get it to work. The situation is as follows:

This piece of code is in root, and it works just fine in producing a clear line of the path that the player should travel. Now I just need to make the player move across the path.

extends Node2D

func _unhandled_input(event):
	if event.is_action_pressed("click"):
		var path = get_node("Navigation2D").get_simple_path(get_node("YSort/Player").position, get_node("Navigation2D").to_local(get_global_mouse_position()))
	get_node("Line2D").points = path
	get_node("YSort/Player").path = path

This is my current movement code, without pathfinding. It is done using a youtube tutorial, which I can’t seem to find for some reason.

func _unhandled_input(event):
	if event.is_action_pressed("click"):
		moving = true
		target = get_global_mouse_position()
		
#Method for moving the character
func checkMovement(delta):
	if moving == false:
		speed = 0
	else:
		speed += accelaration
		if speed > max_speed:
			speed = max_speed
	velocity = position.direction_to(target) * speed
	moveDirection = rad2deg(target.angle_to_point(position))
	if position.distance_to(target) > 5:
		velocity = move_and_slide(velocity)
	else:
		moving = false

func _physics_process(delta):
	checkMovement(delta)
	playAnimation()
	velocity = position.direction_to(target) * speed
	if position.distance_to(target) > 5:
		velocity = move_and_slide(velocity)

Any tips on how to implement the pathfinding? I have watched many tutorials and scoured the internet for solutions, but all of them seem to be using different way of implementing movement, and I would not like to change my movement because I have implemented other functions to it, including animations.

Any help is greatly appreciated!

:bust_in_silhouette: Reply From: Inces

So You already have a path and a movement code and You want to combine it ?
Your movement code seems fine and it will combine eaisily. You will need to modify input event mostly. Input should get a path and pass it to movement function. You formerly used target as high scope argument for checkmovement, do it with your path now. Checkmovement should now have additional lines to extract current target from path. Make target private variable for checkmovement function that equals path[0] as long as path is not empty. When positions distance to target is LESS than 5 remove element 0 from path. This will automatically move all elements of path array to the front, so next point of path will become new element 0, so checkmovement will still be going, towards next point.
Finally, boolean “moving” should now be true or false depending on if path is empty.

Thanks for the answer! I got it to work +1!

Aaruli | 2022-01-31 01:33