Adding wandering to the Behavior Tree

Now that we have our custom task, we can make the task sequence that will cause the enemy to find a WanderPoint, move to it, and wait there for a brief period of time. These Actions are represented by the nodes of the screenshot: 

Start by dragging a wire out from the Wander sequence node, and then attach it to your new FindWanderPointTask task node. Click on the new node and change Wander Key, which is in the Details panel, to WanderPoint. Also, change Node Name to Get Next Wander Point in order to be more explicit about its purpose.

Drag another wire down from the Wander sequence node, and then attach it to a MoveTo task node. Click on this node and change Blackboard Key to WanderPoint. Move this node to the right of the Get Next Wander Point node. Drag the third wire down and attach it to a Wait task node, placing the node to the right of those other two task nodes. In the Details panel, change Wait Time to 3.0 and Random Deviation to 1.0 to give the wait time a bit of variance. Now, save the Behavior Tree.

A few final modifications should be made before we test our work. Return to the Level Editor by clicking on FirstPersonExampeMap. Any enemies you have manually placed in the world can now be removed, as we now have an enemy spawner to serve the Function of creating our enemies. Find the enemy Actors in World Outliner and delete them by right-clicking on the object, selecting Edit, and then selecting Delete. Now, find the FirstPersonCharacter object and select it. In the Details panel, scroll down until you see a list of the variables we attached to the character Blueprint. Unless you created a custom category for these variables, they will be listed under the Default category. From here, we can easily modify the game values that determine player behavior and our win conditions.

In this case, we want to modify the Target Goal value to be higher so that the game can continue for a longer period of time. I set this value to 20 so that the player must eliminate 20 enemies before winning the game:

Now, click on Play to run the game. You should see the spawned enemies choosing paths in random directions and moving toward them. When enemies reach their WanderPoint, they will pause there briefly before choosing another random point and walking there. You will also notice that the enemies choose their WanderPoint independently of one another. This is because each enemy has its own instance of the Behavior Tree. Thus, every Behavior Tree task, such as the one we just created to find a WanderPoint, is running independently for each enemy. You will also notice that because we chose to have higher-priority sequences abort lower-priority sequences, any time an enemy hears or sees the player, they will end their wander movement and begin approaching the player or the location of the sound.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset