Advanced AI Behavior

The majority of this article is obsolete in CRYENGINE 3.5 as many steps listed are either no longer necessary or no longer applicable in later versions. Please see the main AI documentation area for up-to-date documentation.

Overview

After completing this tutorial, you will be able to place enemies in a level and define where they can walk and hide. By default, they will shoot at the player on sight.

Initial Setup

Placing an AI Character in the Level

Drag the AI Grunt entity from the Objects section of the RollupBar to the Perspective viewport.

Giving the AI a Weapon

Clicking on the "..." button next to the EquipmentPack text field will open the EquipmentPacks dialog box. Selecting and clicking OK will give the character the weapons contained within.

For more information on how to set up the EquipmentPacks, refer to the EquipmentPack tutorial.

You need to click Reload Script (located below the entity properties) in order to have the grunt start using the selected equipment pack.

Enemy Archetypes

AI characters are typically placed as Archetype objects, meaning that the parameters need to be set up only once and then can be used across levels. To see an Archetype object's parameters, press the Entity Archetype box (located below the entity properties) to load an Entity Archetype.

Generating AI Navigation

Even on a flat terrain, be sure to generate new AI navigation at least once after creating the map, and also after any modification to the terrain, or after adding vegetation and other objects.

AI navigation can be generated by clicking Generate all navigation on the AI menu.

Testing the Results

Go into the game mode (CTRL+G) and approach the AI; if configured correctly, the AI will shoot the player when it sees or hears them.

Setting up Navigation

Introduction

To make your AI function correctly in your level, you need to set up various types of navigation.

Typing ai_DebugDraw=79 in the console will show you if a triangulation is already setup, this means the AI will be able to find its way from one point to another without bumping into obstacles.

Vegetation

AI will automatically walk around Vegetation objects if set up correctly.

If you want them to hide behind a tree, you just need to set the Hideable flag in the Vegetation properties.

The AI Radius Parameter should be used with care. It can be seen as a time saving feature, but used inappropriately it can provide undesired results. Most vegetation objects do not need this parameter set as the triangulation system can handle simple cylindrical shapes, such as bushes, trees etc. quite effectively. But if you are using more irregular vegetation shapes then you may consider using this parameter. Setting the AI Radius will automatically generate a forbidden area around the vegetation, based on the physics proxy of the model. Hence you should ensure that the physics proxy of the model is a simple as possible. Otherwise this will lead to a overly complex triangulation of the navigation mesh and thus slow/poor AI navigation. Alternatively and often more effective is just to create your own forbidden area around these pieces of irregular vegetation.

Cliffs and Big Solid Objects

Typing ai_debugdraw 85 in the console will display all areas that are too steep to climb (red) and areas that may cause sliding when you try to walk on them (blue).

Now, surround all such areas carefully with ForbiddenAreas (see section below) or a ForbiddenBoundary.

Forbidden Areas

You need to surround each internal waypoint region with a ForbiddenArea, this way the AI will not try to walk through walls and other objects.

Click on AI/ForbiddenArea and create a shape around the area you want to make impassable (for example the external walls of a building). Select ForbiddenArea from the menu as shown below.

Once your ForbiddenArea is created and your level triangulated, it will not be possible for the AI to walk through the walls.

Setting up Waypoint Regions in Internal Areas

To make the AI go through doors and navigate inside the building you need to add a Waypoint Region. You need to extend one section of this Waypoint Region outside of the forbidden area, as an entryway for the AI, as below.

The 3 things you need to setup a Waypoint Region are:

  • AI Navigation Modifier Area.
  • AIWaypoints (of various types).
  • AIWaypoint Entry/Exit points.

You need to surround each waypoint region with an AI Navigation Modifier "Human Waypoint" (the blue shape).

Click on AI/AINavigationModifier and create an area shape of that type.

By default the NavType is set to "Human Waypoint" so no need to modify anything.

Place AI/AIpoints inside your area. You will see them turn blue once they are in a valid area.

At the place where AI should be able to enter and exit the area change the type of the AIPoint to Entry/Exit (a door symbol will be displayed instead of the blue ball.

One part of the AINavigation modifier area should be outside the forbidden area shape (red line) so the AI can reach the point.

To generate the connection between all points select the AINavigationModifier shape you placed and set Auto-Dynamic in the Waypoint Connection Parameters.

And finally click on one of the points and press the Regen Links Button.

All Points should now be connected like this and the AI will be able to navigate inside the area and enter/exit it at the points where you set the specific entry and exit points.

See the section below on how ForbiddenAreas and Waypoints should be setup in terms of placement.

Flight Navigation

You need to surround every area where you want something to fly with an AI Navigation Modifier shape called Flight. You also need to give this area a height.

Water Navigation

You need to surround every area where you want something to navigate on water (i.e. boats) with an AI Navigation Modifier shape called Water.

Paths and Roads

Create an AIPath with the Road box ticked, and vehicles will automatically have a strong preference to use the road/path. AI can also be made to follow the path using the AIFollowPath flowgraph node.


Please note that in the default SDK the path look ahead distance is set quite high. What does this mean exactly? It means that in combination with path beautification and smart path following features the AI sometimes can seem to not properly stick to its path but cut corners a bit drastically.

If you want to change this behavior you can set ai_UseSmartPathFollower_LookAheadDistance to a lower number and have them stick to your desired path more thoroughly.

External Hide Points

To make AI hide at specific spots you have to place AIAnchor (outside of waypoint areas).

Place an AI/AIAnchor next to any solid object. Now, notice the two red bars in front of the hide point, which indicates if the AI can use a crouch/hide animation, or a left/right strafe animation.

The red line on the front of a hide point shows that there is left an right enough space the strafe left and right. Make sure the Green Area is not blocked by anything, so that the AI can actually use it.

AIPoint Type Hide Points in Nav Areas

The same concept for AIAnchors applies to the AIPoint Hide Points. So, change one waypoint within a nav area to a Hide Point, by changing the Type in the AIPoint Properties.

Setting up AI Correctly – Advanced Tips

Forbidden Areas

Here the forbidden area is placed too far away from the actual object:

Instead place them closer the the edges of the objects. This avoids the problem of AI slipping through the gaps. In the following example, the AI can navigate much better around the objects:

Do not place two forbidden areas close to each other. Even if the AI doesn't fit through, the navigation will still trace a path between the two areas:

Instead, delete one forbidden area and make one big forbidden area containing both objects:

Do not place objects close to the edges of a forbidden area. Otherwise, the AI will circle around the hide objects and slip inside of the forbidden area:

Place the hide objects either inside of the forbidden area or outside of the forbidden area. Make sure that if the AI circles around the hidden object it doesn't slip into the forbidden area:

Large, open sided structures should not have one big forbidden area around them. Rather, place smaller forbidden areas around them:

Hide Anchors

The AI hides at the hide anchor if an enemy is the cone. The more AI guys you have the more hide anchors you have to place. Do not place hide anchors for just one direction. Place them for all possible directions.

Place hide anchors at the sides of an object and not only at the edges. Sides provide better cover than edges.

For thin objects place hide anchors close to the edges so that the AI can leave their positions quickly. Do not forget to place them on both sides pointing at each other

Place hide anchors only at positions where the AI has real cover. For example, the front and back of a car provides less cover than its middle.

Edges of a house typically provide less cover. First place hide anchors close to the edge pointing directly in the direction. Then place hide anchors with 45° rotation slightly away from the edge.

If you do not have enough hide anchors for many AI guys you can increase the hide anchors. The basic hide anchors are most important. Then you can increase the number hide anchors with the 45° ones.

If you have long objects, do not place hide anchors in the middle or else the AI has to move too far to change position. You should only place them in the middle if there is other cover in the area.

Waypoints

For two rooms it is enough to place these two waypoints. From each corner of the rooms the AI can reach the closest waypoint. The link gives the AI the possibility of switching rooms.

Place waypoints at the edges of objects. The AI will have no problem going around and will not get stuck.

The AI in this example is at the wall and is looking for the closest waypoint. Accidentally the closest waypoint is behind an obstacle. In the new engine the AI would still find the correct one but it costs more performance. Instead place the waypoints with the same distance next to thin objects.

Take care when you link waypoints. The links should never go through or go very close to physical objects.

Avoid waypoints that are too close to links. If two AI guys start moving they might collide because the links run too close to each other.

Avoid crossed links at object corners. The AI might run into each other at the intersections.

You can easily solve this by adding a new waypoint at the edge (You should have a waypoint at the edge anyway).

If you have alternative routes think how it would look like if you would walk exactly along the lines. Sometimes it is better to use more waypoints to simulate a more natural movement like the example on the right.

If you have a room first place all waypoints at all edges of objects. Only do this with objects you can't walk over.

Then you look for waypoints which are too close to each other. If they are close together use one waypoint instead of two for cleaner navigation and less chances for the AI to get stuck.

After the waypoints, place the hide points, following the general rules from the hide anchors.

Hidepoints are also part of the navigation, merge hidepoints and waypoints if they are too close to each other.

If all navigation points are placed, start linking them.

This waypoint in the middle may not provide perfect movement but is better then the AI getting stuck.

The red lines show bad links: either the links go through/close to geometry, or they are too close to other links. It is okay if the AI first has to take 1-2m step(s) before walking in the right direction. That is better than the AI getting stuck.

Result

Once all of this is set up properly, the AI will be able to walk to any point and fight against the player. The better you place the waypoints and hide points, the more realistic and challenging it will be for the player to fight against the AI.