Class PathFollowBehavior
Drives an AI actor along a CinemachinePathBase by issuing move commands to an IMoveCommandInvoker each frame or on-demand.
[DisallowMultipleComponent]
public class PathFollowBehavior : MonoBehaviour, IPathFollow
Inheritance
Derived
Implements
Examples
There are two wiring modes; choose based on whether the actor has an
AIBrainComponent driving it or not.
Mode 1 — Autonomous (simple NPCs without a Behaviour Tree)
Enable AutoSendMoveCommandOnUpdate = true in the Inspector. The component
calls FollowPath from Update automatically every frame so no
external code is required. Suitable for background traffic NPCs that always follow
their assigned path with no conditional logic.
Mode 2 — BT-driven (actors controlled by AIBrainComponent)
Leave AutoSendMoveCommandOnUpdate = false. The BT task calls
FollowPath explicitly, so movement only happens when the BT decides
the actor should move. This is the pattern used by AIBrainComponent.MoveToDestination.
Enabling AutoSendMoveCommandOnUpdate on a BT-controlled actor would send a
second command every frame, causing the invoker to reset the movement command
continuously and producing erratic behaviour.
// Inside a Panda BT [Task] method on AIBrainComponent:
[Task]
void MoveToDestination()
{
// pathFollower is an IPathFollow resolved from the same GameObject.
pathFollower.FollowPath(); // delegates to IMoveCommandInvoker.MoveAlongPath
ThisTask.Succeed();
}
// HasValidDestination() guards the BT node so movement stops at the path end:
[Task]
bool HasDestination()
{
return pathFollower.HasValidDestination();
}
Remarks
Path following is kept separate from the AI brain so that simple NPC actors (not
controlled by a Behaviour Tree) can follow a path by enabling
AutoSendMoveCommandOnUpdate, while BT-controlled actors leave that flag off and
instead call FollowPath explicitly from within a BT task. Mixing both would
double-apply movement commands every frame, producing conflicting velocity inputs to the
vehicle controller.
The ArrivalRadius is expressed in 2D (XZ plane) distance to prevent a vehicle that
is slightly above or below the path's world height from being considered not-arrived, which
would cause it to stall at the end of the path indefinitely.
Methods
FollowPath()
Advances the object one step along its current path toward the destination, updating both position and orientation.
public void FollowPath()
Remarks
The look-ahead settings are passed with every call because they describe how to follow this path, not how the invoker behaves in general; point- and transform-based moves ignore them. The invoker reuses an already-running command for the same path and radius, so the values are read when a new path command starts rather than every frame.