Saturday 4 February 2017

AI Teams, Unit Health and Basic Combat

AI teams








The teams are represented by a public integer so the information can be accessed by other AI units. At the moment there are only two teams, 0 and 1. When the program starts, I check whether the unit is in team 1 or 0; if it's on team 1 then I colour the mesh red.



Health

The health is represented by the blue numbers. In order to display the health on the screen, I used Unity's OnGUI function. The function loops through each AI unit, gets the position relative to the screen, and renders the text on the screen in the appropriate position each frame.

Combat

On each frame, each unit checks a sphere with a radius of 2 to check if there is any AI nearby; if the AI is on the enemy team, is not the current target and the current unit is not attacking anything else, then the "attack" co-routine is run. Whilst the target is still alive and within the radius, then the current unit attacks and takes 1 health from the enemy AI per 0.5s.

Finished Result:






Statistics After Binary Heap Optimisation

Moving - With Binary Heap Optimisation

The binary heap is only used whilst the algorithm is running, so we'll only be taking the movement statistics.

Frame time whilst moving (20x20 grid):






1/0.0013 = 769 fps
Frame time whilst moving (50x50 grid):





1/0.0025 = 400 fps
Frame time whilst moving (100x100 grid):





1/0.015 = 66 fps

As you can see, binary heap optimisation has roughly doubled performance, and has also made frame drops less noticeable at 50x50. The algorithm could be optimised further, but a more significant way to lower frame time is by changing the map representation.