#Boost pad

1 messages · Page 1 of 1 (latest)

mild spire
#

I am trying to add a boost pad feature to my car game for jumps and gaps. I figured it was easier to have it in the car script than to put every car on the dash script. Anyways I went ahead and made a IEnumerator for the function and a OnTriggerEnter to access it. I want to increase the stat (public float) of the moveForce(speed) I know I'm missing something. Help!?

#
    {
        if (gameObject.CompareTag("Booster"))
        {
            Boost();
        }
    }

    IEnumerator Boost()
    {
        HoverCar.moveForce * 2;
        yield return new WaitForSeconds(.3f);
        HoverCar.moveForce / 2;
    }
}
gentle yacht
#

If you call an IEnumerator like a normal method, it will behave like one.
What you want to do, is pass the value Boost() returns into StartCoroutine().
And you also probably want to make sure to stop a running boost before applying a new one.

mild spire
gentle yacht
#

Is the float increment correct?
Arithmetically? I guess.
Programmatically? No. You are calculating values, but you are not telling your program what to do with the results of those calculations.
You need to re-assign them to the variable: HoverCar.moveForce = HoverCar.moveForce * 2f; (or the shorter version, HoverCar.moveForce *= 2f;). Likewise for the division.

That being said, this is a rather unreliable approach.
What happens if you drive over a boost, while another is still active?
You would double the already doubled force again. This may lead to complications or unwanted behaviour.
A better approach may be to have separate values for the "defaultMoveForce" and the "boostedMoveForce", which you swap out depending on whether you are currently boosting.
That would also give you more flexibility because you are not bound to arithmetic operations to determine the final force.