How to avoid decrease in velocity after collision

Hello everyone,
I am creating an endless car game where car speed is increasing by 1 in every 2 seconds. After collision with road sides car must not slow down. I am using below script for this:

IEnumerator IncreaseSpeed()
    {
        while(true)
        {
            yield return new WaitForSeconds(2f);
            speed++;
            rB.velocity = Vector3.forward * speed;
        }
    }

How to avoid decrease in velocity after collision?

Thanks

Cheap and cheerful: put line 7 in your Update() function…

2 Likes

I tried this and put line 7 in FixedUpdate but no help.

Well that’s the correct quick and easy step so something else is causing it to not function.

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime, such as right where you set that velocity in FixedUpdate().

Plus… put something in the coroutine to ensure it runs. Remember that just typing IncreaseSpeed(); in your Start() function does NOT run the coroutine. Review coroutines to see what I mean.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

1 Like