Raycasts and enemy movement.

4.3

I want to create an NPC that can detect the player, and try to walk towards them. Think of the Pokemon DS games where an NPC looks along the X or Y axis and then walks towards you and drags you into battle. That’s what I want. I’m brand new to coding/programming, so I’m pretty lost. I’m using raycast2D’s at the moment.
This is the code that I have currently, but I don’t know where to go from here to actually have my enemy walk towards the player. Any ideas would be greatly appreciated!


While you can use a raycast for this purpose, your case would be better served with an Area2D. When an enemy enters the Area2D, use a vector from the player to the enemy to get the direction and distance the enemy should move along to eventually reach the player.

1 Like