name: date: period: artificial intelligence and non-player ......artificial intelligence and...

13
All images are Screenshots of © Unreal® Engine 4 2019 by Author DISTRIBUTION A: Approved for public release; distribution unlimited. Approval given by 88 ABW/PA, 88ABW-2020-1247, 06 Apr 2020. Name: Date: Period: Artificial Intelligence and Non-Player Characters: Additional Tutorials DIRECTIONS: Use the tutorials below to further develop your Artificial Intelligence (AI) and Non-Player Characters (NPCs). Doing so will ensure that your NPCs behave in specific ways. Examples include having your NPC 1. walk to a target point and 2. chase the playable character. You could copy a previous character, but then you may need to delete components later. To gain experience, it is recommended that you repeat steps and make new characters. Green text directs you to items to specially note. COPYRIGHT NOTE: All images within this tutorial were taken as screenshots from Unreal® Engine 4 by the author. NPC: Walk to a Target Point 1. Create another NPC Character (AI and NPC Tutorial steps 9 – 15). 2. This character will walk to a specific location that you designate within a Navigation (Nav) mesh. You’ll want to distinguish this NPC from others, so make sure your name reflects what it will do. 3. Drag your NPC into your level. 4. There are many ways to perform similar tasks in Unreal. Let’s work within the Level Blueprint for this NPC. NOTE: Make sure that your new “npc_Target” actor is selected before opening the Level Blueprint.

Upload: others

Post on 05-Jun-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Name: Date: Period: Artificial Intelligence and Non-Player ......Artificial Intelligence and Non-Player Characters: Additional Tutorials DIRECTIONS: Use the tutorials below to further

All images are Screenshots of © Unreal® Engine 4 2019 by Author DISTRIBUTION A: Approved for public release; distribution unlimited. Approval given by 88 ABW/PA, 88ABW-2020-1247, 06 Apr 2020.

Name: Date: Period:

Artificial Intelligence and Non-Player Characters: Additional Tutorials

DIRECTIONS: Use the tutorials below to further develop your Artificial Intelligence (AI) and Non-Player

Characters (NPCs). Doing so will ensure that your NPCs behave in specific ways. Examples include having

your NPC 1. walk to a target point and 2. chase the playable character. You could copy a previous

character, but then you may need to delete components later. To gain experience, it is recommended

that you repeat steps and make new characters. Green text directs you to items to specially note.

COPYRIGHT NOTE: All images within this tutorial were taken as screenshots from Unreal® Engine 4 by

the author.

NPC: Walk to a Target Point

1. Create another NPC Character (AI and NPC Tutorial steps 9 – 15).

2. This character will walk to a specific location that you designate within a Navigation (Nav) mesh.

You’ll want to distinguish this NPC from others, so make sure your name reflects what it will do.

3. Drag your NPC into your level.

4. There are many ways to perform similar tasks in Unreal. Let’s work within the Level Blueprint

for this NPC.

NOTE: Make sure that your new “npc_Target” actor is selected before opening the Level

Blueprint.

Page 2: Name: Date: Period: Artificial Intelligence and Non-Player ......Artificial Intelligence and Non-Player Characters: Additional Tutorials DIRECTIONS: Use the tutorials below to further

All images are Screenshots of © Unreal® Engine 4 2019 by Author Virtual History Museum: Artificial Intelligence and Non-Player Characters Distribution A.

5. Remember, you can create new nodes by right-clicking within the Event Graph and then

searching for specific actions. Replicate the Level Blueprint script below. Start by adding a

Reference Point for the NPC. Searches to help you include “get controller” (dragged from

“npc_Target,” and “simple move to location.”

6. The GOAL for the above highlighted node is the target point location destination. This is where

you want the NPC to move.

7. Close the Level Blueprint (you shouldn’t need to compile it). Drag a Target Point into your level

from Modes → All Classes → Target Point. You can also search “target point” from the Modes

panel.

8. Your NPC can travel to multiple target points. To demonstrate this, drag a second Target Point to

a different location.

9. Select one of your Target Points. Go back to your Level Blueprint.

10. “Create a reference to TargetPoint.”

11. Drag out to “get actor location.”

12. Replicate the Blueprint below:

Page 3: Name: Date: Period: Artificial Intelligence and Non-Player ......Artificial Intelligence and Non-Player Characters: Additional Tutorials DIRECTIONS: Use the tutorials below to further

All images are Screenshots of © Unreal® Engine 4 2019 by Author Virtual History Museum: Artificial Intelligence and Non-Player Characters Distribution A.

13. “Event key (chosen letter to tell NPC to move to Target Point).” The example below uses the “r”

key to trigger the NPC's movement.

14. Select the other Target Point and go back to your Level Blueprint.

15. “Create a reference to TargetPoint” and copy the rest of the nodes. You’ll need to assign

another key. The example below uses the “t” key.

The above Level Blueprint should have your NPC seeking a random location until you (the

player) press either the “r” or “t” keys. At that point, it should travel to one of the target points.

You can create more NPCs with this ability to act as visitors to the museum, or even artifacts

within your exhibits, such as NPCs performing military drills or sprinting in your Wars and

Conflicts Exhibit.

Page 4: Name: Date: Period: Artificial Intelligence and Non-Player ......Artificial Intelligence and Non-Player Characters: Additional Tutorials DIRECTIONS: Use the tutorials below to further

All images are Screenshots of © Unreal® Engine 4 2019 by Author Virtual History Museum: Artificial Intelligence and Non-Player Characters Distribution A.

NPC: Chase the Playable Character

1. Create another NPC Character (refer to AI and NPC Tutorial steps 9 – 15).

2. This character will walk to the playable character and follow it around the Navigation (Nav)

mesh. Examples of this NPC could be children or members of your party who tour the museum

with you, or perhaps you are playing as a celebrity, and the NPC is your biggest fan! You’ll want

to distinguish this NPC from others, so make sure your name reflects what it will do.

NOTE: This tutorial will have you adding to the Behavior Tree, etc. that you created during the

first AI and NPC Tutorial. You can then add what you learn to a separate NPC later by following

steps 9 – 44 of the AI and NPC Tutorial, and then continuing the steps below.

3. You can change the name of the nodes by selecting the node and typing a new name in the

Details window.

4. Make a new task by selecting the “BTTask_BlueprintBase” option for the “Chase Player” node.

5. In the Event Graph, create the nodes: “Event Receive Execute AI” and “Finish Execute.”

6. To find the player’s location, you must add “get player character.” Drag from “return value” and

add “get actor location.”

7. Drag from “get actor location” and type “navigable” to add “get random point in navigable

radius.”

Page 5: Name: Date: Period: Artificial Intelligence and Non-Player ......Artificial Intelligence and Non-Player Characters: Additional Tutorials DIRECTIONS: Use the tutorials below to further

All images are Screenshots of © Unreal® Engine 4 2019 by Author Virtual History Museum: Artificial Intelligence and Non-Player Characters Distribution A.

8. The “radius” in the “get random point in navigable radius” node will represent the radius around

the player character (your “get” node) that the NPC will encompass when it begins to chase the

player. It appears more natural if this radius isn’t “0,” so change this to a higher number, such as

100.

9. Make a Blackboard Key under “Variables” in the My Blueprint window.

10. In the Details Window, select the Blackboard key by searching “”Blackboard Key Selector” under

the “variable type.” Check the “Instance Editable” box. This should make the variable public.

11. Drag the vector variable into the Event Graph. Select “get vector.”

12. Drag from the vector node and type “set value” to select “Set Blackboard Value as Vector.”

13. Replicate the Blueprint. Don’t forget to check the “success” box and click “compile” and “save.”

14. Close the Blueprint. Rename it “Find Player Location.”

Page 6: Name: Date: Period: Artificial Intelligence and Non-Player ......Artificial Intelligence and Non-Player Characters: Additional Tutorials DIRECTIONS: Use the tutorials below to further

All images are Screenshots of © Unreal® Engine 4 2019 by Author Virtual History Museum: Artificial Intelligence and Non-Player Characters Distribution A.

15. Replicate the Behavior Tree. Make sure you assign the correct vector and key.

16. Remember, the Behavior Tree executes tasks from left to right. Since we added the actions to

the previous NPC, we now need to tell the Behavior Tree when to have the NPC switch from

finding a random location to chasing the player. You wouldn’t have to do this if you made a new

NPC with a new Behavior Tree, but you also then wouldn’t learn how to incorporate Decorators

into Behavior Trees!

Within the Blackboard, make a new “Bool” key. Name it “can see player.” Save it and go back to

the Behavior Tree.

17. Decorator: A conditional node that determines the actions that occur within the Behavior Tree.

Right-click on the top of the “go to random location” node. Select “Add Decorator” and then

“Blackboard.”

Page 7: Name: Date: Period: Artificial Intelligence and Non-Player ......Artificial Intelligence and Non-Player Characters: Additional Tutorials DIRECTIONS: Use the tutorials below to further

All images are Screenshots of © Unreal® Engine 4 2019 by Author Virtual History Museum: Artificial Intelligence and Non-Player Characters Distribution A.

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.

Page 8: Name: Date: Period: Artificial Intelligence and Non-Player ......Artificial Intelligence and Non-Player Characters: Additional Tutorials DIRECTIONS: Use the tutorials below to further

All images are Screenshots of © Unreal® Engine 4 2019 by Author Virtual History Museum: Artificial Intelligence and Non-Player Characters Distribution A.

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.”

Page 9: Name: Date: Period: Artificial Intelligence and Non-Player ......Artificial Intelligence and Non-Player Characters: Additional Tutorials DIRECTIONS: Use the tutorials below to further

All images are Screenshots of © Unreal® Engine 4 2019 by Author Virtual History Museum: Artificial Intelligence and Non-Player Characters Distribution A.

Compile, save and close the Blueprint.

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.

Page 10: Name: Date: Period: Artificial Intelligence and Non-Player ......Artificial Intelligence and Non-Player Characters: Additional Tutorials DIRECTIONS: Use the tutorials below to further

All images are Screenshots of © Unreal® Engine 4 2019 by Author Virtual History Museum: Artificial Intelligence and Non-Player Characters Distribution A.

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:

Page 11: Name: Date: Period: Artificial Intelligence and Non-Player ......Artificial Intelligence and Non-Player Characters: Additional Tutorials DIRECTIONS: Use the tutorials below to further

All images are Screenshots of © Unreal® Engine 4 2019 by Author Virtual History Museum: Artificial Intelligence and Non-Player Characters Distribution A.

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.

Page 12: Name: Date: Period: Artificial Intelligence and Non-Player ......Artificial Intelligence and Non-Player Characters: Additional Tutorials DIRECTIONS: Use the tutorials below to further

All images are Screenshots of © Unreal® Engine 4 2019 by Author Virtual History Museum: Artificial Intelligence and Non-Player Characters Distribution A.

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.

Page 13: Name: Date: Period: Artificial Intelligence and Non-Player ......Artificial Intelligence and Non-Player Characters: Additional Tutorials DIRECTIONS: Use the tutorials below to further

All images are Screenshots of © Unreal® Engine 4 2019 by Author Virtual History Museum: Artificial Intelligence and Non-Player Characters Distribution A.

Your NPC should now travel to a random location if it does not see the player character, and smoothly chase the player character if it sees it.