I have had some spare time to spend on getting the AI working in my game for enemies. Moving on from simple Chickens, which don't do anything much except walk around waiting to be killed, I have now moved on to getting smarter and meaner creatures to stalk you through the map and make the player's life more akward.
I started out by obtaining two nice models to use as my first two beasties (click the pictures below to enlarge):
These two beasties all inherit from the same 'creature' framework I have created known as "GenericAggressive". The GenericAggressive type is built of two parts, a blueprint AI class, and a blueprint controller class. The controller class is responsible for selecting the correct visual and audio behaviours for the creature, for example the sounds it makes when it sees the player, the animations it uses for spotting, attacking, and chasing the player, and the sounds it makes when it is hurt. These are exposed via parameters to the instance of the object:
The AI class controls the movement behaviour of the creature, e.g. when it is patrolling, when it is chasing the player, when it gives up and how aggressive it is (click the image below to enlarge):
The AI has a few simple "dumb" traits which combine together to give the impression of a much smarter whole:
- All GenericAggressive creatures have a default home location, which is set in the constructor of the blueprint to whever it is placed initially in the map.
- The creature will patrol at random around its home location, picking random navigable points within that radius to walk to, with a random break of one to three seconds before it picks another route.
- Each type of creature has a sight cone, provided by the "AISensing" object, and an auditory sense. Detection of the player by them passing into the sight cone causes the monster to lock onto the player's coordinates and start moving quickly to that location. Simply hearing the player causes the creature to navigate to where it heard the sound, in the hope that during this route it will see the player and be able to chase them proper.
- A creature which is chasing the player is then 'locked on' to the player, and will pursue them to within 150% of its patrol radius before giving up, or until the player or the creature are dead.
- When a creature locks on to the player, a 'sensed player' animation and sound are played. For example for the cave troll, it roars and raises its weapons in the air ready to charge. These sense animations and sounds serve as warnings to the player of imminent attack.
- Any sounds generated by the creature can only be heard within 1000 units of the creature, with the sound volume dropping off linearly along that range.
- A creature which is locked onto the player will use the navmesh to attempt to get into range, repeatedly re-calculating its route until it is considered to be within weapons range (at present all creatures have non-projectile weapons such as swords or stingers). Once in weapons range, the creature will attempt to strike if it is within a 90 degree arc facing the player, dealing damage if the player is within the collision capsule of the creature.
- Any creature which is close enough to wound the player is also close enough to be wounded by the player and the victor is simply the one who lands the most hits whilst dodging the hits of the other. Each creature can have any number of attack animations, each of which deal different damage amounts and can have any number of attack sounds.
- If the player kills the creature, its AI controller is detatched to stop it functioning, and a 'death' animation and sound are played (creature-specific). For example, the scorpion curls into a ball and the cave troll falls backwards and drops its swords.
- If the creature kills the player, the player controller is detatched from the player to prevent input to the player character having any effect, the death animation and sound for the player character are played and the level blueprint runs a matinee sequence to fade the screen to black, causing the game to end. Any creatures close by to the player when the player dies will switch to a celebration animation, for example the cave troll waves its swords in the air and cheers. I am hoping to find some creatures that have feeding animations, as this would be a fitting end for any adventure unfortunate enough to die at the claws of those creatures...
- If a player manages to get more than 150% of the creatures patrol range away from the creature (calculated as vector length) then the creature simply gives up, and walks back to its spawning point, starting its patrol sequence again. At any point if it sees the player again, it re-initialises the attack sequence.
- If there are creatures alive within 1000 units of the player character, the player character switches to his 'nervous' idle animation, which causes him to look around at random and keep his sword close to his chest. At all other times the normal 'idle' animation is used.
Combined together, these rules make for some pretty interesting gameplay, as shown in the animated gif below (which shows me getting my ass handed to me on a plate by my own game... how embarrasing!):
[size=2]A high resolution YouTube version of this, with sound, can be found here.
Ok, i admit it... I purposefully died to demonstrate the health bar and fade to black sequences. (honest!)...
On a related note, now that the screenshots and videos i am putting out there are showing some real action, and not just random wandering across an empty map, i find myself spending more and more time each day on social media drumming up enthusiasm for the game. This is easier than i anticipated as everyone loves a good gif of a fight, so simply recording videos of combat, converting them to gif and putting them on twitter seems to be going a long way in spreading my name around right now. Time will tell!
As always, comments, suggestions, questions are all welcome below!