Creating quick Afterburner ?

I have this code, which moves the drone around with vertical axis

public float movementForwardSpeed = 500.0f;
    private float tiltAmountForward = 0;
    private float tiltVelocityForward;
    void MovementForward()
    {
        if (Input.GetAxis ("Vertical") != 0) {
            ourDrone.AddRelativeForce (Vector3.forward * Input.GetAxis ("Vertical") * movementForwardSpeed);
            tiltAmountForward = Mathf.SmoothDamp (tiltAmountForward, 20 * Input.GetAxis ("Vertical"), ref tiltVelocityForward, 0.1f);
        }
    }

i’m trying to have quick afterburner where when a Key is pressed, the movementforward speed changes to say 3000 for 1 second then back to normal speed, giving arcadey afterburner effect… but i’m not sure how to do this? i’ve tried waitForSeconds or starting a coroutine but both give errors… any suggestions ?

If you’d like some help on the code you tried - for instance the coroutine, please post the code so people can see it :slight_smile:

Oh, that’s true, i just thought the little ways i tried are so petty and so not worth posting, but anyway for example i tried this way:

    public float BurstSpeed = 1000.0f;
    public float CurrentSpeed;
    public float movementForwardSpeed = 500.0f;
    private float tiltAmountForward = 0;
    private float tiltVelocityForward;

    void Start ()
    {
        CurrentSpeed = movementForwardSpeed;
    }
    void MovementForward()
    {
        if (Input.GetAxis ("Vertical") != 0) {
            DroneRB.AddRelativeForce (Vector3.forward * Input.GetAxis ("Vertical") * CurrentSpeed);
            tiltAmountForward = Mathf.SmoothDamp (tiltAmountForward, 20 * Input.GetAxis ("Vertical"), ref tiltVelocityForward, 0.1f);
        }
        if (Input.GetKey (KeyCode.Space)) {
            StartCoroutine (AfterBurner);
        }
    }

    private IEnumerator AfterBurner()
    {
        CurrentSpeed = BurstSpeed;
        yield return new WaitForSeconds (2f);
        CurrentSpeed = movementForwardSpeed;
    }

Okay, so I think you just forgot ’ () ’ in your call to the coroutine. If you add that, is it doing what you want?

StartCoroutine(Afterburner());

Nope :frowning:

What i need sounds really simple, when Space is pressed, CurrentSpeed = BurstSpeed for 2 seconds, then CurrentSpeed is back to being movementForwardSpeed

but i can’t figure out something for it

I think you want to use KeyDown and you probably only want to call this once per burst. Try this…

if (Input.GetKeyDown (KeyCode.Space) && CurrentSpeed != BurstSpeed) {
            StartCoroutine (AfterBurner());
        }

another way is this…

if (Input.GetKeyDown (KeyCode.Space) && CurrentSpeed != BurstSpeed) {
            CurrentSpeed = BurstSpeed;
            Invoke (ResetSpeed, 2.0f);
        }

void ResetSpeed() {
        CurrentSpeed = movementForwardSpeed;
    }

HTH

I’m sorry, that is correct; you should only allow it to activate once until it’s done. I was trying to answer why the coroutine didn’t start, and didn’t “complete” the rest of the code for the situation.

You could also use a bool to indicate you’re using the afterburner.

yeah, for some reason none of the methods here work, the boost only stays for about half a second then goes, so i think i’ll change the system to meter system where if the meter at 0% then boost is not available and recovers automatically when boost is not used.

Ah okay. Are you saying the boost wasn’t staying because you stopped giving input?
Or even with input it stopped?