Skip to main content

Class NavigationWaypointManager

Manages a linear sequence of NavigationWaypoints and responds to event-aggregator messages that request or update the current position in that sequence.

[DisallowMultipleComponent]
public class NavigationWaypointManager : MonoBehaviour, IHandle<NavigationMessages.WaypointIndexChangedMessage>, IHandle<NavigationMessages.WaypointIndexOffsetMessage>, IHandle<NavigationMessages.WaypointPositionRequestMessage>

Inheritance​

Implements​

Examples​

Advance to the next waypoint from a UI "Next" button without holding a reference to this manager — any system that has access to the event aggregator can do the same:

// In a UI button OnClick handler:
if (UnityServiceLocator.Current.TryGet(out var aggregator))
{
aggregator.Publish(new WaypointIndexOffsetMessage { Offset = 1 });
}

Query the current waypoint world position without polling the component each frame — WaypointPositionRequestMessage delivers the answer through a callback, so the caller never needs a hard reference to NavigationWaypointManager:

if (UnityServiceLocator.Current.TryGet(out var aggregator))
{
aggregator.Publish(new WaypointPositionRequestMessage(position =>
{
if (position.HasValue)
Debug.Log($"Current waypoint is at {position.Value}");
}));
}

Remarks​

Using message types (WaypointIndexChangedMessage etc.) rather than direct method calls keeps the waypoint system decoupled from the UI and the navigation renderer: any component can request the current waypoint position without holding a hard reference to this manager, which simplifies multi-scene study setups where the manager may or may not be present.

Properties​

CurrentWaypointIndex​

Gets or sets the current waypoint index, ensuring it wraps within the valid range.

public int CurrentWaypointIndex { get; set; }

Property Value​

int

Remarks​

The double-modulo expression (value % count + count) % count is required because C#'s % operator is a remainder, not a true modulo: it returns a negative result when value is negative (e.g. -1 % 3 == -1 in C#). Adding count before the second % shifts negative remainders into the positive range so that decrementing past zero wraps correctly to the last waypoint rather than returning a negative index.

Methods​

GetCurrentDestination()​

Retrieves the position of the current waypoint.

public Vector3 GetCurrentDestination()

Returns​

Vector3 — The position of the current waypoint, or Vector3.zero if unavailable.

Handle(WaypointIndexChangedMessage)​

Handles the WaypointIndexChangedMessage to set the waypoint index.

public void Handle(NavigationMessages.WaypointIndexChangedMessage message)

Parameters​

NameTypeDescription
messageNavigationMessages.WaypointIndexChangedMessageThe message containing the new index.

Handle(WaypointIndexOffsetMessage)​

Handles the WaypointIndexOffsetMessage to offset the waypoint index.

public void Handle(NavigationMessages.WaypointIndexOffsetMessage message)

Parameters​

NameTypeDescription
messageNavigationMessages.WaypointIndexOffsetMessageThe message containing the offset amount.

Handle(WaypointPositionRequestMessage)​

Handles the WaypointPositionRequestMessage to provide the current waypoint position.

public void Handle(NavigationMessages.WaypointPositionRequestMessage request)

Parameters​

NameTypeDescription
requestNavigationMessages.WaypointPositionRequestMessageThe position request message.

OffsetWaypointIndex(int)​

Offsets the current waypoint index by the specified amount.

public void OffsetWaypointIndex(int offset)

Parameters​

NameTypeDescription
offsetintThe amount to offset the waypoint index.

SetWaypointIndex(int)​

Sets the current waypoint index to the specified value.

public void SetWaypointIndex(int newIndex)

Parameters​

NameTypeDescription
newIndexintThe new waypoint index.

TryGetCurrentDestination(out Vector3)​

Attempts to get the position of the current waypoint.

public bool TryGetCurrentDestination(out Vector3 destination)

Parameters​

NameTypeDescription
destinationVector3Outputs the position if available.

Returns​

bool — True if the position was retrieved; otherwise, false.

  • Examples
  • Remarks
  • Properties
    • CurrentWaypointIndex
  • Methods
    • GetCurrentDestination()
    • Handle(WaypointIndexChangedMessage)
    • Handle(WaypointIndexOffsetMessage)
    • Handle(WaypointPositionRequestMessage)
    • OffsetWaypointIndex(int)
    • SetWaypointIndex(int)
    • TryGetCurrentDestination(out Vector3)