Time to talk about AI is approaching, but before diving into such a deep topic, there is one thing we
must understand first in order to make things easier. And that is pathfinding.
I will only cover pathfinding using navigation meshes, and I will assume that you already know how to set those up.
As usual, here’s the documentation I’m assuming you’ve read:
You might be wondering how can we talk about pathfinding without talking about AI ? Well, the ability to
make path searches can be useful to the player too. In Unreal Tournament 3′s CTF games, a button
press shows you the path to the flag. In Dead Space or Fable 2 and 3, you can have the path to your
next objective. In Crazy Taxi, the arrow tells you when to turn according to the layout of the streets.
I’ll be using that last example as a case in point.
The labyrinth
We have the simple maze pictured below:
In this maze, we have to reach a goal (the green ball).
I want an arrow that tells me how to get to the goal.
The obivous solution
That’s fairly trivial. An actor is attached to the player and always turn to face the goal. I’m updating the arrow’s location in the controller’s PlayerMove, which is called every tick. Here’s the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
class
SandboxPlayerController
extends
UDKPlayerController;
var
SandboxNavigationActor NavArrow;
event
PostBeginPlay()
{
super
.PostBeginPlay();
NavArrow = Spawn(
class
'Sandbox.SandboxNavigationActor'
,
self
);
}
event
Possess(Pawn aPawn,
bool
bVehicleTransition)
{
//Here I'm attaching the arrow to the Pawn.
local
Vector arrow_loc;
super
.Possess(aPawn, bVehicleTransition);
arrow_loc.Z =
64
;
if
(NavArrow !=
none
)
{
NavArrow.SetBase(Pawn);
NavArrow.SetRelativeLocation(arrow_loc);
}
}
state
PlayerWalking
{
function
PlayerMove(
float
DeltaTime)
{
local
Vector arrow_loc, arrow_dir;
local
Rotator final_dir;
super
.PlayerMove(DeltaTime);
if
(NavArrow !=
none
)
{
if
(SandboxGame(WorldInfo.Game).CurrentObjective !=
none
)
{
//This custom GameInfo simply contains a reference to the actor that represents my goal.
arrow_dir = SandboxGame(WorldInfo.Game).CurrentObjective.Location - NavArrow.Location;
final_dir = Rotator(arrow_dir);
}
NavArrow.SetRotation(final_dir);
}
}
}
|
The less obvious solution
The above solution is nice and easy, but let’s do something more sophisticated. I want the arrow to tell me where to turn in the maze.
This can be done with pathfinding. Note that the following solution is not better than the first one, it’s just different. It all depends on what you want in your game.
As stated in the documentation, all pathfinding functionality is handled by a native class called NavigationHandle.
Note that the Controller class (which is parent of both AIController and PlayerController) has a variable that stores a reference to a NavigationHandle and is called… NavigationHandle.
So, in order to gain pathfinding ability, you just need to initialize that variable at some point (PostBeginPlay sounds like a nice place):
1
2
3
4
5
6
7
|
event
PostBeginPlay()
{
super
.PostBeginPlay();
NavArrow = Spawn(
class
'Sandbox.SandboxNavigationActor'
,
self
);
NavigationHandle = new(
self
)
class
'NavigationHandle'
;
}
|
Now the Controller is ready to do path searches. It’s a 4 step process:
- Decide where you want to go.
- Decide how you want to go there.
- Search for a path.
- Once the path is found, do something with it!
You choose where you want to go by adding a Goal Evaluator to the Navigation Handle. During the path search,
the goal evaluator will be asked if the currently processed navmesh polygon fits the goal conditions.
The documentation lists the different goal evaluators. Most of the time, the NavMeshGoal_At should be just what you need.
All goal evaluator classes have a wrapper static function that you use to add the evaluator to the handle, for instance:
1
|
class 'NavMeshGoal_At' . static .AtActor(NavigationHandle,SandboxGame(WorldInfo.Game).CurrentObjective);
|
Meaning that you can’t make your own goal evaluators. Same goes for the path constraints I’ll be talking about a bit later.
There is a special kind of goal evaluator the documentation doesn’t talk about: The goal filters.
They are evaluators you add on top of you main goal evaluator. Their role is to filter out possible goals that don’t match specific criteria.
Below is a sample list of the available filters:
- NavMeshGoalFilter_MinPathDistance
- NavMeshGoalFilter_NotNearOtherAI
- NavMeshGoalFilter_OutOfViewFrom
All these classes can be found in the Engine package.
Now that we know where we want to go, we need to choose how to get there.
This is done by the Path constraints, which work the same way as goal evaluators.
I remind you that you can’t create your own path constraints, so you’ll need to be imaginative
with the paramaters of the one you’ve got at your disposal. In most simple cases, the NavMeshPathToward will be your best bet:
1
|
class
'NavmeshPath_Toward'
.
static
.TowardGoal(NavigationHandle,SandboxGame(WorldInfo.Game).CurrentObjective);
|
Now you’ve got your path, you need to follow it. In our case, we will just orientate our arrow to follow the path.
This is done by NavigationHandle.GetNextMoveLocation(). It tells you where to go according to your current position.
However, if you wander off the path, it tends to get lost. To counter that, I request a new path each tick (certainly very inefficient,
it would be best to make a new request only if we are lost (when GetNextMoveLocation() returns a null vector)):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
//We're still in the PlayerController
state
PlayerWalking
{
function
PlayerMove(
float
DeltaTime)
{
local
Vector arrow_loc, arrow_dir;
local
Rotator final_dir;
super
.PlayerMove(DeltaTime);
final_dir = NavArrow.Rotation;
//keeping the previous rotation in case the path fails.
if
(NavArrow !=
none
)
{
if
(SandboxGame(WorldInfo.Game).CurrentObjective !=
none
)
{
class
'NavmeshPath_Toward'
.
static
.TowardGoal(NavigationHandle,SandboxGame(WorldInfo.Game).CurrentObjective);
class
'NavMeshGoal_At'
.
static
.AtLocation(NavigationHandle,SandboxGame(WorldInfo.Game).CurrentObjective.Location);
if
(!NavigationHandle.FindPath())
{
`log(
"COULD NOT FIND PATH"
);
}
if
(NavigationHandle.GetNextMoveLocation(arrow_dir,
64.0
))
{
DrawDebugLine(Pawn.Location,arrow_dir,
255
,
0
,
0
,
false
);
arrow_dir -= NavArrow.Location;
final_dir = Rotator(arrow_dir);
final_dir.Pitch =
0
;
// GetNextMoveLocation returns a position near the ground, so I zero the pitch so the arrow doesn't point to the groung.
}
}
NavArrow.SetRotation(final_dir);
}
}
}
|
And we get the following:
The arrow is a bit jumpy, but that’s because I’m enforcing the rotation and the Navigation Mesh’s resolution
is probably too low in regard of the level geometry. Making a smooth rotation would help making it look nicer but hey, it works!
The point here is to make sure we can get pathfinding to work. This will be one less thing to worry about when learning about AI.