Skip to main content
GameDev.net gamedev.net

PRO Tired of ads? Read GameDev.net ad-free and help keep the community independent with GameDev Pro — $3/month.

Chromatic Galaxy, adding basic AI - Devlog 2

Chromatic Galaxy, adding basic AI - Devlog 2

Kupferrot
Kupferrot
Chromatic Galaxy · · 3 min read
14,914 1

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.

Recommended resources

Game Engines

See full guide
Unreal Engine 5 Best Practices cover
Editor pick

Unreal Engine 5 Best Practices

Amazon · Book

Written by multi-award-winning Unreal generalist Tyson J. Butler-Boschma, Founder and Creative Director of Toybox Games Studios, this book addresses common challenges you face when advancing your expertise in lighting, environment design, and cinematic storytelling.

GameDev.net may earn a commission if you purchase through these links. This helps fund the site at no extra cost to you.

GameDev.net may earn a commission if you purchase through these links. This helps fund the site at no extra cost to you.

Discussion

Loading comments...