Custom Task
Instruction to create custom user task
In the AI Tree you can easily create your own task by using scripting API. In this page we will show how you can do it.
Let's create task to move platforms between two point.
This is a very simple example, we have specially chosen a simple example to make it easier to learn this material.
First of all you need to create new script. The easiest way is to use templates. Open context menu in Project window (right mouse click) and go to following path.
Create -> Renowned Games -> AI Tree -> Script Templates -> Task

We'll call it MovePlatformTask, after creation open it script in you scripts editor tool.

Task script template already contains all required code blocks, you just need to add task logic.
We want to create following logic: A task is considered successful if the platform reaches one of the points after which it go to another task.
We will remove methods that are not necessary for this task so as not to overload the code.
Let's focus on each line to understand what they are for.
The label in node content attribute may differ from the name of the class, for example, you can add spaces, symbols, numbers, etc. there.
We renamed the automatically generated MovePlatformTask label to Move Platform, just for nice display in tree graph.
For moving platform we will use Unity transform component. Every gameobject in the scene has this component, so let's store it to easy access in every tick update.
In next step we need to define two point, speed and one vector for destination, for this time let's make them fixed.
Now let's implement the logic of moving between points.
Done. Now we can add our task on behaviour tree window and setup simple tree.
What happed in this tree:
Sequencer running task from left to right (built-in node).
Move Platform task moving self to destination point.
Wait task waiting for a 1 second (built-in node).
Repeat.
First, we move the platform to point B (since it’s already at point A), then wait for one second. On the next entry to the MovePlatform task, we change the destination to point A (since the platform is now at point B) and repeat this process infinitely.

Result

Last updated