Writing a New Action Task

This topic is a continuation of the previous topic. It is recommended that you first take a look at the writing a new conditional task topic first.

The next task that we are going to write is the Move Towards task. Since this task is going to be changing the game state (moving an object from one position to another), we will derive the task from the Action class:

using UnityEngine;
using BehaviorDesigner.Runtime.Tasks;

public class MoveTowards : Action
{
}

This class will only need two variables: a way to set the speed and the transform of the object that we are targeting:

using UnityEngine;
using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;

public class MoveTowards : Action
{
   public float speed = 0; 
   public SharedTransform target;
}

The target variable is a SharedTransform and it will be set from the Within Sight task that will run just before the Move Towards task. To do the actual movement, we will need to override the OnUpdate method:

   public override TaskStatus OnUpdate()
   {
      if (Vector3.SqrMagnitude(transform.position - target.Value.position) < 0.1f) {
         return TaskStatus.Success;
      }
      transform.position = Vector3.MoveTowards(transform.position, target.Value.position, speed * Time.deltaTime);
      return TaskStatus.Running;
   }

When the OnUpdate method is run, it will check to see if the object has reached the target. If the object has reached the target then the task will success. If the target has not been reached yet the object will move towards the target at a speed specified by the speed variable. Since the object hasn’t reached the target yet the task will return running.

That’s the entire Move Towards task. The full task looks like:

using UnityEngine;
using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;

public class MoveTowards : Action
{
   // The speed of the object
   public float speed = 0; 
   // The transform that the object is moving towards
   public SharedTransform target;

   public override TaskStatus OnUpdate()
   {
      // Return a task status of success once we've reached the target
      if (Vector3.SqrMagnitude(transform.position - target.Value.position) < 0.1f) {
         return TaskStatus.Success;
      }
      // We haven't reached the target yet so keep moving towards it
      transform.position = Vector3.MoveTowards(transform.position, target.Value.position, speed * Time.deltaTime);
      return TaskStatus.Running;
   }
}

Now that these two tasks are written, parent the tasks by a sequence task and set the variables within the task inspector. Make sure you’ve also created a new variable within Behavior Designer:

That’s it! Create a few moving GameObjects within the scene assigned with the same tag as targetTag. When the game starts the object with the behavior tree attached with move towards whatever object first appears within its field of view. This was a pretty basic example and the tasks can get a lot more complicated depending on what you want them to do. All of the tasks within the sample projects are well commented so you should be able to pick it up from there. In addition, we have written some more documentation on the continuing topics such as variables, referencing tasks and task attributes.