Skip to content

AI Brain

Aprius edited this page Oct 25, 2024 · 5 revisions
graph TD;
    AIBrain-->AIContext;
    AIBrain-->AIAction;
    AIBrain-->Consideration;
    AIBrain-->Sensor;
Loading

Requirement

  • AI Navigation

image

  • Sensor

AI Action

Is a ScriptableObject that represents a specific action for an object.

By default it will have two basic properties:

  • targetTag : a string constant indicating the tag of the object that the action is targeting

  • consideration : a scriptable object of type Consideration

Whenever you create a new specific action type, you need to inherit from the AIAction class and implement the Execute method, the main logic for handling the action's behavior is written here.

In addition to Execute, AIAction has two more methods:

  • CalculatePiority used to calculate the priority based on the calculation of Consideration
  • Initialize a virtual method used to initialize

There are two sample actions provided: IdleAIAction and MoveToTargetAIAction

    public class MoveToTargetAIAction : AIAction
    {
        public override void Initialize(AIContext context)
        {
            if (!context.Sensor.tags.Contains(targetTag)) context.Sensor.tags.Add(targetTag);
        }

        public override void Execute(AIContext context)
        {
            var target = context.Sensor.GetClosestTarget(targetTag);
            if (target == null) return;

            context.Target = target;
            context.Agent.SetDestination(target.position);
        }
    }

Consideration

As a ScriptableObject, it contains operations to determine the priority of the Action, the Action with the highest priority will be selected to execute.

There are 4 types of pre-made Consideration:

  • Constant Consideration: does not calculate anything but only returns the input constant
  • Curve Consideration: the final value varies according to the value represented by AnimationCurve, the input data is determined by AIContext through a key of type StringConstant, the value in this AIContext needs to be normalized in the range from 0 to 1
float hpPercent = currentHp / MaxHp;
context.SetData<float>("player_hp", hpPercent);

...
  • In-Range Consideration: similar to CurveConsideration but it has additional conditions of MaxDistance and MaxAngle
  • Composite Consideration: synthesizes multiple considerarions based on the expression

AI Context

Acts as a data communication bridge between AIAction, Consideration and the external environment. The data is stored as key-value pairs contained in a Dictionary<string, object> and manipulate the data through two methods GetData and SetData

We can also access AIBrain's Agent and Sensor through context.

    public class CurveConsideration : Consideration
    {
        public AnimationCurve curve;
        public StringConstant key;

        public override float Evaluate(AIContext context)
        {
            var value = context.GetData<float>(key.Value);
            return curve.Evaluate(value).Clamp01();
        }

        private void Reset() { curve = new AnimationCurve(new Keyframe(0f, 1f), new Keyframe(1f, 0f)); }
    }

Sensor

There are 2 types of sensors available: Range Sensor and Ray Sensor (supports both 2D and 3D)

image

  • raycastRate : sensor scan frequency

raycastRate = 1 => Raycast after every frame

raycastRate = 2 => Raycast every 2 frames

  • layer : determines the layer of the object that the sensor will find
  • detectOnStart : If enabled, the sensor will automatically scan when the object containing the sensor is active.
  • tags : used to filter objects that the sensor scans. Objects with tags not in this list will be removed from the search results.
  • newTagSystem : if true it will use the tag defined via the Tag component (using List to define the object tag) instead of using unity's legacy tag

image

  • radius : search radius.
  • stopAfterFirstHit : stop searching if any results are found.
  • source : take this object as the center of the scan area, it is like the eye of the storm
  • detectedEvent : event called every time the scanner finds a result
Clone this wiki locally