Increasing speed for a few seconds

How would I increase the speed of my character for just a few seconds?

This is what I've got, but it doesn't seem to work.

bool ApplyPickup (PlayerScript playerStatus)
{
    switch (pickupType)
    {
        case PickupType.Health:
        healthPickup();
        break;

        case PickupType.Speed:
        PlayerScript.moveSpeed += 5000;
        StartCoroutine("speedPickup");
        break;
    }

    return true;
}

IEnumerator speedPickup ()
{
    yield return new WaitForSeconds(3);
    PlayerScript.moveSpeed = PlayerScript.originalSpeed;
}

You could set a variable to say, 0 for seconds then increment it with Time.time and when it hit say, 3 seconds you could stop the temp speed increase.

Time.time is "the time in seconds since the start of the game".

I assume that speedPickup() is only called when you actually pick up the object, so it's only being called once. If it occurs more than 6 seconds after the start of the game, speedBoost will immediately set the moveSpeed back to the originalSpeed.

What you could do is:

void speedPickup ()
{
   AIscript.moveSpeed += 50;
   yield WaitForSeconds (6);
   revertSpeed();
}

void revertSpeed ()
{
   AIscript.moveSpeed = AIscript.originalSpeed;
}

(Preserving original answer and adding edits below)


Have you tried adding debug printouts to your code? I use this to debug that the code is being executed when/where I'd expect.

bool ApplyPickup (PlayerScript playerStatus)
{
    print("ApplyPickup() called");           // ApplyPickup() has been called
    switch (pickupType)
    {
        case PickupType.Health:
          healthPickup();
        break;

        case PickupType.Speed:
          print("Speed pickup found");       // found pickup of Enum type 'Speed'
          PlayerScript.moveSpeed += 5000;
          StartCoroutine("speedPickup");
        break;
    }

    return true;
}

IEnumerator speedPickup ()
{
    print("speedPickup() running");          // speedPickup() Coroutine running
    yield return new WaitForSeconds(3);
    PlayerScript.moveSpeed = PlayerScript.originalSpeed;
    print("moveSpeed reverted to original"); // reverted the move speed
}

If all 4 messages print out when you pickup the speed boost, then your problem is with the application of your moveSpeed to your player movement. Otherwise, seeing which messages do and don't appear will help track down the problem.