Skip to main content

Class PlayerCarDriver

A layer between the player's input controller and the car's physics system. It adapts raw Unity Input System values from PlayerController into the throttle, steering, and gear signals consumed by CarController.

[RequireComponent(typeof(CarController))]
public class PlayerCarDriver : MonoBehaviour, IControllable, IMoveable, IHasUnityContext, IGearChangeable, ISteerable, IMaxSpeedProvider

Inheritance​

Implements​

Remarks​

This driver layer exists to decouple the input-system bindings from the physics controller: the same CarController can also be driven by the AI path-follow system without any changes to input handling code. The driver owns the smoothing and gear-change timing logic that is specific to human input.

The _gearChangeTimer debounce prevents the vehicle from toggling between Drive and Reverse the moment the speed crosses zero — a common issue when the physical Fanatec pedals produce slight residual input. The vehicle must hold the opposite input for the full timer duration before the gear actually flips.

Properties​

Gear​

Gets the current gear type of the component.

public GearType Gear { get; }

Property Value​

GearType

MaxSpeed​

Gets the maximum movement speed of the object in meter per second.

public float MaxSpeed { get; }

Property Value​

float

SteerAmount​

Gets the current steering amount, normalized to the range [-1, 1].

public float SteerAmount { get; }

Property Value​

float

Methods​

ChangeGear(float)​

Changes the gear based on the input value.

  • Input >= 1f: Changes to Drive gear
  • Input <= -1f: Changes to Reverse gear
public void ChangeGear(float input)

Parameters​

NameTypeDescription
inputfloatThe directional input to determine the target gear.

Move(Vector2)​

Applies a normalised 2D movement input to the unit.

public void Move(Vector2 input)

Parameters​

NameTypeDescription
inputVector2Normalised input vector where x is the lateral/steering axis and y is the forward/throttle axis, both in the range [−1, 1].

SetSteerInput(float)​

Sets the steering input value.

public void SetSteerInput(float input)

Parameters​

NameTypeDescription
inputfloatThe input value representing steering direction and magnitude.

Stop()​

Brings the unit to a halt, equivalent to Move(Vector2.zero) but may additionally disengage physics forces on the concrete implementation.

public void Stop()