Name: Date: Period: Artificial Intelligence and Non-Player ......Artificial Intelligence and Non-Player Characters: Additional Tutorials DIRECTIONS: Use the tutorials below to further
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
18. Click the decorator. Make sure the query, key and node name match the following image:
19. Make another decorator for the “Chase Player” node. Match the following images:
20. AI Perception: AI attached to an NPC that allows the NPC to react to its surroundings through
seeing or hearing actions within a level. An example of this is having an NPC react to the player character when it sees it by running toward the player character.
Open the AI Controller Blueprint. Add “AIPerception” and select it.
21. In the ‘AI Perception” section of the Details panel, click “+” to add an “AI sight config” sense. This will tell the NPC to do something (chase the player) when it sees the player character.
22. Click the empty triangle to extend it. Extend “sense” and “detection by Affiliation” to see additional setting options. Check all boxes under “Detection by Affiliation” for now. Compile.
23. NPCs can behave in many ways. To ensure that your NPC does what you want it to, you need to create a stimulus source. You can add this in the third person character Blueprint. Note the breadcrumb trail below to open the character Blueprint.
24. Add an “AIPerceptionStimuliSource.” Click it to access its Details panel. Update the “AI Perception” section to match the third image below. **Note the available sense options. Later, you could program a security guard NPC to respond to a player’s sound by adding “AISense_Hearing.”
25. Open your AI Controller Blueprint. Click “AI Perception.” Right-click in the event graph to access the node below.
26. Replicate the Blueprint below. If you have difficulty searching for nodes by right-clicking, try searching for them after dragging from a connecting node.
27. You need to connect the key that you made earlier to the “set value as bool” node. This requires you to search for your bool key name from your Blackboard. It’s probably “CanSeePlayer.” To find it, drag from “Key name” and type “make literal name.” Type your key name for the value. Type it exactly as it is shown in your Blackboard.
28. Compile and save. If you play your level, the NPC should now go to the player’s location when it sees the player. It’s not quite “chasing” at this point, so we need to add some more things.
29. Open the Behavior Tree. Select the “can’t see player” decorator. Notice the “Flow Controls” section in the Details panel. Assign “both” for “observer aborts” to ensure that your Behavior Tree functions in the correct order.
30. Create a new task by selecting “BTTask_BlueprintBase.” Change the name to something like “chase player.”
31. Remember, in order for your Behavior Tree to execute its actions, its Blueprint needs “event receive” and “finish execute” nodes. You’ll also need to tell the NPC where to go. Replicate the Blueprint below:
32. You need to reference a key from the Blackboard. Create a new vector variable and assign it to the “Blackboard Key Selector.” Don’t forget to make it “instance editable.” Otherwise, you won’t be able to access it in the Behavior Tree.
33. Compile. The error is to remind you that there is more to do! Make sure your Blueprint matches the image. You’ll need to select “get vector” when you drag the vector into the event graph.
34. Compile, save and go back to the Behavior Tree. The issue with the NPC was that it would “move to” the player’s location instead of chasing it. Delete the “move to” node from the “chase player” sequence.
35. Add your “chase player” node. Make sure the “TargetLocation” vector is assigned.
36. Play your level, and notice how choppy the NPC looks when it chases the player. This is because the NPC is constantly changing its location to match the player’s location. A Behavior Tree “cooldown” decorator can help with this. Replicate the image below.
37. Notice the “cool down time” in the Details panel. This represents how often the “chase player” sequence restarts. Five seconds is too long to have the NPC chase the player in a smooth fashion. Decrease the “cool down time” and figure out an amount that works for you by playing your level after making changes.
38. The NPC should now be chasing the player more smoothly; however, it is probably still “snapping” whenever it turns. This is not as natural-looking as can be. To fix this, you’ll need to change the rotation within the NPC character’s Blueprint.
39. Notice what the NPC (self) “pawn” details show, specifically what box is checked.
40. Since the NPC is not controlled by the player, you’ll want to uncheck that box.
41. Under “components,” select “CharacterMovement (Inherited).” Scroll down to “character movement (rotation settings)” in the Details panel. Check one of the boxes and play the level to see the difference it makes. You may also want to change the “z rotation rate” to something smaller, such as 250. Hopefully, you understand why these changes improve the movement of your NPC.