Posted May 04, 2025 by Targol
#AI #Chase #Obstacle Avoidance #Raycast #2D
Hello!
I decided today to stop procrastinating on working on the combat mechanics and FINALLY created the placeholder of my first enemy. I decided to do vibe coding and spend about 3 days working on the scenario itself, thinking about the puzzle, strategic places to put obstacles, etc. I'm far from being a level designer, or any kind of designer to be honest, but it was good to find the answer to many "whys" that I had while choosing option A vs B. It is not perfect, I know, but at least it helped me to understand the foundations of level/game designer.
First choice I had to make when creating the enemy: Will it chase Cael right away, or be in patrol state until he is visible? Given the size of the room, I went with the first option. In my opinion, it gives urgency to the player and makes it more fun to play.
Now that the enemy mode is past us, what about the obstacles? Well, my first thought was to use NavMesh from Unity. Although it's made for 3D, they say it can also work for 2D. But, luck as I am, I couldn't make it work on my game. I don' t know if that was a problem with Unity 6 or my lack of deep knowledge on that, but it just didn't work. I was able to made some progress by by adding a RigidBody2D to my ground tilemap, but it couldn't detect the NavMesh Modifiers on my ostacles, so I gave up after trying for some time.
After trying a couple of other free options, I just decided to create my simple AI. After all, isn't this MVP to advance on my progress as game dev?! So, I went for that. The most difficult part was to think about a way to detect if the enemy was looking to any of the 8 directions, but I quickly realized that it wasn't needed. All I had to do was to include a circle raycast to cover all 8 directions and identify which was had a hit. If you have a hit on the UP raycast, for example, make your enemy go left, something like that. There is a catch here -- There might be some cases that although your raycast can't detect anything, the obstacle's collider is still there, so I found out that using a circle instead of a line is way better to cover those corner cases (pun intended).
That was it for today. Now, my first enemy is chasing Cael non-stop and avoiding obstacles somewhat smoothly.
I will start working on the real combat system tomorrow by adding HP, damage, spawn time etc.
Targol.