This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Hi friends!

Working on a top-down pixel action RPG, and I have an NPC who offers to help the player. If you say yes, they move around independently (there's a timer, and on timeout the NPC gets randomly assigned a new direction), but they stick close to you (if the distance between the NPC and the player is greater than 200, the direction is updated to move the NPC directly towards the player. Here's my code:

var player_relative_position = player.position - position
     if abs(distance.x) >= 200 || abs(distance.y) >= 200:
            direction = player_relative_position.normalized()

This works fine, unless there's an obstacle blocking the path between the NPC and the player - then it just gets stuck.

Any best practices on how to navigate around collisions, especially when it comes to following the player down narrow hallways? I suspect I need a RayCast2D here, but I've not worked with them a ton and I'm not sure how exactly to script it so they could help the NPC move around obstacles.

Thanks so much for any insights!

Godot version 3.2.3
in Engine by (172 points)

3 Answers

+1 vote

Pathfinding is actually a pretty complicated topic! What you're trying to achieve is not trivial.

Have you looked at any aStar examples? If you google Godot aStar you should find some good tutorials. I think gdquest has one.

You might also want to look at how boids work, for some basic avoidance algorithm.

Godot 4 has some promising new navigation features that I'm hoping to try soon.

If you're obstacles are simple and there's only one at a time, you might be able to get away with a simple avoidance algorithm

Another thing to consider is cheating... Like, if the NPC ends up off screen, then teleport them back on screen. The player won't notice, and you won't have to do any complicated pathfinding.
Or letting the NPC move through objects instead of being blocked by them
Depending on the object this can look/feel ok

In terms of raycasts, they can tell you if there's a collision in a specific direction. The question then becomes how do you decide what to do if there is a collision?

by (1,346 points)

Thanks, this is super helpful!

+1 vote

It sounds like you are going to need to delve into the exciting (and sometimes painful) world of path finding! Luckily, godot offers some great out of the box options to get path finding working. One such option is using the Navigation2D class. The fastest way to get a path out of this class is to supply it with a child TileMap that contains all areas that are NOT an obstacle, and then call something like this (untested code alert!):

func get_path(current_global_npc_position:Vector2, npc_global_position_goal:Vector2) -> Array:
    current_global_npc_position = $Navigation2D.get_closest_point(current_global_npc_position)
    npc_global_position_goal = $Navigation2D.get_closest_point(npc_global_position_goal)
    return $Navigation2D.get_simple_path(current_global_npc_position, npc_global_position_goal )

This will only get you so far, as there are lots of nuiances to path finding particular to each game's situation, such as optimization, main thread blocking issues, paths that are to optimized for the size of an NPC, how to get NPCs to actually follow a path without doing the charleston, and how to generate navigation tilemaps without losing your mind.

I recommend checking out this excellent youtube tutorial for more info:

by (283 points)

Appreciate this!

+1 vote

this might be too simple for what you are trying to do, but my solution would be have an array of positions from the player node, whenever they move.

if Input.is_action_pressed("move"):
     position += Vector2(x, y)

Have that list only like 35 positions long though, or whatever distance you want the NPC to stop at.

Have the NPC follow behind your player along this path, I'd say use position.movetoward(patharray[index], position.distance_to($player.position) * delta)
Then, when you have the NPC where you want it (when the player is stopped), set a timer, and once it runs out, let the NPC start moving around, but then go back to it's position once new elements start getting added to the array, once the character starts moving again.

I know this response is written kind of sloppy, but I hope you understood the basic gist.

by (45 points)

That's a really interesting possibility, thank you!

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.