Class Speedometer
Abstract base class to use for speedometer interface.
public abstract class Speedometer : MonoBehaviour
Inheritance
Derived
Remarks
This speedometer is designed with the assumption that all instances in the scene will receive speed updates from a single actor only (typically the player). If multiple actors in the scene send ActorSpeedMessage events, it will cause conflicts and unreliable readings, as all Speedometer instances will respond to messages from every actor indiscriminately. Ensure only one actor broadcasts speed updates per scene.
Fields
speedDisplayScale
Scales the converted speed value before it's displayed. Useful for adjusting how fast or slow the speed appears visually.
[Header("SPEED DISPLAY MODIFIERS")]
[Tooltip("Scales the converted speed value before it's displayed. Useful for adjusting how fast or slow the speed appears visually.")]
[SerializeField]
[Min(0)]
protected float speedDisplayScale
Field Value
float
speedDisplayUnit
The unit used to convert the speed from meters per second for display (e.g., MPH, KPH).
[Tooltip("Determines the unit used to convert the speed from meters per second for display (e.g., MPH, KPH).")]
[SerializeField]
protected Speedometer.SpeedUnit speedDisplayUnit
Field Value
Methods
ConvertRawSpeed(float, SpeedUnit)
Converts a speed value expressed in meters per second (m/s) to the specified SpeedUnit.
protected float ConvertRawSpeed(float rawSpeed, Speedometer.SpeedUnit speedUnit)
Parameters
| Name | Type | Description |
|---|---|---|
rawSpeed | float | The speed in meters per second (m/s) to convert. |
speedUnit | Speedometer.SpeedUnit | The target unit to convert into. |
Returns
float — The converted speed expressed in speedUnit.
Exceptions
NotImplementedException — Thrown if an unsupported SpeedUnit value is provided.
SetRawSpeed(float)
Sets the current raw speed sample (in meters per second) for this speedometer.
public abstract void SetRawSpeed(float rawSpeed)
Parameters
| Name | Type | Description |
|---|---|---|
rawSpeed | float | The actor's speed in meters per second (m/s), typically the magnitude of its velocity. |
Remarks
Implementations should convert to the configured display unit, multiply by
speedDisplayScale, and take the absolute value before updating any visuals,
Call this whenever a new speed value is available (e.g., per FixedUpdate).
Keep implementations allocation-free and fast; this may be invoked frequently.
A common pattern is:
var converted = Mathf.Abs(ConvertRawSpeed(Mathf.Max(0f, rawSpeed), speedDisplayUnit) * speedDisplayScale); // Update UI/needle/text/etc. with 'converted'