moveSpeed effect fades away

Made it so that speed boost/slowDown effect is gone after some time using Coroutines.
Speed

int moveSpeed;
Coroutine? moveSpeedEffect;

void ChangeMoveSpeed(int newSpeed) {
    if (moveSpeedEffect != null) {
        StopCoroutine(moveSpeedEffect);
    }

    moveSpeed = newSpeed;
    moveSpeedEffect = StartCoroutine(ChangeMoveSpeedBackToNormal());
}

IEnumerator ChangeMoveSpeedBackToNormal() {
    yield return new WaitForSeconds(moveSpeedEffectDuration);
    moveSpeed = normalMoveSpeed;
    moveSpeedEffect = null;
}

Also made backwards movement slower as well as fixed steering when moving backwards - when you move backwards and steer left - it moves left instead of right.
Backwards

if (move >= 0) {
    steer *= -1;
} else {
    move /= 2;
}

transform.Rotate(Vector3.forward, steer * steerSpeed * Time.deltaTime);
transform.Translate(0, move * moveSpeed * Time.deltaTime, 0);
6 Likes

Fantastic job

I haven’t used Coroutines yet, so my approach was to simply store the time when the collision happens, and if more than 5 seconds have elapsed, I reset the speed back to normal.

    [SerializeField] float steerSpeed = 300;
    [SerializeField] float startingMoveSpeed = 20;
    private float moveSpeed;
    [SerializeField] float slowSpeed = 5;
    [SerializeField] float boostSpeed = 30;
    private float timeSinceCollision = -1;

    void Start()
    {
        moveSpeed = startingMoveSpeed;
    }
    // Update is called once per frame
    void Update()
    {
        if (Time.time > 0 && Time.time >= timeSinceCollision + 5)
        {
            moveSpeed = startingMoveSpeed;
            timeSinceCollision = -1;
        }
        float steerAmount = Input.GetAxis("Horizontal") * steerSpeed * Time.deltaTime;
        float moveAmount = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
        transform.Rotate(0, 0, -steerAmount);
        transform.Translate(0, moveAmount, 0);
    }

    void OnCollisionEnter2D(Collision2D other)
    {
        timeSinceCollision = Time.time;
        moveSpeed = slowSpeed;
    }
1 Like

hello, what does Time.time mean?
I do not get this part…

if (Time.time > 0 && Time.time >= timeSinceCollision + 5)
{
moveSpeed = startingMoveSpeed;
timeSinceCollision = -1;
}

Time.time is a unity class Time property

The statement is simply checking to see if that Time is greater than 0, and if that same Time is greater than or equal to timeSince Collision + 5.
timeSinceCollision variable can be found being assigned to Time.time in the OnCollisionEnter2D function below the Update function.

Gameplay:
We start the game. Time.time is called and keeps track of how long the game is open. We drive around and collide with a bush. This triggers the OnCollisionEnter2D function. We have a slower speed now because we hit something. This also saves the time we hit something. Update then triggers every frame, and checks to see if the Time.time(current time) is great than or equal to the saved time + 5.

Say we collided with that bush 2 minutes in. Time.time would equal 120.000 and save that to the timeSinceCollision. Then every frame it’ll check the Time.time. Only when the current time is greater than 120 + 5 will it return us to normal speed, and reset our timeSinceCollision to -1

Sorry if this is convoluted

3 Likes

thank you, your clarification helped me understand it better. I need to practice using it though.

Hello Narshe1412 I was wondering if you encountered this issue with your code.
When I hit a Boost I get the boostSpeed until it expires. I cannot use another Boost after that UNLESS I hit an untagged or “other” object.
This is to say that I cannot use boostSpeed again UNLESS I trigger slowSpeed which seems really weird.
Any ideas?

1 Like

Privacy & Terms