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!