top of page

Week 5 - Enemy behavioural states and Death animation

This week, I fixed the initial problem of enemy movement, where boids chose an unwalkable destination on the Navmesh. I additionally added two enemy behavioural states. Behavioural states comprise of a boid wander function and an enemy flee function. These states switch depending on the relative position between the player and the enemy. I additionally added an enemy death animation. This instantiates a particle blood splatter effect and places a transparent plane with a blood splatter texture instantiated at the position of enemy death.

The creation of enemy behavioural states

When working on the enemy movement last week, I came across a bug where the random destination position for boids was chosen at an unwalkable position on the map where a static object was placed. This caused enemy’s to be moved towards the invalid walkable position. Upon analysing the bug, I realised that a walkable Navmesh area was being generated within the defined unwalkable GameObject.

Defined unwalkable game object Navmesh still present within object

Resolution – place a plane and define it as an unwalkable area

In order to resolve this issue, I simply placed a plane under the gameobject and rebaked the navmesh. This ensured that the entire area instead of just the perimeter under the gameobject was unwalkable, thereby resolving last weeks boid movement issue.

To create the wander behavioural state, I placed cube gameobjects in the scene and added a ‘Waypoint’ tag to each. This was followed by attaching a ‘Connecter’ script to each waypoint in which a public list was used to define waypoints which were connected to the current waypoint. As the variable was public, I was able to add and remove the connecting waypoints easily within the inspector.

Script attached to each waypoint

In the boid base class I created an array of waypoints within the scene. I set the initial destination to the first waypoint. Using a for loop which looped through each waypoint in the waypoints array, I compared them to the current positions of boids within the scene. If a boid position was equal to the position of a waypoint, the attached connectors script of that particular waypoint was called and a new boid destination chosen based on the connected waypoints of that waypoint. If the waypoint had multiple connectors, a random connector attached to that waypoint was chosen and set as the new boid destination.

Wander function

In order to create the enemy flee state, I used Unity’s distance component to calculate the distance between the player and each boid. If the distance of a boid was below a certain threshold, a variable calculated the sum of the boid position and the player’s forward position. I added a number to this variable value which defined a point infront of the boid position. Using the navmesh sample position, a nearest walkable point to the created flee variable was chosen.

Flee function

Although boids now fled away from the players position, an out of bounds error occurs when boids reach the perimeter of the map. This is due to the boids not being able to find a new relative position once they are pushed to the perimeters of the map. I was unable to find a solution for this and will work on resolving this issue in the next week.

Out of bounds error

Finally I created an enemy death function. I created a blood splatter particle effect using Unity’s Particle System and added a blood splatter texture image to a transparent plane which I saved as a prefab. When the death function is called in the boid base class, the enemy boid is destoryed and the blood splatter particle effect and textured plane is instantiated at the point of enemy death.

Enemy death particle effect


Recent Posts
bottom of page