Rigidbody2d Pathfinding

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Newby
:warning: Old Version Published before Godot 3 was released.

I want my enemy to go back and forth on any sized platform without falling off the side

:bust_in_silhouette: Reply From: eons

That is not pathfinding, is just “border check”.

Add raycasts to the sides pointing down, if going to the left, check the left one, if going to the right, check the right one.

If the raycast gives a “false” collision with the floor, is because there is no floor and the body need to turn to the other side.

The platformer demo uses that simple approach for the enemies.

:bust_in_silhouette: Reply From: rustyStriker

you can use the Test_motion function in the Rigidbody, just check if the enemy will collide in a position that is equal to his width(of the collision box/ sprite, what works best for you) and 1 to his height - 1 down( aka the vector should be (Width , Height - 1) to the direction he is going, that way, if it returns false you know that there is no ground there…

you can tweak the numbers, add or subtract some numbers or variables like speed to match your needs

:bust_in_silhouette: Reply From: jsena

You can use a “marker” to make the enemy go in the other direction. Look at Dolphin Island 2 project and you’ll see how they did it.
Dolphin Island 2 - Godot project - GitHub