Flying Enemy Obstacle Avoidance
Project Overview
This project was created as an Artificial Intellegence Artefact for my second year of Univercity. The Module (COMP250 Individual Specialist Computing Project: Artificial Intelligence) provided by Falmouth univercity is a module where we were required to propose and create an AI that can be used within a game. I chose to create an flying movement system that would be able to navigate between all kinds of objects while in the air. This enemy was inspired by an enemy called Rakk from Borderlands 2.
This enemy was desined to be an irritating flying bird like creature that has the functionality eighter of dive bombing the player or shoot the with a slime projectile. Because of all th high obsticles that we tower in Interspersed I needed to have the enemy, called Carrier, avoid bumping into obsticles while sicessfully following the player around the map.
How does it work
The obsticle avoidance is quite simple when brocken down. It works by utilizing raycasts. There are four raycasts that are fixed pointing straight forward from the current position of the Carrier. These raycasts work to simply navidate which direction it needs to rotate.
- Left Raycast triggered -> Turns Right
- Right Raycast triggered -> Turns Left
- Top Raycast triggered -> Turns Down
- Botton Raycast triggered -> Turns Up
This covers the most basic of scenarios, but the enemy would not last long without colliding into an obstacle. If there were two or more raycasts that are detecting objects the enemy would not go in the opposite direction of all the detected objects. This was the next thing that needed to be fixed. A lot of considerations were made and there were two prototypes of how to fix this issue. But at the end nighter of them were satisfactory as they would have taken too much time. In the end the method used to fix this issue was to get the detected objects and find the avrage opposite direction from all of the detected obstacles. This way the system would not have to use any "if" statements for the mevement decisions. By doing that avrage calculation the obsticle avoidance would automatically recieve the following movement conditions:
- Left Raycast triggered and Top Raycast triggered -> Turns Bottom Right
- Left Raycast triggered and Botton Raycast triggered -> Turns Top Right
- Right Raycast triggered and Top Raycast triggered -> Turns Bottom Left
- Right Raycast triggered and Botton Raycast triggered -> Turns Top Left
- Top Raycast triggered and Left Raycast triggered and Right Raycast triggered -> Turns Down
- Botton Raycast triggered and Left Raycast triggered and Right Raycast triggered -> Turns Up
This system now would do a decent job of actually moving around and avoiding obstacles. It worked nicely when there are not a massive amouth of obstacles. If my artefact was not aimed to navidate through an abundance of obstacles this would have done the job. To continue improving this system, the next issue needed to be solved. What happens when all of the raycasts are triggered. Currently the enemy moved forward and phased trough the obstacles. This dead end scenario can simply be solved by stopping the enemy midair and turning it around to go back out from where it came from.
- Botton Raycast triggered and Left Raycast triggered and Right Raycast triggered and Top Raycast triggered -> Turns Around
If the enemy was trying to navigate inside of very tight areas, similar to a tunnel of obstacles, There needed to be a way of checking if the enemy can fit inside of the tunnel. This is where I introduced three Sphere Casts.
- The first sphere cast, that is constactly checking directly infront, is used to check if there was the required amount of space needed to proceed inside of the tunnel.
- The second sphere cast, that point in the direct direction from the enemy to the target, is used to check if there is anouther path that is straing forward to the target. This one is mainly there to check for a simple shorter path.
- The third shpere cast, ponts in the middle of the first and second sphere casts, is used to check if the enemy can turn and not collide with any obstacles on the way. This can be tought of as the check for the turning radius of the enemy.
When the avrage and directional sphere casts are not blocked the enemy will be able to turn and move towards the target in the shortest path trough the obstacles.
Possible improved
There is absolutelly a few things that can be added to improve this system to be even better than it is right now. One of these things is to check if there is an object in very close to the enemy. So close that the starting point of the raycasts is inside of the object. Because of how the raycasts work, if a raycast is shot from inside of a object, that object will not be detected by that raycast. A way that this can be fixed is to add a trigger collider or a fourth Sphere Cast that will cover the starting positions of the raycasts and act as an additional buffer to prevent the phasing trough obstacles when they happen to be too close to the enemy.
Details
- Created by: Georgi Aleksandrov
- Character Sculpt: Lenny Chilcott
- Related Game: Interspersed