Overview | Key Features | Videos | Documentation | Samples | Add Ons | Support

Doc Home > Writing a New Conditional Task

This topic is divided into two parts. The first part describes writing a new conditional task, and the second part (available here) describes writing a new action task. The conditional task will determine if any objects are within sight and the action class will towards the object that is within sight. We will also be using variables for both of these tasks. We have also recorded a video on this topic and it is available here.

The first task that we will write is the Within Sight task. Since this task will not be changing game state and is just checking the status of the game this task will be derived from the Conditional task. Make sure you have the BehaviorDesigner.Runtime.Tasks namespace included:

using UnityEngine;
using BehaviorDesigner.Runtime.Tasks;

public class WithinSight : Conditional
{
}

We now need to create three public variables and one private variable:

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

public class WithinSight : Conditional
{
   public float fieldOfViewAngle;
   public string targetTag;
   public SharedTransform target;

   private Transform[] possibleTargets;
}

The fieldOfViewAngle is the field of view that the object can see. targetTag is the tag of the targets that the object can move towards. target is a shared variable which will be used by both the Within Sight and the Move Towards tasks. If you are using shared variables make sure you include the BehaviorDesigner.Runtime namespace. The final variable, possibleTargets, is a cache of all of the Transforms with the targetTag. If you take a look at the task API, you can see that we can create that cache within the the OnAwake or OnStart method. Since the list of possible transforms are not going to be changing as the Within Sight task is enabled/disabled we are going to do the caching within OnAwake:

   public override void OnAwake()
   {
      var targets = GameObject.FindGameObjectsWithTag(targetTag);
      possibleTargets = new Transform[targets.Length];
      for (int i = 0; i < targets.Length; ++i) {
         possibleTargets[i] = targets[i].transform;
      }
   }

This OnAwake method will find all of the GameObjects with the targetTag, then loop through them caching their transform in the possibleTargets array. The possibleTargets array is then used by the overridden OnUpdate method:

   public override TaskStatus OnUpdate()
   {
      for (int i = 0; i < possibleTargets.Length; ++i) {
         if (withinSight(possibleTargets[i], fieldOfViewAngle)) {
            target.Value = possibleTargets[i];
            return TaskStatus.Success;
         }
      }
      return TaskStatus.Failure;
   }

Every time the task is updated it checks to see if any of the possibleTargets are within sight. If one target is within sight it will set the target value and return success. Setting this target value is key as this allows to Move Towards task to know what direction to move in. If there are no targets within sight then the task will return failure. The last part of this task is the withinSight method:

   public bool withinSight(Transform targetTransform, float fieldOfViewAngle)
   {
      Vector3 direction = targetTransform.position - transform.position;
      return Vector3.Angle(direction, transform.forward) < fieldOfViewAngle;
   }

This method first gets a direction vector between the current transform and the target transform. It will then compute the angle between the direction vector and the current forward vector to determine the angle. If that angle is less then fieldOfViewAngle then the target transform is within sight of the current transform. One thing to note is that unlike MonoBehaviour objects, all tasks already have all of the MonoBehaviour components cached so we do not need to precache the transform component.

That's it for the Within Sight task. Here's what the full task looks like:

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

public class WithinSight : Conditional
{
   // How wide of an angle the object can see
   public float fieldOfViewAngle;
   // The tag of the targets
   public string targetTag;
   // Set the target variable when a target has been found so the subsequent tasks know which object is the target
   public SharedTransform target;

   // A cache of all of the possible targets
   private Transform[] possibleTargets;

   public override void OnAwake()
   {
      // Cache all of the transforms that have a tag of targetTag
      var targets = GameObject.FindGameObjectsWithTag(targetTag);
      possibleTargets = new Transform[targets.Length];
      for (int i = 0; i < targets.Length; ++i) {
         possibleTargets[i] = targets[i].transform;
      }
   }

   public override TaskStatus OnUpdate()
   {
      // Return success if a target is within sight
      for (int i = 0; i < possibleTargets.Length; ++i) {
         if (withinSight(possibleTargets[i], fieldOfViewAngle)) {
            // Set the target so other tasks will know which transform is within sight
            target.Value = possibleTargets[i];
            return TaskStatus.Success;
         }
      }
      return TaskStatus.Failure;
   }

   // Returns true if targetTransform is within sight of current transform
   public bool withinSight(Transform targetTransform, float fieldOfViewAngle)
   {
      Vector3 direction = targetTransform.position - transform.position;
      // An object is within sight if the angle is less than field of view
      return Vector3.Angle(direction, transform.forward) < fieldOfViewAngle;
   }
}

Continue to the second part of this topic, writing the Move Towards task.


<- Parent Tasks
Writing a New Action Task ->