Quest: Drift Driving Quest
Challenge: Speed Boost
Feel free to share your solutions, ideas and creations below. If you get stuck, you can find some ideas here for completing this challenge.
Quest: Drift Driving Quest
Challenge: Speed Boost
Feel free to share your solutions, ideas and creations below. If you get stuck, you can find some ideas here for completing this challenge.
I used an IEnumerator for this one so I it had a timer which I can vary the boost time depending on the boost Pad.
IEnumerator Boost(float boostTime)
{
float startSpeed = forwardSpeed;
forwardSpeed = forwardSpeed * 2;
float totalTime = 0;
while (totalTime <= boostTime)
{
totalTime += Time.deltaTime;
yield return null;
}
forwardSpeed = startSpeed;
}
Speed Boost:
I took the most simple approach here with a collider on the boost pad and On Trigger Enter and on Trigger Exit…
This implies of course that the player can accelerate both “backwards” and “forwards” on a boost pad.
Since the player already can exist in two states, grounded and not grounded… when in grounded state, it can also exist as “boosted” or not. If boosted is true, in the CarController script, in the FixedUpdate, I added a “boost multiplier” that simply multiplies the moveInput by a number (say x5) while the player is in the boost pad collider range.
I ended up using a LayerMask and a method similar to CheckIfGrounded to indicate if the player is on a turbo pad.
void CheckIfTurbo()
{
isOnTurboPad = Physics.CheckSphere(transform.position, distanceCheck, turboLayers, QueryTriggerInteraction.Ignore);
}
After that it was simply just to add a boost modifier when moving forwards in the MovementInput.
if (moveInput > 0)
{
moveInput *= isBoosting ? forwardSpeed * boostModifier : forwardSpeed;
}
The isBoosting bool is active for a customizable duration after the player enters a boost pad.
I also created a CheckIfTurbo method that returns a bool and then just added:
if(CheckIfTurbo()) moveInput = moveInput * 2;
before the AddForce to the isGrounded in the Forced Update method.
I just decided to do something simple and just add-force whenever the player collides with the speed boost:
bool touchSpeedBoost = Physics.CheckSphere(transform.position, distanceCheck, turboLayers, QueryTriggerInteraction.Ignore);
if (touchSpeedBoost)
{
Debug.Log("Speed Up");
sphereRigidbody.AddForce(transform.forward * speedBoost, ForceMode.Acceleration);
}