Why are we using 3 variables for the firing rate?

In the end the value is selected between a range of 2 values. Why not just use a minFiringRate and a maxFiring rate in the Random.Range function?

Hi Alejandro,

You are right. We could have achieved the same result with your solution. However, if you read the following code, Gary’s idea becomes quite clear, at least in my opinion.

float timeToNextProjectile = Random.Range(baseFiringRate - firingRateVariance,
                                            baseFiringRate + firingRateVariance);

Gary probably had game designers in mind who is not supposed to edit the code. A game designer might want to define a base firing rate, and maybe a little variance. If there should not be any variance, one could set those values to 0.

Of course, this is just an idea. It’s not how it must be done. You may write the code as you want. As long as it solves a problem in this game, it’s a solution.

Nevertheless, as game developers, we also have to keep the situation in mind. If we write code that nobody but us reads, we just have to keep us and our future self in mind. If we work in a team, we also have to keep the team members in mind who might need what we do, and who are not supposed to inadvertendly destroy our work.

I hope this made sense. :slight_smile:


See also:

The code is used for both the enemies and the player. The way Gary did this allows us to set a base firing rate with no variance for the player, and a base firing rate with some variance for the enemies.

No variance for the player could have been achieved with two variables having the same value. If firingRateVariance is set to 0, we get Random.Range(baseFiringRate, baseFiringRate). Instead of the variance variable, the solution could have been Random.Range(minFiringRate, maxFiringRate) where minFiringRate = maxFiringRate.

This is true, but the question was “why”. I merely stated “why” we are using it the way we are. There are certainly a host of different ways this could have been achieved.

Privacy & Terms