Driver Script - Boost / Bump Coroutines

Just thought I’d post my version of the implementation of speed boosts / bumps for the driver script:

[SerializeField]
float forwardSpeed = 16.0f, rotationalSpeed = 160.0f, boostSpeed = 32.0f, bumpSpeed = 8.0f, boostTime = 1.0f, bumpTime = 1.0f;
float modSpeed = 0.0f;
bool hitBoost = false, hitBump = false;
Vector2 rotationalForce = new Vector2(0,4);
Rigidbody2D courierRB;

// Start is called before the first frame update
void Start()
{
    courierRB = GetComponent<Rigidbody2D>();
    
}

// Update is called once per frame
void Update()
{
    if (hitBump == true && hitBoost == true)
    {
        StopAllCoroutines();
        modSpeed = 0.0f;
        hitBoost = false;
        hitBump = false;
    }

float thrustAmount = (forwardSpeed + modSpeed) * Input.GetAxis("Vertical");
    float steerAmount = rotationalSpeed * Input.GetAxis("Horizontal");

    transform.Translate(Vector3.up * thrustAmount * Time.deltaTime);
    transform.Rotate(Vector3.forward * -steerAmount * Time.deltaTime);
}

private void FixedUpdate()
{
    //courierRB.AddForce(1, 1);
}

private void OnTriggerEnter2D(Collider2D collision)
{
    if(collision.tag == "Boost")
    {
        hitBoost = true;
        modSpeed = boostSpeed - forwardSpeed;
        StartCoroutine(BoostSpeed());
    }
    else if (collision.tag == "Bump")
    {
        hitBump = false;
        modSpeed = bumpSpeed - forwardSpeed;
        StartCoroutine(DampSpeed());
    }
}

IEnumerator BoostSpeed()
{

    yield return new WaitForSeconds(boostTime);
    hitBoost = false;
    modSpeed = 0.0f;

}

IEnumerator DampSpeed()
{

    yield return new WaitForSeconds(bumpTime);
    hitBump = false;
    modSpeed = 0.0f;

}

Privacy & Terms