The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

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!

Godot version 3.4
in Engine by (17 points)

1 Answer

+1 vote
Best answer

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.

by (8,188 points)
selected by

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

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.