Enemy Swimming AI - Devlog 7 (16/11/2022)


Welcome back to the continuing devlog of Depths of Dread.
Jack here, lead programmer, and for this devlog, we will be taking a look at the
swimming enemy AI that we will be using for our game.
 


Swimming AI 

The swimming AI allows the enemy AI to move freely within the game in any direction, in a manner that resembles real-world aquatic movement. To get this freedom of movement, the swimming AI is based on Boids, rather than the navmesh-based AI that we will be using for our grounded enemies.

Enemy Controller 

The enemy controller controls the AI movement. It calculates its own position, its forward direction (so that the AI is always facing in the direction it is moving), its steering force (which is the difference between the target location and its own location multiplied by its predetermined speed) and its velocity (which uses the steering force multiple by Time.deltaTime to move the enemy towards its target).

Field Of Vision 

The FOV script allows the AI to locate the player's position, which is then used by the behaviours to move the AI towards the player. The FOV scripts check whether the player is in the enemy's sight or attack range. The sight and attack ranges are created with an overlap sphere that checks all the colliders within its area and then uses a raycast to find the player's position. Both the sight and attack range will be using angles to determine how much of each overlap sphere will be raycasting to find the player. This allows for our AI to have blind spots in its line of sight, so when the AI is facing away from the player, it won't be able to detect the player. Two-layer masks are used within each overlap sphere, the target layer mask and the obstacle layer mask. The player is assigned the target layer mask and if they enter either the sight or attack range, they will be detected by the overlap spheres. To prevent the enemy AI from locating the player from behind objects, all of the level's environments within the game world will be assigned the obstacle layer mask. If the player is behind an object, then the AI will not be able to detect the player even if they are within sight or attack overlap spheres, allowing the player the ability to hide behind cover.

Behaviours 

For the swimming AI, several behaviours will be used by the enemy controller script to change how the AI acts for each situation. 

Arrive 

The arrive behaviour is used to allow the AI to move towards a game object and stop within a predetermined distance of that object. When the enemy reaches within a certain distance of the game object, the AI  will begin to slow down as it approaches its target and comes to a stop when it reaches its predetermined distance from the target.

Patrol 

The patrol behaviour is used to allow an AI to patrol around an area of the game world. When a patrol path is assigned to the AI, it will begin its patrol. The AI will move to the position of each waypoint in the order assigned in the path script. The patrol script will change when the AI approaches each waypoint to allow the enemy to move to the next waypoint. While the AI is patrolling, the FOV script will be checking for the player's position.

Seek 

The seek behaviour allows an AI to chase after a game object. This behaviour finds the vector position of its target and, using its own vector position, will calculate the distance between them and begin to chase its target.

Obstacle Avoidance 

To prevent the enemy from ignoring the environment of the game world, obstacle avoidance is required. To achieve this, layer masks were used to prevent the AI from colliding with an object in its desired path to its target.  When the AI approaches an object that has its layer mask assigned as an obstacle (such as a wall, floor, boulder, etc), a raycast will be used to detect that object. The enemy controller will then recalculate the quickest route to its target position and move around that obstacle. 

State Machine 

To have the AI switch between each behaviour, I designed a basic finite state machine. This state machine uses the FOV script to decide which state is currently active. When the state machine switches into a new state, the correct behaviours of that state will be enabled and the previous state's behaviours will be disabled. This allows the AI to switch between its behaviours seamlessly.

Idle State 

This is the default state of the state machine. When the game begins, every AI state machine will be set to an idle state. In the idle state, the AI will begin searching for the player. If they can see the player, the state machine will move into the pursue state and begin to chase the player. If the player enters the AI's attack range, then from there the AI will move into the attack state. However, if the player is not in the AI's line of sight or attack range, then the state machine will switch to the patrol state instead.

Pursue State 

This state allows the AI to chase the player. Once the state is active, the seek behaviour is enabled. The AI will locate the player and begin pursuing them with the seek behaviour. Once the AI moves into the attack range, the state machine will switch to the attack state. If the player leaves the AI line of sight, the AI will switch to the patrol state.

Patrol State 

This state allows the AI to patrol around the path assigned to it. Once the state is active, patrol behaviour is enabled. The AI will then begin its patrol along its chosen path. If the player enters the AI's line of sight, then the AI will switch to the Pursue State.

Attack State 

This state allows the AI to attack the player and damage them. When the player enters the AI attack range, the state machine will switch to the attack state.  In this state, the AI movement will be disabled, and the AI will begin attacking the player with a cooldown between each attack. When the player leaves the attack range, the AI will check whether the player is within the sight range. If so, then they will switch to the Pursue state. Otherwise, they will return to the Patrol State.


And so, that wraps up our Swimming AI at present. While it's not finished and will need rigorous testing, I hope that this outline shows you clearly how our fish AI will act within our game. We are making great progress in each of our respective fields, Kayne's texturing and implementation of our player model have been amazing, and Conor has been working on tweaking our player movement and the game's cameras. He also has incorporated the underwater shaders, which have really brought life to our test scenes. Jason has been doing great work updating our GDD and coming up with some great ideas for our enemy designs. He and I will begin work on the level design for the Vertical slice, which will be featured in another upcoming devlog.
Thank you for continuing to check in on our devlogs and stay tuned for more info on Depths of Dread. 

-Jack

Get Depths of Dread- Final Release!

Leave a comment

Log in with itch.io to leave a comment.