Scene Manager

Table of Contents

The RCCP_SceneManager is a singleton component that acts as the central hub for all RCCP vehicles at runtime. It tracks every vehicle in the scene, manages the active player vehicle, connects the camera and UI automatically, caches terrain data for surface detection, and applies global runtime settings such as fixed timestep and frame rate overrides.

You do not need to interact with RCCP_SceneManager directly in most cases. The static helper class RCCP wraps its methods so you can call RCCP.RegisterPlayerVehicle(), RCCP.Transport(), and other convenience methods from anywhere in your code.


Adding the Scene Manager to Your Scene

Menu path: Tools > BoneCracker Games > Realistic Car Controller Pro > Create > Scene Managers > Add Scene Manager

If you forget to add it manually, RCCP_SceneManager will auto-create itself at runtime the first time any RCCP system accesses RCCP_SceneManager.Instance. This is because it inherits from RCCP_Singleton, which creates a new GameObject automatically when the singleton is first requested.

That said, it is good practice to place the Scene Manager in your scene ahead of time so you can configure its Inspector properties (such as registerLastVehicleAsPlayer) before entering Play mode.


Key Properties

PropertyTypeDefaultDescription
activePlayerVehicleRCCP_CarControllernullThe vehicle currently receiving player input. Set via registration methods.
activePlayerCameraRCCP_CameranullThe RCCP camera following the player vehicle. Auto-assigned when an RCCP camera spawns.
activePlayerCanvasRCCP_UIManagernullThe UI manager displaying dashboard gauges, buttons, etc. Auto-assigned when the UI spawns.
activeMainCameraCameranullUnity's Camera.main reference, updated every frame.
allVehiclesListemptyEvery RCCP vehicle in the scene, including AI vehicles. Vehicles are added on spawn and removed on destroy.
registerLastVehicleAsPlayerbooltrueWhen enabled, any newly spawned RCCP vehicle is automatically registered as the player vehicle.
disableUIWhenNoPlayerVehicleboolfalseWhen enabled, the UI canvas is hidden whenever there is no active player vehicle.

Vehicle Registration

Registering a vehicle tells the Scene Manager "this is the player's car." The camera and UI automatically switch to the registered vehicle.

Registering a Vehicle

Use the static RCCP class from any script:


// Register a vehicle as the player vehicle
RCCP.RegisterPlayerVehicle(myVehicle);

// Register with controllable state (true = player can drive it)
RCCP.RegisterPlayerVehicle(myVehicle, true);

// Register with controllable state and engine state
RCCP.RegisterPlayerVehicle(myVehicle, true, true);
OverloadParametersBehavior
RegisterPlayerVehicle(vehicle)Vehicle onlySets the vehicle as active. Camera follows it.
RegisterPlayerVehicle(vehicle, isControllable)Vehicle + controllable flagSame as above, plus enables or disables player input on the vehicle.
RegisterPlayerVehicle(vehicle, isControllable, engineState)Vehicle + controllable + engineSame as above, plus starts or stops the engine.

When a vehicle is registered, the following happens automatically:

  1. activePlayerVehicle is set to the new vehicle.
  2. If an RCCP camera exists, it calls SetTarget() to follow the new vehicle.
  3. On the next frame, the OnVehicleChanged and OnVehicleChangedToVehicle events fire (see API Reference for event details).

De-Registering a Vehicle


RCCP.DeRegisterPlayerVehicle();

This disables player control on the current vehicle, sets activePlayerVehicle to null, and tells the camera to remove its target.

Automatic Registration

If registerLastVehicleAsPlayer is enabled (the default), every vehicle that spawns via RCCP.SpawnRCC() or is already in the scene will be registered automatically. The last vehicle to spawn becomes the player vehicle.

If you are managing multiple vehicles and want manual control over which one the player drives, set registerLastVehicleAsPlayer to false and call RCCP.RegisterPlayerVehicle() yourself.


Transport (Teleport)

Transport instantly moves a vehicle to a new position and rotation. This is useful for respawning after a crash, moving to a checkpoint, or switching between gameplay areas.

Overloads


// Teleport the active player vehicle
RCCP.Transport(newPosition, newRotation);

// Teleport a specific vehicle
RCCP.Transport(targetVehicle, newPosition, newRotation);

// Teleport a specific vehicle, with optional velocity reset
RCCP.Transport(targetVehicle, newPosition, newRotation, resetVelocity);
OverloadParametersDescription
Transport(position, rotation)Position + RotationTeleports the active player vehicle. Always resets velocity.
Transport(vehicle, position, rotation)Vehicle + Position + RotationTeleports a specific vehicle. Always resets velocity.
Transport(vehicle, position, rotation, resetVelocity)Vehicle + Position + Rotation + boolTeleports a specific vehicle. If resetVelocity is false, the vehicle keeps its current speed.

What Happens During Transport

  1. Rigidbody interpolation is temporarily set to None to prevent visual glitches.
  2. Linear and angular velocity are zeroed (unless resetVelocity is false).
  3. The vehicle is moved via Rigidbody.MovePosition() and Rigidbody.MoveRotation().
  4. All wheel collider motor torques are reset to zero.
  5. If the vehicle has a connected trailer, the trailer is moved along with it, maintaining its relative offset.
  6. Physics.SyncTransforms() is called to ensure the physics engine recognizes the new position immediately.

Example: Respawn at a Checkpoint


public Transform respawnPoint;

public void RespawnPlayer() {
    RCCP.Transport(respawnPoint.position, respawnPoint.rotation);
}

Terrain Caching

RCCP_SceneManager caches Unity Terrain splatmap data at startup so that wheel colliders can look up ground materials efficiently at runtime. This is how the system knows whether a wheel is on grass, asphalt, gravel, or any other surface defined in your Ground Materials setup.

How It Works

  1. On Start(), the Scene Manager calls GetAllTerrains() as a coroutine.
  2. It collects all active terrains via Terrain.activeTerrains.
  3. For each terrain, it caches:
  1. Once complete, the terrainsInitialized flag is set to true.

Relevant Properties

PropertyTypeDescription
allTerrainsTerrain[]Array of all Unity Terrains found in the scene.
terrainsTerrains[]Cached terrain data (splatmap, alphamap dimensions, physics material) for each terrain.
terrainsInitializedboolTrue once all terrain data has been cached and is ready for ground material queries.

If your scene does not use Unity Terrains (for example, you use mesh-based roads only), these properties will remain empty and the flag will stay false. Ground material detection will then rely on physics material assignments on your mesh colliders instead.


Multithreading Support

RCCP_SceneManager checks at startup whether the current platform supports async/await operations. Some platforms (such as WebGL) do not support multithreading.


RCCP_SceneManager.multithreadingSupported  // static bool

The check works by attempting to run a trivial Task.Run() call. If it completes within one second, multithreadingSupported is set to true. If it fails or times out, it is set to false and RCCP falls back to synchronous methods.

This flag is used primarily by the Damage System for async mesh deformation and repair operations.

Note: Multithreading can be disabled globally in RCCP Settings by unchecking the multithreading toggle, regardless of platform support.


Runtime Settings Applied on Awake

When RCCP_SceneManager initializes, it reads several values from RCCP_Settings and applies them to the Unity runtime:

SettingConditionEffect
Fixed TimestepoverrideFixedTimeStep is trueSets Time.fixedDeltaTime to the configured fixedTimeStep value (default 0.02).
Frame RateoverrideFPS is trueSets Application.targetFrameRate to the configured maxFPS value (default 120).
Telemetry UIuseTelemetry is trueInstantiates the telemetry overlay prefab for real-time vehicle data display.
Input RebindsautoSaveLoadInputRebind is trueLoads saved input rebind overrides from PlayerPrefs on Awake, and saves them on disable.

These overrides are applied once during Awake. If you need different values at runtime, you can modify Time.fixedDeltaTime or Application.targetFrameRate directly after the Scene Manager initializes.


Events Fired by the Scene Manager

The Scene Manager monitors the active player vehicle each frame and fires events when it changes:

EventSignatureWhen It Fires
RCCP_Events.OnVehicleChangedvoid()A different vehicle becomes the active player vehicle.
RCCP_Events.OnVehicleChangedToVehiclevoid(RCCP_CarController)Same as above, but passes the new vehicle as a parameter.

Example: Listen for Vehicle Changes


void OnEnable() {
    RCCP_Events.OnVehicleChanged += OnPlayerVehicleChanged;
}

void OnDisable() {
    RCCP_Events.OnVehicleChanged -= OnPlayerVehicleChanged;
}

void OnPlayerVehicleChanged() {
    Debug.Log("Player switched to: " + 
        RCCP_SceneManager.Instance.activePlayerVehicle.name);
}

The Scene Manager also listens to these internal events to track vehicles:

EventPurpose
OnRCCPSpawnedAdds the vehicle to allVehicles. Registers as player if registerLastVehicleAsPlayer is true.
OnRCCPAISpawnedAdds the AI vehicle to allVehicles.
OnRCCPDestroyedRemoves the vehicle from allVehicles.
OnRCCPAIDestroyedRemoves the AI vehicle from allVehicles.
OnRCCPCameraSpawnedSets activePlayerCamera. If a player vehicle already exists, sets it as the camera target.
OnRCCPUISpawnedSets activePlayerCanvas.

Additional Methods

MethodParametersDescription
SetBehavior(int behaviorIndex)Index of the behavior presetActivates behavior override mode and applies the preset at the given index from the behavior types array in RCCP_Settings.
SetMobileController(MobileController type)TouchScreen, Gyro, SteeringWheel, or JoystickSwitches the active mobile controller type. See Mobile Input.
ChangeCamera()NoneCycles to the next camera mode on the active RCCP camera. See Camera System.

Common Issues

Camera not following the vehicle

UI not showing

Vehicle not responding to input

Multiple vehicles but wrong one is controlled

Terrain surface detection not working


Next Steps


Support: bonecrackergames@gmail.com | www.bonecrackergames.com

Need help? See Troubleshooting