Basis of an enemy AI!!!
the beginnings…
I feel like ten hours is a little much for just this, but there was so much work to do.
The main step began with a pretty simple area. It just detects if the player enters. The code uses the player position (or “target” position in the code), and subtracts it from the enemy position, if the numbers greater than 1 it moves one way and if less than -1 other. (The buffer in-between the 2 is just for smoothing)
If the player is within the hitbox, the enemy shoots out a ray cast towards the player. If it touches the player, it lets the enemy move, if it hits a box or the ground first it doesn’t, pretty simple, till you realize what can go wrong…
For clarification i have NO CLUE why any of what i did helped, but it did so yippee!!! The first problem was that the enemy would only detect the player if they were off the ground. I tried to fix this by forcing the ray cast to not interact with the Tile Map (which wouldn’t have worked anyways, because it still needs to so the objects can block the enemy’s sight). I set a piece of code to print what the enemy was interacting with, and it turns out it was the background? (I know, weird?) I seperated the background and collision ground into 2 tile layers, then turned of collisions for the back layer.
After that there was some glitchy stuff which you can see in the video a bit, but I fixed almost all of it with literally one if statement that only lets the player become a target for the code
(My code is a modified version from this website) / / / / /
Godot 3.0: Visibility with Ray-casting · KCC Blog
Comments 0
No comments yet. Be the first!
Sign in to join the conversation.