Minigame Monday: Covert Behaviour

 Covert behaviour:

  • The concept:

I've been doing some studies into behaviour trees in Unity for one of my personal projects, so I figured I'd experiment with the framework I'd created (using this tutorial as a guide (I did notice that the GetData and ClearData functions seem to doing unneeded work - they call the parent function, which then calls its parent function and so on recursivly, however they also loop upwards individually in the original function, so it seems like the functions might be being called multiple times on nodes close to the root. I rewrote the functions for now, and I'll look into it more as I use data in the BT. (P.S sorry for the nested brackets, but I guess I am an programmer :P))).

I decided to make a stealth game, where enemy behaviour was controlled by a behaviour tree. Enemies would patrol, and charge at the player if the player crossed their line of sight. On top of that, I added a panic mechanic; if the player reached certain spots, all the enemies would panic, prioritising that for a length of time. A diagram of the tree is below:

Enemy AI behaviour tree

  • The challenge:

The first part of the AI I looked at was patrolling. This is pretty simple behaviour; move to a point, move to the next point and so on until you've gone to them all, then start again at the first. However, I wanted to make sure I could define the routes for the enemies in a modular way, to allow me to easily define different routes for each enemy, or to have multiple enemies on the same route. I therefore created a scriptable object, storing an array of patrol coordinates, as well as other data about the enemy's chase speed and patrol speed. This allowed me to select a patrol path object for each enemy to give it the behaviour I wanted, and the patrol node would be initialised with the object and extract the details of its path.

Next, I built the detection for the enemies to spot the player (I guess I ended up building the tree backwards in a way). To give a cool effect, I added a spot light to the enemy, so what the light illuminated would be what the enemy sees. This looked really cool, however the light was being emmitted in a cone, and Unity doesn't have a cone primative for me to use as a collider. I've used probuilder before, but I've had issues when using it for colliders only, so instead I booted up blender and made my first project - just a single cone. Importing this into Unity gave me a mesh I could use as the collider, once I'd scaled it to the light.

From this point on, it was pretty smooth sailing. I finished up the tree, added basic player controls, a few interactables, and handling for winning and losing. Actually designing the level took a little while, and having to put in each waypoint for patrol path was a bit laborious, but I soon had a playable (and quite difficult) game.

  • The result:


 From a programming perspective, I'm happy with how this turned out; my decision to use scriptable objects made adding new enemies simple, and the behaviour tree worked exactly as I wanted. From a gameplay perspective, the game is pretty difficult, and one thing I'd look into if I'd had more time on the game is making sure the spotlights always show on the ground, as (presumably due to a quirk of the lighting engine) sometimes the beams don't show. One thing for future games is I might look for assets online to use in the games, as I'm still using primatives and having full models would add a lot to the games, but I'm still happy with how this turned out. You can play the game at https://reddragonmakesgames.itch.io/covert-behaviour, and as normal the code and assets are on my github.

Comments

Popular posts from this blog

Armageddump (Boss Rush Jam 2023)

Imperfect Control: Gameplay

Hair of the Wolf (GameOff 2022)