Trial of Skeleton AI
Previously, Vlad talked about the shaders that give Trial of Sacrifice its distinct look, now I’ll be talking about the Skeleton AI-- My name is Teegan, and I had a lot of enjoyment programming the Skeleton AI that Viktor fights against in the dungeon.
The Skeleton AI primarily uses a finite state machine to achieve its goals. There are four main states, and 6 main transitions. The four main states are: Idle State, Move To Player State, Attack State, and Flee State. The 6 main transitions are: Spot Player, In Range of Player, Out Of Range of Player, Out of Stamina, Stamina Regained, and Lost Player.
Move To Player
At startup, all Skeletons get a reference to the player. This allows the ai to perform distance checks to the player’s location. This is especially helpful for transitioning out of the idle state, as when the player is close enough, it “spots” the player. It then transitions to the Move To Player state. The Move To Player State uses a NavMeshAgent component to figure out how to get to the player, moving around any obstacles in the way. Additionally, this state also uses the player’s location to keep facing the player, even when they move. When the Skeleton AI is in attacking range, it transitions to the Attack State. The Enemy AI cannot chase Viktor out of their starting rooms, fearing the Chernobog’s retribution if they dare to disobey his orders, so if Viktor is beyond a reachable distance, the Enemy AI will transition to the idle state, where it stands in place, hoping to spot Viktor once more.
Attack State
So, this is obviously the state where the Skeletons attack. However, there is more to this state than just attacking. Each time a Skeleton attacks, it suffers a short cool-down. During this cool-down, the Skeleton preserves its attacking distance to the player. Its attacking distance is dependent on its weapon. If the Skeleton fails to keep a reasonable distance because, say, the player has run too far from it too quickly, it will transition to the Move To Target state. However, in this discussion, lets presume that the Skeleton didn’t need to transition out. As soon as the cool-down is complete, the Skeleton tries to attack once more. When a skeleton attacks, it loses stamina, and tries to recover this loss during the cool-down. However, if the player takes advantage of their ability to kick the enemy, the enemy becomes stunned. This changes matters.
During this stun state, the skeleton doesn’t recover stamina, and in fact, it resets the wait time for it to recover said stamina. When the skeleton exits out of the stun state, it immediately tries to attack the player where it last saw them – when they were about to be stunned. So, by taking advantage of stunning the skeleton at the right times, and therefore refreshing their ability to recover, can lead to the Skeleton running out of stamina entirely. When the Skeleton runs out of stamina, it will begin to flee from the player.
Flee State
In the flee state, the Skeleton is trying to make distance between the player and itself so it can buy itself time to recover enough stamina to attack. The Skeleton recovers stamina at a faster rate in this state. When the Skeleton has regained its stamina, it transitions back to the Move To Player state, closing the distance once more.
That’s all for today! Thank you very much for reading! Next week, Josh is going to talk about the lighting in the dungeon that helps convey the vibes that Trial of Sacrifice is going for, so if that interests you, feel free to follow us on all our socials to ensure you find out when our upcoming blog posts drop.