Skip to main content

Class AICommand

A concrete command issued by Timeline playables or game logic that directs an AI actor to move to a location, follow a transform, or stop.

public class AICommand : ICommand

Inheritance​

object→AICommand

Implements​

ICommand

Examples​

The following reproduces the construction pattern used by AICommandMixerBehaviour when a Timeline clip becomes active. The mixer resolves the bound actor, picks a constructor overload based on the clip's CommandType, and calls Execute exactly once per clip entry — the idempotency guard inside IMoveCommandInvoker ensures issuing the same target every frame is safe.

// GoToLocation: drive the actor to a fixed world-space waypoint
Vector3 waypointPosition = new Vector3(10f, 0f, 5f);
AICommand gotoLocationCmd = new AICommand(actorGameObject, waypointPosition);
gotoLocationCmd.Execute(); // calls IMoveCommandInvoker.MoveToLocation(Vector3)

// GoToTransform: follow a moving target (e.g., another actor's root)
Transform targetTransform = targetGameObject.transform;
AICommand gotoTransformCmd = new AICommand(actorGameObject, targetTransform);
gotoTransformCmd.Execute(); // calls IMoveCommandInvoker.MoveToLocation(Transform)

// Stop: halt the actor immediately (no payload needed)
AICommand stopCmd = new AICommand(actorGameObject);
stopCmd.Execute(); // calls IMoveCommandInvoker.PauseCommand()

Remarks​

AICommand bundles the target actor and the movement payload into a single object so that Timeline clips can create commands at authoring time and execute them at runtime without knowing the actor's concrete type — they only need a supporting invoker (e.g., IMoveCommandInvoker) interface on the actor's root.

The payload pattern (ICommandPayload) decouples the command type from the data it carries. Execute pattern-matches on the payload type to dispatch to the correct invoker method, which means adding a new movement type requires only a new ICommandPayload subclass and a matching if branch — not a new command class.

Constructors​

AICommand(GameObject, Transform)​

Initializes a new AICommand to move the AI to a specific target transform.

public AICommand(GameObject actor, Transform transform)

Parameters​

NameTypeDescription
actorGameObjectThe GameObject that will execute the command.
transformTransformThe target transform to move to.

AICommand(GameObject, Vector3)​

Initializes a new AICommand to move the AI to a specific world position.

public AICommand(GameObject actor, Vector3 position)

Parameters​

NameTypeDescription
actorGameObjectThe GameObject that will execute the command.
positionVector3The target position (world space).

AICommand(GameObject)​

Initializes a new AICommand to stop all current actions. No payload is needed for this command type.

public AICommand(GameObject actor)

Parameters​

NameTypeDescription
actorGameObjectThe GameObject that will execute the command.

Methods​

Execute()​

Advances the command by one step; called every frame by the invoker until the command reaches its completion condition.

public void Execute()

Remarks​

For commands that are one-shot (e.g., Stop), this should complete immediately. For multi-frame commands (e.g., move to a distant position), this drives the motion each frame and the command clears itself via a completion callback when the goal is reached.

Kill()​

Immediately aborts the command, releasing any resources it holds and signalling the invoker to clear the active command slot.

public void Kill()

Remarks​

Called by the invoker when a new conflicting command arrives, or by external code (e.g., a Timeline clip ending) when the command should no longer run regardless of whether it reached its goal.