Create an Entity Condition when a transition check needs to run through Unity’s ECS workflow. It is suited to projects that already use ECS or checks that profiling shows must scale across many entities. For an ordinary scene-object check, a GameObject Condition is usually the simpler starting point.

Add the Condition to a transition

After the example scripts compile, select a transition in the graph and add Within Distance ECS Condition from the Documentation/ECS category in the Element Inspector. The Condition appears as a reusable transition choice, with Origin, Target, and Distance fields taken from the serialized values on its authoring class.

Configure those fields to describe the distance check. The Condition reports whether that check is valid; it does not change states or perform gameplay work directly.

The illustrated Waiting → Move To Point transition uses Continuous evaluation with Origin: (0, 0, 0), Target: (0, 0, 4), and Distance: 5.

Waiting enters the standalone Move To Point ECS Action through a distance Condition, then reaches Arrived when the Action finishes.

The Waiting to Move To Point transition evaluates WithinDistanceECSCondition continuously using All Conditions, Origin (0, 0, 0), Target (0, 0, 4) and Distance 5.

Authoring workflow

An Entity Condition has four parts:

  1. An authoring Condition class that appears in the editor.
  2. A runtime buffer component containing the condition values.
  3. An enableable flag identifying when the Condition is active.
  4. A system, with an optional job, that evaluates the runtime data.

Unlike a GameObject Condition, an Entity Condition does not return a bool from IsValid(). Its ECS system increments TransitionComponent.ValidConditionCount when the check is true.

Authoring Condition class

This class stores the fields shown in the Element Inspector and builds runtime condition data for a specific transition index.

using Opsive.StateDesigner.Runtime.Conditions;
using Unity.Entities;
using UnityEngine;

/// <summary>
/// Returns valid when the origin is within distance of the target.
/// </summary>
[Opsive.Shared.Utility.Category("Documentation/ECS")]
[Opsive.Shared.Utility.Description("Returns valid when origin is within distance of target.")]
public class WithinDistanceECSCondition : ECSCondition<WithinDistanceECSConditionSystem, WithinDistanceECSConditionComponent, WithinDistanceECSConditionFlag>
{
    [Tooltip("Origin position.")]
    [SerializeField] protected Vector3 m_Origin;
    [Tooltip("Target position.")]
    [SerializeField] protected Vector3 m_Target;
    [Tooltip("Distance threshold.")]
    [SerializeField] protected float m_Distance = 5f;

    /// <summary>
    /// Creates runtime ECS data for this condition and transition.
    /// </summary>
    /// <param name="transitionIndex">The transition buffer index this condition belongs to.</param>
    /// <returns>The initialized condition buffer data.</returns>
    public override WithinDistanceECSConditionComponent GetBufferElement(ushort transitionIndex)
    {
        return new WithinDistanceECSConditionComponent
        {
            TransitionIndex = transitionIndex,
            Origin = m_Origin,
            Target = m_Target,
            Distance = m_Distance
        };
    }

}

This is the class added in the Element Inspector. It prepares data rather than evaluating the Condition itself. The transitionIndex identifies the transition entry that the system must update.

Runtime buffer component

This struct is the runtime data the Condition system reads.

using Unity.Entities;
using Unity.Mathematics;

/// <summary>
/// Runtime ECS data for WithinDistanceECSCondition.
/// </summary>
public struct WithinDistanceECSConditionComponent : IBufferElementData
{
    public ushort TransitionIndex;
    public float3 Origin;
    public float3 Target;
    public float Distance;
}

TransitionIndex maps this Condition entry to the correct TransitionComponent in the entity’s transition buffer.

Active flag

This enableable tag controls when the Condition system processes the entity.

using Unity.Entities;
/// <summary>
/// ECS enableable tag indicating WithinDistanceECSCondition is active.
/// </summary>
public struct WithinDistanceECSConditionFlag : IComponentData, IEnableableComponent { }

System and job

The Condition system runs in ConditionSystemGroup. When the runtime data is valid, it increments transition.ValidConditionCount.

using Opsive.StateDesigner.Runtime.Components;
using Opsive.StateDesigner.Runtime.Groups;
using Opsive.StateDesigner.Runtime.States;
using Opsive.StateDesigner.Runtime.Transitions;
using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;

/// <summary>
/// Runtime system for WithinDistanceECSCondition.
/// </summary>
[DisableAutoCreation]
[UpdateInGroup(typeof(ConditionSystemGroup))]
public partial struct WithinDistanceECSConditionSystem : ISystem
{
    private EntityQuery m_Query;

    /// <summary>
    /// Builds and caches the entity query used by this condition.
    /// </summary>
    /// <param name="state">The current ECS system state.</param>
    [BurstCompile]
    private void OnCreate(ref SystemState state)
    {
        m_Query = SystemAPI.QueryBuilder().WithAllRW<TransitionComponent>().WithAll<StateComponent, WithinDistanceECSConditionComponent, WithinDistanceECSConditionFlag, EvaluateFlag>().Build();
    }

    /// <summary>
    /// Schedules the condition job for matching entities.
    /// </summary>
    /// <param name="state">The current ECS system state.</param>
    [BurstCompile]
    private void OnUpdate(ref SystemState state)
    {
        state.Dependency = new WithinDistanceECSConditionJob().ScheduleParallel(m_Query, state.Dependency);
    }

    /// <summary>
    /// Job that evaluates the condition and updates transition validity counts.
    /// </summary>
    [BurstCompile]
    private partial struct WithinDistanceECSConditionJob : IJobEntity
    {
        /// <summary>
        /// Executes condition logic for a single entity's buffers.
        /// </summary>
        /// <param name="transitionComponents">Transition buffer.</param>
        /// <param name="stateComponents">State buffer.</param>
        /// <param name="conditionComponents">Condition runtime data buffer.</param>
        [BurstCompile]
        public void Execute(ref DynamicBuffer<TransitionComponent> transitionComponents, in DynamicBuffer<StateComponent> stateComponents, in DynamicBuffer<WithinDistanceECSConditionComponent> conditionComponents)
        {
            for (int i = 0; i < conditionComponents.Length; ++i) {
                var condition = conditionComponents[i];
                if (condition.TransitionIndex >= transitionComponents.Length) {
                    continue;
                }

                var transition = transitionComponents[condition.TransitionIndex];
                if (transition.SourceIndex >= stateComponents.Length) {
                    continue;
                }

                var sourceState = stateComponents[transition.SourceIndex];
                if (sourceState.Status == StateStatus.Inactive) {
                    continue;
                }

                var shouldEvaluate = transition.EvaluationMode switch
                {
                    EvaluationMode.Continuous => true,
                    EvaluationMode.OnStateFinished => sourceState.Status == StateStatus.Finished,
                    EvaluationMode.OnIterationComplete => sourceState.IterationComplete,
                    _ => true
                };

                if (!shouldEvaluate) {
                    continue;
                }

                if (math.distance(condition.Origin, condition.Target) <= condition.Distance) {
                    transition.ValidConditionCount++;
                    transitionComponents[condition.TransitionIndex] = transition;
                }
            }
        }
    }
}

This system checks each Condition entry, skips inactive or untimed evaluations, and increments ValidConditionCount when the distance check passes. It does not transition directly; transition triggering happens later in TransitionEvaluationSystem.

How it runs

State Designer creates a WithinDistanceECSConditionComponent for the configured Condition. Its TransitionIndex connects the runtime values to the correct transition, and WithinDistanceECSConditionFlag controls when the system processes the entity.

The job checks the source state’s status and follows the transition’s Evaluation Mode. When the configured points are within Distance, it increments ValidConditionCount. TransitionEvaluationSystem then applies the transition’s All or Any Condition Mode and changes state when its requirements pass. Cleanup systems reset the count for the next evaluation cycle.

Verify the Condition

After the scripts compile, confirm that:

  • Within Distance ECS Condition is available on a transition under Documentation/ECS.
  • Selecting the transition shows Origin, Target, and Distance in the Element Inspector.
  • In Play Mode, values within the configured distance allow the transition when its Evaluation Mode permits evaluation.
  • Values outside the configured distance leave the Condition invalid.

If the Condition appears but does not evaluate, confirm that the entity has the required State Designer buffers and flags and that the system query includes TransitionComponent, StateComponent, WithinDistanceECSConditionComponent, WithinDistanceECSConditionFlag, and EvaluateFlag.

Related: