Interpreting and storing the noise Event data

The PawnSensing Component we added to EnemyController gives us the foundation to build both visual and auditory sensing in our enemy AI. Thus, we want to return to EnemyController and add some Blueprints that will instruct our AI how to react to the sounds in the world around them. Go to Content Browser, open the Enemy folder, and then open EnemyController.

In the Components panel, click on the PawnSensing object, and then use either the search Function or the Events section of the Details panel to add an On Hear Noise (PawnSensing) node. This node will activate any time the PawnSensing Component attached to EnemyController detects a special kind of sound broadcast by a pawn noise emitter. We will have to set up the Blueprint so that the enemies only detect noises that are made a short distance away, otherwise, it would feel unfair for the player to shoot their gun from the opposite corner of the map and let every enemy instantly know their location.

Attach a Branch node to the On Hear Noise (PawnSensing) node. Before continuing with the nodes that will store the data about the noise Event, we will first check whether the noise has occurred close enough to the enemy to trigger our investigate Action. To evaluate this, we have to compare the location of the noise Event detected and the location of the enemy. We will accomplish this by setting up a vector comparison, as shown in the following screenshot:

To figure out the location of the enemy doing the listening, create a Get Controlled Pawn node. Then, drag a wire out from the Return Value output pin and attach it to a Get Nav Agent Location node. These two nodes will output the location of the pawn object that is controlled by the AI controller we are currently editing.

We want to subtract the vector location of the enemy from the vector location of the noise to get our distance, so let's drag a wire from the Location output pin of On Hear Noise (PawnSensing), and then attach it to a Vector - Vector node. Next, connect the bottom input pin of the Vector - Vector node to the Return Value output pin of Get Nav Agent Location. Drag a wire from the output pin of the Vector - Vector node, and then attach it to a Vector Length node. This will translate the vector length to a float number.

We can now evaluate whether the float number calculated is less than the threshold distance we want to define for the enemy's hearing range. Drag a wire from Return Value of the Vector Length node, and then attach it to a Float < Float node. Remember that, as with any arbitrary value that defines a property, you can create a variable here to replace the number field and make it easier to adjust your threshold at a later time. To do so, create a new variable of the float type and give it a default value that matches the number you want. I called this variable HearingDistance and gave it a value of 1600, which worked well for the layout of my Level. You may need to adjust this value to be appropriate for your map and intended gameplay. Attach your variable to the bottom input pin of the Float < Float node, or just type in the value.

To complete the condition, attach the output pin of Float < Float to the Condition input pin of the Branch node. This completes the steps we need in order to ensure that the sound being heard is within the range to act upon. Now, we need to store the data about that sound in our Blackboard so that the Behavior Tree can access it. The Blueprint nodes needed to accomplish this can be seen in the following screenshot:

Start by dragging a wire from the True output execution pin of the Branch node, and then attach it to a Set Value as Vector node. You must uncheck Context Sensitive in the menu in order to see the Set Value as Vector node. We need to fill in the three inputs for this node to store data about our sound location. First, drag a wire from the Target input pin, and then attach it to a Get Blackboard node. Then, drag a wire from the Target input of Get Blackboard and attach it to a Self node. Next, drag a wire from the Key Name input of Set Value as Vector and attach it to a Make Literal Name node. Type LocationOfSound in the Value input as we want to store the vector location in the corresponding key in the Blackboard. Finally, drag a wire all the way from the Location output pin of the On Hear Noise (Pawn Sensing) Event node to the Vector Value input pin of the Set Value as Vector node.

The last thing we need to do is store the information that a sound has been heard in the Blackboard. Drag the output execution pin of Set Value as Vector out and attach it to a Set Value as Bool node. Drag a second wire from the Return Value output pin of the Get Blackboard node you used earlier to the Target input pin of Set Value as Bool. Now, drag a wire from the Key Name input pin out and attach it to a new Make Literal Name node. Inside this Value input, type HasHeardSound. Finally, ensure that the Bool Value checkbox input of Set Value as Bool is checked in order to designate that a sound has been heard. Wrap this entire series of nodes in a useful comment container, 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