Race Creation
Last updated
Last updated
Make sure a single instance a "Race Manager" component exists in the scene.
Create an empty game object and drag and drop it into the "Start Position" property of the "Race Spawner" component. Also, drag and drop the "Race Manager" component.
Next, Race Manager requires a component that inherits from the "PositionEvaluator" class. This component is supposed to represent a race track as a spline and for a given world position, return a direction to follow.
You can create such a script by yourself, but CustomVehicleController already comes with the "CVCSplinePositionEvaluator" component that uses Unity's Splines and Mathematics packages.
Make sure "Splines" and "Mathematics" packages are installed in the package manager:
Add the "CVCSplinePositionEvaluator" component to the game object with the "RaceManager" script and add the reference:
In the scene view, select the orthographic type of view and change the orientation to look down on the ground (top right menu):
In the Hierarchy, right click and select Splines -> Draw Spline Tool:
Start drawing the race layout by left clicking. Try not to make the distance between points too big (> 50 metres). The first point should be at the start of the race. It's important because the race progress is calculated starting from it.
If you want to make a looped track layout, click on the first spline point.
You can modify the points' position by changing their value from a Spline Container component, which exists on a recently created "Spline" game object.
You can edit the spline by clicking the following button in the scene view (if the spline container is selected):
Drag and drop the spline into the "CVCSplinePositionEvaluator":
Adjust the "RacePos" game object's position to the start of the track.
In the "Race Spawner", adjust the Rows, Columns, Horizontal Distance, Vertical Distance values as you see fit.
Every car that will take part in the race must have the "Race Participant" component. When you added the "AI Brain" component, "Race Participant" component was added as well.
Add the reference to the CustomVehicleController component and the collider of the vehicle's body. The collider should be approximately the size of the vehicle's model, otherwise, AI won't be able to detect it with raycasts.
Set the "Is Player" boolean to the appropriate value.
In Start method, the cars will automatically register to the race if Race Manager component exists in the scene,
At this point you should be able to start the race and the vehicles should spawn.
Property | Description |
---|---|
Spawn Condition
Possible values: "In Awake" , "After External Call". If you select the second option, you must get a referenct to this component and call the "Spawn" method.
BeginCountdownAfterSpawn
If set to True and "Race Start Condition" on the "Race Manager" is set to "After External Call" the countdown will start after spawning the vehicles.
Spawn Player
Boolean that decides wheter to spawn the player
Unique Only
If true: spawns a single instance of every prefab from the Racer AI Vehicle Prefab Array. If false, instantiates random car for every available spawn position.
Randomize Player Pos
Randomizes player's spawn position index
Player Pos Index
Specifies player's spawn position index.
Player Vehicle Prefab
Prefab for the player's vehicle. Must have the "Race Participant" component with the "Player" boolean set to True.
Racer AI Vehicle Prefab Array
Prefab array for the bots prefabs. Must have the "Race Participant" component with the "Player" boolean set to False.