On this page
Module guide

Drag Racing

Stage an RCCP 2 vehicle and accelerate through the finish line. Read the reaction time, elapsed time and finish speed.

Install the module

If you imported the complete Driving Sandbox, this module is already installed. Go to the next section. Use these steps only for RCCP2-DragRacing.unitypackage, made with Export Module > Drag Racing.

Installation order for Drag Racing
  1. If any required packages are missing, install Input System, Editor Coroutines and Unity UI in Package Manager.
  2. Install Realistic Car Controller Pro 2 v4.1.3 or later. The RCCP 2 installer stops if a required package is missing.
  3. Open Edit > Project Settings > Player > Other Settings.
  4. Set Active Input Handling to Input System Package (New) or Both.
  5. If Unity requests a restart, restart the Editor.
  6. If TMP Essential Resources are missing, select Window > TextMeshPro > Import TMP Essential Resources.
  7. Use Assets > Import Package > Custom Package to import RCCP2-DragRacing.unitypackage.

Drive the example

Drag Racing at the start of Play Mode
No. Item
1 RCCP dashboard: speed, gear and engine RPM
2 Run status
3 Progress bar
4 RETRY button
5 INFO button
6 Lane button
7 Staging lights
  1. Open Assets/RCCP 2 Driving Sandbox/Modules/DragRacing/Samples/Drag Racing Example.unity.
  2. Enter Play Mode. The vehicle starts stopped in the lane A staging box.
  3. To race in lane B, select Use Lane B before you launch. Both lanes measure 400 m.
  4. Tap Space to start the staging lights. Press W to rev the engine in neutral during the 3-second countdown.
  5. When the light turns green, the gearbox selects first gear. Hold W to launch. Press Shift or RB when the HUD shows SHIFT UP.
  6. After the finish, brake with S. Read the elapsed time, finish speed and reaction time in the HUD.
  7. Select RETRY to return to the staging box of the active lane.
One run, from the staging box to the result

Select INFO in the sample to read the rules.

The Info panel

Switch lanes before you launch or after a run. The switch moves the vehicle to the selected staging box and clears the timing result. The other lane's staging lights stay off.

The challenge sets the assigned vehicle to manual transmission and disables ABS, ESP and TCS. The HUD shows SHIFT UP when engine RPM and road speed reach the gearbox's upshift thresholds. If the engine reaches the rev limiter while the prompt shows, the HUD shows REV LIMIT. Change gears with the RCCP controls. The progress bar fills as the vehicle covers the lane.

The three intervals of a run

The green-to-start interval is the reaction time. The reaction time starts at green, so a late launch gives a long reaction time. The start-to-finish interval is the elapsed time. The challenge uses the vehicle's Rigidbody position to detect crossings. Only a start line crossing before green gives a false start. Leaving the active lane after starting ends the run.

Task result feedback

A finished run and a false start

The HUD shows a green banner when you finish. A false start or a lane exit during the race gives a red banner. The HUD keeps the result until you select RETRY or switch lanes.

The banner stays fully visible for 3 seconds between fades. A new attempt hides the banner sooner. Play continues during the banner. You can use the HUD buttons while the banner is visible.

The supplied HUD has a Result Notification reference. Clear Result Notification to hide the banner and keep the standard HUD. Keep the included Shared/UI/SandboxTaskNotification.cs script in the project when you use the HUD.

Controls

Action Key
Throttle W or Up Arrow
Brake S or Down Arrow
Handbrake and start the lights Space
Steer A / D or Left / Right Arrow
Shift up Shift or gamepad RB
Shift down Ctrl or gamepad LB
Change camera C
Drag Racing keys

Use it in your scene

If you use the complete Driving Sandbox, open your scene. Select your RCCP vehicle in the Hierarchy. On the Drag Racing card, select Add to Scene.

The tool adds one lane with a start line, a retry marker and the HUD. To add the optional light tree, see Configure the staging lights.

Keep 400 m of flat, straight road ahead of Start Line and about 90 m beyond the finish. If the road is too short, decrease Lane Length. Put the finish line art on the red line of the gizmo. Complete any other steps in the setup dialog.

Relative asset paths start at Assets/RCCP 2 Driving Sandbox/Modules/DragRacing/.

In a single module package, or to do it manually, use these steps.

Before you start, prepare a scene with a solid driving surface. To test, drag Assets/Realistic Car Controller Pro 2/Prefabs/Prototype/RCCP_Sedan01.prefab into the scene. Place the vehicle at its normal spawn height above the surface.

To add a driving camera, select Tools > BoneCracker Games > Realistic Car Controller Pro 2 > Add to Scene > RCCP Camera. Then disable the original Main Camera. For the dashboard, select Tools > BoneCracker Games > Realistic Car Controller Pro 2 > Add to Scene > UI Canvas.

If the scene has no RCCP Scene Manager, select Tools > BoneCracker Games > Realistic Car Controller Pro 2 > Add to Scene > Scene Manager. If the scene has more than one vehicle, assign your vehicle to Active Player Vehicle on RCCP Scene Manager. Clear Register Last Vehicle As Player. If this option is on, the last eligible vehicle can take the player slot at startup.

  1. Add Prefabs/Drag Race.prefab to the scene.
  2. Drag your RCCP vehicle from the Hierarchy to Vehicle. If RCCP.SpawnRCC creates the vehicle at runtime, set the Vehicle property in your code.
  3. Create an empty GameObject on your start line. Turn it until its blue Z axis points toward the finish.
  4. Assign the empty GameObject to Start Line.
  5. Set Lane Length, Lane Half Width, Staging Depth and Countdown Seconds.
  6. Select the challenge to see the lane in the Scene view. The yellow box is the staging box. The white lines are the lane edges. The green line is the start. The red line is the finish.
  7. Put your finish line art on the red line.
  8. Create a second empty GameObject about 0.5 m behind the start line, at the spawn height of your vehicle. Point its blue Z axis toward the finish.
  9. Assign the second empty GameObject to Retry Spawn. The cyan sphere shows it in the Scene view. Place the vehicle there before Play.
  10. For a second lane, assign Second Start Line and Second Retry Spawn. Lane B uses the same length, width and staging rules.
  11. For the HUD, add Prefabs/Drag Racing HUD.prefab to the scene. The prefab has its own Canvas. Assign the challenge to Challenge on the HUD.
  12. If the scene has no EventSystem, select GameObject > UI > Event System. Use an Input System UI Input Module on the EventSystem.
  13. Use On Race Finished to read ActiveLane, ReactionSeconds, ElapsedSeconds and FinishSpeedKph.

The Drag Race Challenge applies its gearbox and assist rules while enabled, also away from the strip. In a larger scene, keep the component disabled until the vehicle reaches the strip. Disable the component to restore the transmission and ABS, ESP and TCS settings.

Drag Race: 1 Vehicle, 2 Start Line, 3 Retry Spawn, 4 Second Start Line, 5 Second Retry Spawn. Drag Racing HUD: 1 Challenge
Lane A with DragRaceManager selected
Both lanes and the retry spawn of lane B

The HUD connects its RETRY and lane buttons in code. Do not add an On Click () call for Retry. For a single lane, leave both second lane fields empty. The HUD hides the lane button. The race can run with the HUD disabled.

Timing uses the vehicle's Rigidbody position, not its front bumper. The staging box is Staging Depth long and ends at the start line. At the sample spawn, the front bumper is past the painted start line.

Give the vehicle a run-off area after the finish. At 140 km/h, the calculated braking distance at 0.9 g is about 86 m.

The INFO panel in the HUD describes the example. Change the text or delete the INFO button in your scene.

The example scene, the strip art and the lens materials in Samples are sample-only. Prefabs/Drag Race.prefab and Prefabs/Drag Racing HUD.prefab do not use files from Samples.

Configure the staging lights

The example has a staging light tree for each lane. The Drag Race prefab supplies the challenge component. Create a staging light tree for each lane in your scene.

  1. Select each tree. Select Add Component > BoneCracker Games > Driving Sandbox > Drag Racing > Drag Race Lights. Assign the race controller to Challenge.
  2. Set Lane Index to 0 for Lane A and 1 for Lane B.
  3. Assign the amber renderers to Amber Lights in the order they illuminate.
  4. Assign the corresponding renderers to Green Light and Red Light.
  5. Assign materials to Off, Amber, Green and Red.
Drag Race Lights: 1 Challenge, 2 Lane Index, 3 Amber Lights. The example hierarchy

Script reference

Add using BoneCrackerGames; and using BoneCrackerGames.DrivingSandbox.DragRacing; to your script.

Member Purpose
Vehicle Gets or sets the vehicle. A different vehicle selects lane A, stores its pose for Retry and clears the result.
IsConfigured True when Vehicle is a scene object and Start Line is assigned. The run waits while it is false.
State Staging, Countdown, Green, Racing, Finished, FalseStart or OutOfLane.
IsWaitingForDriver True until the handbrake or throttle input reaches 0.1 in the attempt. Brake input does not start the lights. The staging lights wait until then. Retry() and ResetRun() make the lights wait again.
ReactionSeconds Seconds from green to the start line crossing.
ElapsedSeconds Seconds from the start line crossing to the finish crossing.
FinishSpeedKph Speed at the finish line in km/h.
DistanceMeters Distance from the start line during the run.
Progress01 Fraction of the lane completed, from 0 to 1.
CountdownRemaining Seconds to green during the countdown.
CountdownSeconds / LaneLength The configured countdown and lane length.
ActiveLane 0 for lane A, 1 for lane B.
HasSecondLane / CanSwitchLane True when lane B is configured, and when a lane switch is possible now. Read CanSwitchLane to update your UI.
SelectLane(int) Selects lane 0 or 1. A lane change moves the vehicle and clears the result. Returns false if the lane or a lane switch is unavailable.
SwitchLane() Selects the other lane.
Retry() Moves the vehicle to the active lane's Retry Spawn and clears the result. If Retry Spawn is empty, lane A uses the vehicle's stored starting pose. During a replay it does nothing.
ResetRun() Clears the result without moving the vehicle.
OnRaceFinished Called once when the vehicle crosses the finish.

Troubleshooting

Symptom Cause Fix
Another vehicle responds to the keys. Register Last Vehicle As Player is on, or Active Player Vehicle points to another vehicle. Assign your vehicle to Active Player Vehicle. Clear Register Last Vehicle As Player.
The HUD shows SETUP NEEDED. Vehicle or Start Line is empty, or Vehicle refers to a prefab asset. Read the warning in the Console. Assign the field from the Hierarchy.
The HUD shows ASSIGN THE CHALLENGE ON THIS HUD. Challenge on the HUD is empty. Assign the Drag Race challenge to Challenge on the HUD.
The HUD shows TAP SPACE TO START THE LIGHTS. The lights wait for the first handbrake or throttle input of the attempt. Brake input does not start them. Tap Space. Press W to rev during the countdown and launch on green.
The countdown does not start. The vehicle's Rigidbody position is not in the staging box, or the vehicle moves faster than 0.25 m/s. Select the challenge and look at the yellow box. Move Retry Spawn into the box. Stop the vehicle fully.
RETRY does nothing in your own HUD. Retry Button on the HUD is empty. Assign the Retry button to Retry Button. Remove any On Click () call that also calls Retry.
The HUD buttons do not respond to clicks. The EventSystem or its Input System UI Input Module is missing. Add the missing object or component.
The Console says that lane B needs both fields. Only one of Second Start Line and Second Retry Spawn is assigned. Assign both fields, or clear both for one lane.
The vehicle drives off the end of the strip. The run-off area after the finish is too short. Extend the ground collider after the finish and add an end barrier.