๐ŸŽฎ VR Racetrack Game ๐ŸŽ๏ธ๐Ÿ’จ
Step into the driverโ€™s seat in this thrilling VR racetrack game! ๐Ÿš—๐Ÿ’จ Control your car ๐Ÿ and race around a dynamic track filled with twists and turns. But beware! If your car tips over the track, it will land in the water ๐ŸŒŠ. Watch as it sinks to the bottom ๐ŸŒŠโฌ‡๏ธ, and donโ€™t worryโ€”youโ€™ll respawn and be back on the track in just 3 seconds โฑ๏ธ๐Ÿ”„.
Can you stay on track and race your way to victory? ๐Ÿ†๐Ÿ‘€
Drive Main Script
What the Script Does:
This script controls the movement of the player's car ๐ŸŽ๏ธ in a VR racetrack game. It manages the car's acceleration, braking, and turning based on player input. If the car falls into the water ๐ŸŒŠ and hits the ocean floor ๐ŸŒŠโฌ‡๏ธ, the scene reloads, and the car respawns.
ApplicationData (Static Class) ๐Ÿ“Š
This class stores important game data:
- HitOceanFloor: Tracks if the car has hit the ocean floor (i.e., if it falls off the track). ๐ŸŒŠโฌ‡๏ธ
- DidItHitWater: Indicates if the car landed in the water. ๐Ÿ’ฆ
- CurrentZvelocity / CurrentYvelocity: Tracks the car's speed in the Z and Y directions. ๐ŸŽ๏ธ๐Ÿ’จ
- TimerIsRunning: A flag for a timer, likely for race timing. โฑ๏ธ
- GatePassed: Tracks if the player has passed a gate on the track. ๐Ÿ
- LapCount: Counts the laps completed in the race. ๐Ÿ”
- MyTime: Tracks the current race time. โฒ๏ธ
- Bestlap: Stores the player's best lap time. ๐Ÿ†
- CountDownIsRunning: Indicates if a countdown is active before the race begins. 3... 2... 1... ๐Ÿšฆ
- DriveMainScript (Car Controls) ๐Ÿš—๐Ÿ’จ:
Variables:
- HorizAxis / VertAxis: These store the player's input for steering (horizontal) and accelerating/braking (vertical) controls.
- MyPlayer: A reference to the player's car (or vehicle object).
- ForwardForce: The force applied to the car to move it forward. ๐Ÿš—๐Ÿ’จ
- TurnForce: The force applied to rotate (turn) the car. ๐Ÿ”„
Key Functions:
- Awake() & Start(): These methods run once when the game starts but are currently empty.
- Update(): This method checks player input every frame. Based on the input:
- Move Forward (Up Arrow/W key): Calls GoForward() ๐ŸŽ๏ธ๐Ÿ’จ
- Move Backward (Down Arrow/S key): Calls GoBack() โฌ‡๏ธ
- Stop (No Input): Calls NoGas() (stops the car) โŒ๐ŸŽ๏ธ
- Turn Left (Left Arrow/A key): Calls TurnLeft() ๐Ÿ”„โ†ช๏ธ
- Turn Right (Right Arrow/D key): Calls TurnRight() ๐Ÿ”„โ†ฉ๏ธ
- If the car hits the ocean floor (falls off the track) ๐ŸŒŠโฌ‡๏ธ, it respawns by reloading the scene. ๐Ÿ”„
- GoForward(): Makes the car move forward by applying a force in the Z direction. ๐ŸŽ๏ธ๐Ÿ’จ
- NoGas(): Stops the car by applying zero force in the Z direction. โŒ
- GoBack(): Makes the car move backward by applying a force in the negative Z direction. โฌ‡๏ธ๐ŸŽ๏ธ
- TurnLeft(): Rotates the car left by applying a force that turns it to the left. ๐Ÿ”„โ†ช๏ธ
- TurnRight(): Rotates the car right by applying a force that turns it to the right. ๐Ÿ”„โ†ฉ๏ธ
Start trigger script
Breakdown ๐ŸŽฎ
This script manages the start of a lap in the VR racing game. When the car crosses the start line, it plays a sound and activates the finish line trigger, allowing the player to complete the lap. The start trigger is deactivated after being used.
Key Components
Public Variables:
- StartSound: An AudioSource reference used to play a sound when the player crosses the start line (e.g., a race starting sound or countdown). ๐Ÿ”Š
- FinishTrigger: A reference to the FinishTrigger game object that will be activated once the start trigger is triggered. ๐Ÿšฆ
OnTriggerEnter(Collider other):
- This method is called when another collider enters the trigger zone (the start line).
- It checks if the FrontBumper of the car enters the trigger zone. If true, the player has crossed the start line. ๐Ÿš—๐Ÿ
Start Line Actions:
- The StartSound is played when the car crosses the start line, indicating the start of a lap. ๐ŸŽถ
- The FinishTrigger game object is activated, enabling the player to complete the lap by triggering the finish line when they cross it. ๐Ÿšฆ
- The current StartTrigger is deactivated to prevent it from being triggered multiple times. โŒ
In Summary:
When the player's car crosses the start line (detected by the "FrontBumper"), the script plays a start sound ๐Ÿ”Š, activates the FinishTrigger for lap completion ๐Ÿšฆ, and deactivates itself to ensure it doesn't trigger again. It's part of the race flow, ensuring the player can complete laps one at a time. ๐Ÿ
Finish Trigger Script
Breakdown ๐Ÿ
This script handles the logic for completing a lap in a VR racing game. When the car crosses the finish line (detected by a trigger), it increments the lap count ๐Ÿ†, checks if the player's current lap time is a personal best ๐Ÿ…, and resets the timer for the next lap. It also activates or deactivates the appropriate game objects to control race flow.
Key Components
Public Variable:
- StartTrigger: A reference to the game object that acts as the start trigger for the next lap. This object is activated when the lap is completed. ๐Ÿšฆ
OnTriggerEnter(Collider other):
- This method is triggered when another collider enters the trigger zone (the finish line).
- It checks if the car's FrontBumper collider enters the trigger. If so, it means the car has crossed the finish line! ๐Ÿš—๐Ÿ
Lap Completion:
- The script prints a message: "lap complete" ๐Ÿ“ฃ.
- The LapCount is incremented by 1, keeping track of how many laps the player has completed. ๐Ÿ”ข
- It then checks if the player's current lap time (MyTime) is faster than their Bestlap (the best lap time so far):
- If the current lap is better, Bestlap is updated to the new best time. ๐Ÿ…
- If no best lap has been recorded yet, the script initializes Bestlap with the current lap time. โฑ๏ธ
- MyTime (current lap time) is reset to 0 for the next lap. ๐Ÿ”„
StartTrigger Activation:
- The StartTrigger game object is activated (likely showing the next start line or countdown for the next lap). ๐Ÿšฆ
- The current FinishTrigger object (the one used to detect lap completion) is deactivated to prevent it from triggering multiple times. โŒ
HIt Water Splash Script
HitWaterSplash Script Breakdown ๐ŸŒŠ
This script is responsible for triggering a splash sound effect and marking that the player has hit the water when their car collides with the water in your VR racing game.
Key Components
Start Method:
- Initializes the ApplicationData.DidItHitWater to false. This ensures that the game starts with the assumption that the player hasn't hit the water. ๐Ÿ’งโŒ
OnCollisionEnter(Collision Myplayer):
- This method is called when the car (player) collides with an object that has a Collider attached.
- It checks if the object that collided is tagged as "Player". If true, it proceeds to check if the player has already hit the water. ๐Ÿš—๐Ÿ’ฅ
Water Collision Actions:
- First Collision Check: If the player has not hit the water yet (ApplicationData.DidItHitWater == false), it:
- Prints "hit water" in the console for debugging. ๐Ÿ’ฌ
- Plays the AudioSource component's audio clip (a splash sound effect). ๐ŸŽถ๐ŸŒŠ
- Sets ApplicationData.DidItHitWater to true, indicating that the player has now hit the water. โœ…๐Ÿ’ง
Ocean Floor Script
OceanFloorScript Breakdown ๐ŸŒŠ๐Ÿ’ฅ
This script is responsible for playing a sound effect when the playerโ€™s car collides with the ocean floor and triggering a respawn after 3 seconds if the car hits the bottom of the water.
Key Components:
BangPlayed (bool):
- A flag that tracks whether the sound effect (bang) has already been played upon collision with the ocean floor. ๐Ÿš—๐Ÿ’ฅ
OnCollisionEnter(Collision MyPlayer):
- This method is called when the playerโ€™s car collides with an object that has a Collider.
- It checks if the object involved in the collision is the "Player" by comparing its tag. If the playerโ€™s car hits the ocean floor, it proceeds to play the sound and trigger further actions. ๐ŸŒŠ๐Ÿš—
Sound Effect (BangPlayed flag):
If the BangPlayed flag is false, it:
- Plays the AudioSource attached to the ocean floor object, which could be a splash or crash sound. ๐ŸŽถ๐Ÿ’ฅ
- Sets BangPlayed to true, ensuring the sound effect plays only once until the flag is reset. โœ…๐Ÿ”Š
Invoke("LetBounceOnOceanFloor", 3f):
- This invokes the method LetBounceOnOceanFloor() after 3 seconds, simulating the car bouncing on the ocean floor before respawning. โณ๐Ÿš—๐Ÿ’จ
LetBounceOnOceanFloor():
- This method sets ApplicationData.HitOceanFloor to true, indicating that the playerโ€™s car has hit the ocean floor, and prepares for a respawn.
- It also prints "reload" to the console, signaling that a respawn process is being triggered. ๐Ÿ๐Ÿ”„
Come take a look!
You can find this project and many more in my Github repositories!
Back to Top