Chromatic Galaxy, adding basic AI - Devlog 2

posted in Chromatic Galaxy
Published September 19, 2022
Advertisement

I would like to share my progress on the AI for the enemy ships, I added basic movement patterns, collision avoidance and aiming/shooting at the player. Have been working on this in the last days and I found the topic very interesting and fun to code. Would like to share with you how I did it.

Since I am using Unity I was first thinking about taking advantage of unity's NavMesh. I already have experience with this but since my game is in space and most of space is empty, I wanted to try something else. The only obstacles are asteroids and other ships, both of which can move around the level and maybe a stationary space station every now and then. Because of this I decided to instead give the enemy ships "eyes" by using raycasts.

Of course this didn't work out immediately I had to test different things and it was some trial and error involved. I made a small video about it, in case that you are interested:

The process looks like this:

1. AI get triggered and will follow the player
2. AI will keep a certain distance to the player and circle around him if the distance is sufficient
3. If the Raycasts detect objects in the way, the ship will adjust movement to the left or right (if both option are possible, it will take the shortest route to the player)
4. If the area is unpassable the ship will detect this and change objective to leaving this area before focusing on the player again. This happened very rarely in my tests but it can happen which is definitely the downside of not using a dedicated pathfinding algorithm.

So far this works pretty well, even if there are multiple ships around. I am aware that I might need to redesign this if the AI seems too dumb, but for now I can work with this and I did not experience major issues.
As I said, this didn't work in the first try. Especially when it comes to the raycasts I tested different options. I found it best to have a certain pattern of raycasts as you can see in following gif:

They ray's I use get shorter the more they point out to the side, also if I detect objects close by, the ship reduces it's movement speed and the rays are shortened, this way I found the ship to make more precise movements and navigate through tight gaps. The ray's are always pointing in the movement direction and not in the direction the ship is looking. It can move backwards while shooting at you and still avoid asteroids while moving backwards. You can see this in my video. The shooting part was easy, I just check if the line of sight is free of objects and other ships, if it is and the ship is in range, then it will fire.

I also had alternative idea's how to do it and might come back to them later if necessary. But for now I will test how far I can get with this system.

Thank you very much for reading and hopefully see you next time.

1 likes 1 comments

Comments

Kupferrot
September 19, 2022 03:34 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement