Need clarification about Gary's method to get Random spawn times and fire rates

In Gary’s tutorials, in both EnemySpawner and Shooter scripts, to get random spawn times he declares 3 variables, one for base time, one for variance and last for the minimum limit. Later he uses Random.Range to get a random value between Base - variance and base + variance. And at last he clamps the value so that we won’t run into problems with negative values.

I was thinking, why do we have go through this process which is lengthy and little confusing when we could set up a minimum and max limits and use Random.Range(min, max) to get a random value for our spawn or enemy fire rate.

I’ve been using this instead of Gary’s method and I didn’t find any difference at all. Can anyone tell me what is the actual difference between these 2 methods?

Hi Chanukya,

That’s indeed a good question. I assume you are referring to this code, aren’t you?

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

timeToNextProjectile = Mathf.Clamp(timeToNextProjectile, minimumFiringRate, float.MaxValue);

If so, it would be great if you could share your own solution here. Reading a description of code is always tricky because the other person might misunderstand something. When talking about code, share the code if possible.

I think Gary’s idea was to implement a solution for “no randomness” (without having to edit the code) by defining a baseFiringRate. If firingRateVariance is 0f, Random.Range would return the value of baseFiringRate.

Of course, this is not the only solution for adding some randomness to our firing rate. If you have a different approach and if that approach solves the problem, it is a valid solution. :slight_smile:

2 Likes

Thanks for the reply!

Here are the variables that am using

image

And here’s how I’ve set up the “Random fire rate” of the enemy ships

I’ve set in such a way that the fire rate is randomized only if AI is being used. Since, only the enemy ships are using AI, this wouldn’t effect the fire rate of our own ship.

So far, it is working fine for me. I am assuming that this is also one of the possibility…

In my personal opinion, your code looks fine and makes sense. If it works, feel free to keep it. :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms