IN THIS ARTICLE
2D Navigation with Kythera AI
This tutorial shows you how to use the Kythera AI Gem to create an area of level that can be navigated by Kythera AI agents, and a simple AI agent that follows a visually scripted behavior tree.
You will need to begin from either your own project with the Kythera AI Gem enabled, or the Kythera AI Demo project. See Kythera AI Gem setup for information on setting up the Kythera AI Demo project.
For this example to work, you first need to create two XML configuration files, a Profile for the agent’s behavior, and a NavMesh that defines the parameters for the navigation area.
The behavior of an AI agent is defined in its profile. One of the most important settings in a profile is the agent’s default behavior tree, which is a visually scripted ‘program’ for the agent to run.
Profiles are defined in XML in the
Profiles.xml in the
Scripts directory of your project. Open (or create) this file and add a new profile,
<Profiles> <WanderProfile type="bb"> <CharacterFeatures type="bb"> <GroundNav type="bool">true</GroundNav> <GroundAvoidance type="bool">true</GroundAvoidance> <Movement type="bool">true</Movement> <Behavior type="bool">true</Behavior> </CharacterFeatures> <DefaultBehavior type="string">WanderBehavior</DefaultBehavior> </WanderProfile> </Profiles>
If you change
Profiles.xml while the editor or game is running, you can reload the file with the console command
2D agents operate on a navigation mesh (navmesh) which is a network of 2D tiles that define the navigable areas of the level. Kythera AI generates these navmeshes automatically according to parameters defined in the
NavMesh.xml. Kythera AI allows for multiple types of navmeshes that can be used by different types of agents. For example, a navmesh type for large creatures prevents them from navigating small spaces.
Navmeshes can be global, meaning they are available in all levels in the project, or level-specific. The global
NavMesh.xml is placed in the project’s
Scripts directory. For level-specific navmeshes, a
NavMesh.xml can be placed in the level’s subdirectory.
<NavMeshParams> <DefaultNavMeshName type="string">Default</DefaultNavMeshName> <DefaultNavMeshType type="string">MediumSizedCharacters</DefaultNavMeshType> <NavMeshNames type="bb"> <Default type="bba"> <Entry0 type="string">MediumSizedCharacters</Entry0> </Default> </NavMeshNames> <NavMeshTypes type="bb"> <MediumSizedCharacters type="bb"> <AgentHeight type="float">1.720000</AgentHeight> <AgentMaxClimb type="float">0.700000</AgentMaxClimb> <AgentMaxSlope type="float">45.000000</AgentMaxSlope> <AgentRadius type="float">0.600000</AgentRadius> <CellHeight type="float">0.050000</CellHeight> <CellSize type="float">0.100000</CellSize> </MediumSizedCharacters> </NavMeshTypes> </NavMeshParams>
Create a new empty level.
Remove the ShaderBall entity.
Select the Grid entity, and add a new PhysX Collider component to it. All entities that will be included in the NavMesh need a PhysX collider.
On the collider, set the shape property to
On the collider, set the X and Y scale to
32, so the box encompasses the grid.
If you activate Debug Helpers (the symbol in the top right corner of the viewport) and the Draw Collider option is active on the PhysX collider component, you should see the collider debug visualization:
Create a new entity called
NavBounds. This will define the area of the level in which navmesh is generated.
Add a Kythera NavMesh Bounds component in the entity inspector.
Add a Polygon Prism Shape component and choose the Edit button to enter edit mode for the prism.
Increase the size of the prism using the four red circles so that it encompasses most of the grid.
Increase the height of the prism with the blue arrow in the center.
Choose Done to exit edit mode.
The Kythera AI toolbar has some functions required for the rest of the tutorial. Right click the O3DE toolbar and select the Kythera AI toolbar to add it if the Kythera AI Toolbar is not visible.
Basic Debug Drawin the drop-down of the Kythera Toolbar. This is the first drop-down in the toolbar, showing
NavMesh Dbg offby default.
Choose the Generate navmesh button () in the Kythera AI Toolbar.
Create a new entity called
Agent. This will be the AI character.
Add a Mesh component and select any available mesh. You can use
Lucy_lowfrom the AtomLyIntegration Gem.
Add a Kythera component to the entity. This component registers the entity with Kythera.
Add a Kythera Agent component to the entity.
In the Agent component, set the Profile property to
WanderProfile, as defined in
Add a SimpleMovementController component. This component translates movement requests from Kythera AI into movement of the Agent entity by implementing the
MovementRequestBus. For more information, refer to Character movement APIs .
Note:Animation is not yet supported.
The Inspector is Kythera AI’s browser based debugger and behavior tree authoring tool. The Inspector webserver is started on the local machine when the Kythera Gem is initialized. While Kythera AI is running, the Inspector is available at http://localhost:8081/ .
We will define a very basic behavior tree that randomly generates a position on the NavMesh and then moves to that position. After arriving, it will generate another position and move there to, in an endless loop.
Open the Inspector
Choose the BT Editor tab.
Create a new behavior tree called
WanderBehavior(as referenced in the
Add a Repeater node. This node executes the child nodes the number of times specified by the iterations property. By default, iterations is set to
0, which means endless iteration. Leave the default value of
Add a Sequence node as a child node of the Repeater node. This node executes all child nodes attached to it one after another. However, if one fails, the execution will stop.
Add a Character_RandomPointInRange node as a child of the Sequence node. This node finds a random point on the navmesh.
Set Range to
Set the Point output to
NextPoint. This is the name of the variable that contains the found point.
Character_Gotonode. This node moves a character to a specific position. This node is fully integrated into the Kythera navigation system, so it will use the navmesh to pathfind a way to the target.
Set Destination to
Set Speed to
The finished behavior tree should look like the following image:
Back in the editor, activate Simulate (Ctrl+P) and see the agent move!
You can also activate the debug draw of the paths the agent is walking along the navpaths button on the Kythera AI toolbar ().
Below is the finished example, with the NavMesh debug draw setting changed from
Color Tiles and with Nav Path debug draw activated:
If something is not working, you can check the Kythera AI log file at
<project directory>/user/log/Kythera.log for more information.