Skip to main content

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​

Speedometer.SpeedUnit

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​

NameTypeDescription
rawSpeedfloatThe speed in meters per second (m/s) to convert.
speedUnitSpeedometer.SpeedUnitThe 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​

NameTypeDescription
rawSpeedfloatThe 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'
  • Remarks
  • Fields
    • speedDisplayScale
    • speedDisplayUnit
  • Methods
    • ConvertRawSpeed(float, SpeedUnit)
    • SetRawSpeed(float)