2d side scroller, adding a projectile mechanic to a moving enemy

Godot Version

4.4.1(stable)

To start off I followed this tutorial. https://www.youtube.com/watch?v=LOhfqjmasi0&t=4495s
It worked great and finished it up. Now I want to expand on what I’ve built by making the enemy slime able to detect the player and fire a projectile at them. (I made my own bullet asset to throw in). I’m not sure how to approach any of this though. I was thinking about a large collision shape to act as the slime’s range of sight.

I think I can figure out the shooting and bullet behaviour, etc. Mostly I’m just looking for best practice on how to make the enemy detect the player in a 2d side-scroller environment not a top-down, I think?

That would work fine. In fact I would probably do it this way initially too. However this does mean they will see through walls etc.

Alternatively you could just inject the player node reference into your enemies, and in code check their distance from you. But to avoid the seeing through walls, you could instead use a ray cast node to detect collisions with the player as well.

There are quite a few ways to do this, but a collision area initially is fine. If in the future you have performance problems with too many enemies (which is unlikely) you can always look for a more efficient method then.

2 Likes