First Iteration

With the first iteration of the AI I wanted to use behaviour trees, this would see each AI having different nodes such as patrol or attack. One of the problems that I faced early on was that I was not using a navmesh when all of my experience with unity AI has been in 3D using a navmesh.

The first step was to create the nodes that could be used in the BTs, this required a base class called ‘Node’ that the rest of the nodes such as a selectors, sequences, inverters and action nodes such as a PatrolNode would inherit from.

How this version of the behaviour tree works is as follows:

https://youtu.be/WQXlG11vZbE

Creating attacks

When creating attacks for the enemies two knew nodes were created, ‘ShortRangeAttackNode’ and ‘LongRangeAttackNode’, if the player is within a certain radius of the enemy then it will do the respective attack. In the video below when is says ‘Attacking Playing’ in the console this is a short range attack being triggered.

https://youtu.be/7dHQjwP5Kk4

Looking at the player

When we wanted to get the right sprite for where the player was looking we just used the direction the gun was facing and adjusted accordingly, however, this could not be done with the tree so another method was used. Based on the players position relative to the tree, the tree will look accordingly. For example if the players x position and the players y position is less than the trees x and y position then the player is to the SW of the player and the tree will choose the sprite facing the front and flip the sprite on the x axis. Below is a diagram of how this works along with some pseudo code of the process.

P = player
E = Enemy

if(P.x < E.X && P.y < E.y)
		player in SW direction
if(P.x > E.X && P.y < E.y)
		player in SE direction
if(P.x > E.X && P.y > E.y)
		player in NE direction
if(P.x < E.X && P.y > E.y)
		player in NW direction

Diagram.png

https://youtu.be/gM-J96wbeEU

Second iteration

One of the problems with the AI was it walking into walls and not being able to follow the player, this is when I discovered a package that would allow the navmesh to work on 2D, using this would mean the the AI would walk around walls and try to follow the player.

Another reason for the change to the AI was the BTs felt overcomplicated and it was becoming harder to get the attacks and chase to work with one another because they were using coroutines. This made me convert each of the AI into a single script file as it was easier.