Giving the enemy sight with Pawn Sensing

To grant the enemy the ability to detect the player, we need to add a PawnSensing Component to EnemyController. To do this, open the EnemyController Blueprint and click on the Add Component button in the Components panel. Search for and add the PawnSensing Component. This Component gives us the ability to add a few additional event triggers to the EnemyController Event Graph. The one we are interested in now is called On See Pawn (PawnSensing), as shown here:

There is a second system that can be used for AI behavior development and environment sensing, called the Environment Query System (EQS). As of Unreal Engine version 4.20.3, this feature is still in the experimental development phase. However, the EQS features are expected to take over as the primary system for developing AI that can sense the environment around them. The PawnSensing behavior we cover in this book will remain usable alongside the EQS system, and many of these concepts will transfer over. 

Ensure you have the new PawnSensing Component selected in the Components panel, and then, with Context Sensitive searching checked, search for and add the On See Pawn (PawnSensing) node to the Event Graph. This Event fires when the enemy is able to see the player along a line of sight. To transmit this information to our Behavior Tree, we will first need to create a new Blackboard Key store and pass this information to it. You can see how this is accomplished in the following screenshot:

Drag a wire from the Pawn output pin of the On See Pawn (PawnSensing) node we just created and attach it to a Cast To FirstPersonCharacter node. This will ensure that the enemy only reacts to the player being seen, as we do not want to trigger a chase behavior when it sees other enemies.

Next, we need to get a reference to the Blackboard. Add a GET Blackboard node to Event Graph. Drag a wire from the Target input pin of this node and attach a Self node to it. Now, drag a wire from the Return Value output pin and attach it to a Set Value as Object node. Then, connect the As First Person Character output pin of the casting node to the Object Value input pin of the Set Value as Object node. Also, connect the execution pins of the Cast To FirstPersonCharacter and Set Value as Object nodes.

The final step to pass the information about the player being spotted to the Blackboard is to establish the key that will store the data. Drag a wire from the Key Name input pin of Set Value as Object and attach it to a Make Literal Name node. We don't yet have a key created on the Blackboard for this purpose, but that will be our next step. For now, Enter PlayerActor into the Value field of this node. As always, create a comment around the block of nodes to remind yourself of its functionality, then compile and save your work.

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

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