Skip to main content

Class ActorSpeedPublisher

Samples the actor's forward speed from ISpeedTracker each frame and distributes it to two independent output channels: the event-aggregator bus and a UnityEvent for Inspector wiring.

[RequireComponent(typeof(ISpeedTracker))]
public class ActorSpeedPublisher : MonoBehaviour

Inheritance​

Examples​

Wire _onSpeedChange in the Inspector to update a world-space speedometer text without any code — drag the TextMeshProUGUI.text setter onto the event slot. Enable _BoardcastToListeners only when a global subscriber (such as ActorSpeedMessageRelay or the data logger) needs the value; leave it off for actors whose speed is only consumed locally to avoid per-frame aggregator overhead:

// Inspector setup:
// _BoardcastToListeners = false
// _onSpeedChange → SpeedometerText.SetRawSpeed (UnityEvent slot)

// When a global HUD or data logger must also receive the speed, enable broadcast:
// _BoardcastToListeners = true
// _onSpeedChange → SpeedometerText.SetRawSpeed (UnityEvent slot)

// Any IHandle subscriber on the aggregator bus will then
// receive ActorSpeedMessage.speed (in m/s) each frame, for example:
public class SpeedLogger : MonoBehaviour, IHandle
{
public void Handle(ActorSpeedMessage message)
=> Debug.Log($"Speed: {message.speed:F1} m/s");
}

Remarks​

The two channels serve different consumer profiles: the event-aggregator channel (_BoardcastToListeners) feeds global subscribers such as the in-car speedometer HUD and the data logger, while the UnityEvent feeds local per-scene connections (e.g. a world-space speed indicator near the actor).

The broadcast is opt-in (default off) to avoid spurious speed messages in scenes where no subscriber has registered, which would waste event-aggregator dispatch overhead every frame.

Methods​

Update()​

Publishes the current speed each frame if the tracker is valid.

public void Update()