Class VehicleChanger
Manages switching between multiple vehicles in a scene with support for both instant and character-based (enter/exit) modes.
Namespace: NWH.Common.SceneManagement
Assembly: NWH.Common.dll
Syntax
[DefaultExecutionOrder(500)]
public class VehicleChanger : MonoBehaviour
Remarks
VehicleChanger supports two modes: - Instant switching: Press a button to cycle through vehicles immediately - Character-based: Player must walk to a vehicle and enter/exit at designated points
In character-based mode, the player can only enter vehicles when near an EnterExitPoint and the vehicle is moving slowly enough. This creates more realistic vehicle switching similar to GTA-style games.
Inactive vehicles can optionally be put to sleep to improve performance when managing many vehicles in a scene.
Fields
| Edit this page View SourceactiveVehicleIndex
Index of the current vehicle in vehicles list.
Declaration
[Tooltip(" Index of the current vehicle in vehicles list.")]
public int activeVehicleIndex
Field Value
| Type | Description |
|---|---|
| int |
characterBased
Is vehicle changing character based? When true changing vehicles will require getting close to them to be able to enter, opposed to pressing a button to switch between vehicles.
Declaration
[Tooltip("Is vehicle changing character based? When true changing vehicles will require getting close to them\r\nto be able to enter, opposed to pressing a button to switch between vehicles.")]
public bool characterBased
Field Value
| Type | Description |
|---|---|
| bool |
characterObject
Game object representing a character. Can also be another vehicle.
Declaration
[Tooltip(" Game object representing a character. Can also be another vehicle.")]
public GameObject characterObject
Field Value
| Type | Description |
|---|---|
| GameObject |
enterDistance
Maximum distance at which the character will be able to enter the vehicle.
Declaration
[Range(0.2, 3)]
[Tooltip(" Maximum distance at which the character will be able to enter the vehicle.")]
public float enterDistance
Field Value
| Type | Description |
|---|---|
| float |
enterExitTag
Tag of the object representing the point from which the enter distance will be measured. Useful if you want to enable you character to enter only when near the door.
Declaration
[Tooltip("Tag of the object representing the point from which the enter distance will be measured. Useful if you want to enable you character to enter only when near the door.")]
public string enterExitTag
Field Value
| Type | Description |
|---|---|
| string |
location
When the location is Near, the player can enter the vehicle.
Declaration
[Tooltip("When the location is Near, the player can enter the vehicle.")]
public VehicleChanger.CharacterLocation location
Field Value
| Type | Description |
|---|---|
| VehicleChanger.CharacterLocation |
maxEnterExitVehicleSpeed
Maximum speed at which the character will be able to enter / exit the vehicle.
Declaration
[Tooltip(" Maximum speed at which the character will be able to enter / exit the vehicle.")]
public float maxEnterExitVehicleSpeed
Field Value
| Type | Description |
|---|---|
| float |
onDeactivateAll
Event invoked when all vehicles are deactivated (e.g., when exiting in character-based mode).
Declaration
public UnityEvent onDeactivateAll
Field Value
| Type | Description |
|---|---|
| UnityEvent |
onVehicleChanged
Event invoked whenever the active vehicle changes.
Declaration
public UnityEvent onVehicleChanged
Field Value
| Type | Description |
|---|---|
| UnityEvent |
putOtherVehiclesToSleep
Should the vehicles that the player is currently not using be put to sleep to improve performance?
Declaration
[Tooltip(" Should the vehicles that the player is currently not using be put to sleep to improve performance?")]
public bool putOtherVehiclesToSleep
Field Value
| Type | Description |
|---|---|
| bool |
startInVehicle
Should the player start inside the vehicle?
Declaration
[Tooltip("Should the player start inside the vehicle?")]
public bool startInVehicle
Field Value
| Type | Description |
|---|---|
| bool |
vehicles
List of all of the vehicles that can be selected and driven in the scene.
Declaration
[Tooltip("List of all of the vehicles that can be selected and driven in the scene.")]
public List<Vehicle> vehicles
Field Value
| Type | Description |
|---|---|
| List<Vehicle> |
Properties
| Edit this page View SourceInstance
Declaration
public static VehicleChanger Instance { get; }
Property Value
| Type | Description |
|---|---|
| VehicleChanger |
Methods
| Edit this page View SourceChangeVehicle(Vehicle)
Switches to the specified vehicle if it exists in the vehicles list.
Declaration
public void ChangeVehicle(Vehicle ac)
Parameters
| Type | Name | Description |
|---|---|---|
| Vehicle | ac | Vehicle reference to switch to. |
ChangeVehicle(int)
Changes vehicle to requested vehicle.
Declaration
public void ChangeVehicle(int index)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | Index of a vehicle in Vehicles list. |
DeactivateAllExceptActive()
Enables the current active vehicle and optionally disables all others based on putOtherVehiclesToSleep setting.
Declaration
public void DeactivateAllExceptActive()
DeactivateAllIncludingActive()
Disables all managed vehicles including the currently active one. Used when exiting vehicles in character-based mode.
Declaration
public void DeactivateAllIncludingActive()
DeregisterVehicle(Vehicle)
Removes a vehicle from the managed vehicles list. If the vehicle was active, automatically switches to the next vehicle.
Declaration
public void DeregisterVehicle(Vehicle v)
Parameters
| Type | Name | Description |
|---|---|---|
| Vehicle | v | Vehicle to deregister. |
EnterVehicle(Vehicle)
Puts the player inside the specified vehicle and activates it. In character-based mode, stores the entry position for later exit.
Declaration
public void EnterVehicle(Vehicle v)
Parameters
| Type | Name | Description |
|---|---|---|
| Vehicle | v | Vehicle to enter. |
ExitVehicle(Vehicle)
Removes the player from the vehicle and spawns the character object nearby. Character is positioned at the stored entry location.
Declaration
public void ExitVehicle(Vehicle v)
Parameters
| Type | Name | Description |
|---|---|---|
| Vehicle | v | Vehicle to exit. |
NextVehicle()
Switches to the next vehicle in the list, wrapping to the first vehicle when reaching the end.
Declaration
public void NextVehicle()
PreviousVehicle()
Switches to the previous vehicle in the list, wrapping to the last vehicle when at the beginning.
Declaration
public void PreviousVehicle()
RegisterVehicle(Vehicle)
Adds a vehicle to the managed vehicles list if not already present. Newly registered vehicles are automatically disabled unless they are the active vehicle.
Declaration
public void RegisterVehicle(Vehicle v)
Parameters
| Type | Name | Description |
|---|---|---|
| Vehicle | v | Vehicle to register. |