How to make enemies like Sparks from Mario

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

A lot of platformers have enemies that travel in a circle around a small platform, clinging to the platform all the way around. Like a little spider that crawls around the circumference of a platform.

This is a 2D platformer game with a lot of movement done using move-and-slide, but also some enemies that follow unique Path2D nodes, but I don’t want to have to make a Path2D for this enemy.

Is there a built-in way to figure out how/when to move the enemy? In a best-case scenario, you’d be able to place an enemy on a random platform and they would happily crawl over it.

:bust_in_silhouette: Reply From: Coxcopi

So I think the easiest way would be to have a raycast at the enemy thats slightly rotated,
so you can check if the platform end in front of the enemy or if its still there. If the raycast doesn’t hit anything, just make the enemy walk into the other direction, and if the raycast touches the platform, make it walk straight forward.

I think that makes sense. A diagonal raycast into the wall in the direction the enemy is moving in, with the direction vector rotating a degree inwards for every frame the raycast isn’t touching the block.

Things could get more complicated if the block is more like a wall, and the spark would have to turn away from the wall or else run into it. My guess is a raycast could fix that too though. I’ll stick with the simple version for this first part though. Thanks!

Kanor | 2020-08-04 00:01