Runtime Targets and Behavior Patterns
Use shared variables when perception, player commands, or another gameplay system must supply movement targets and destinations.
Work with dynamic targets
Use a Transform or GameObject when a target can move. Use a Vector3 or rotation value for a fixed snapshot. When perception discovers an object, write it to a shared variable and bind Seek, Pursue, Follow, or a combat branch to that same variable.
The moving-target tasks answer different questions:
- Seek routes to the current target position.
- Pursue predicts an intercept from velocity.
- Follow maintains a relationship or distance.
- Flee moves from the current threat position.
- Evade predicts threat movement before choosing an escape destination.
Change a target at runtime
using BehaviorDesigner.Runtime;
using UnityEngine;
public class MovementTargetDriver : MonoBehaviour
{
[SerializeField] private BehaviorTree m_BehaviorTree;
public void SetTarget(GameObject target)
{
m_BehaviorTree.SetVariableValue("MovementTarget", target);
}
public void SetDestination(Vector3 worldPosition)
{
m_BehaviorTree.SetVariableValue("MovementDestination", worldPosition);
}
}
Bind only the variable that matches the task field. A GameObject or Transform remains connected to a moving object; a Vector3 is a snapshot until code writes another value.
See Accessing Variables from non-Task Objects for GetVariable, SetVariableValue, and global-variable examples.
Interrupt a Running action
Changing a variable does not select a different branch. Use a conditional abort when a new target must interrupt a Running Patrol, Search, Seek, or group task.
Avoid running two actions that own the same destination. Let the first task end or be aborted before the replacement begins.
Compose the tasks
- Patrol and chase: Patrol until Can See Object stores a target, then Pursue.
- Search after loss: Search around the last-known position until sight or hearing succeeds or a timeout selects a fallback.
- Maintain attack range: Seek or Pursue while far away, Follow at the preferred range, then Rotate Towards before attacking.
- Retreat: Flee or Evade until Within Distance fails for the threat, then reevaluate.
Use separate memory variables for current detection and last-known state. A perception task returning Failure should not silently preserve a target forever unless the surrounding graph deliberately owns that memory.