I just want my objects to spawn in a faster rate as time goes by. The print(SpawningInBetween); print out shows the percentage of the difficulty overtime.

This is the code I have:

public Vector2 spawnsMinMax;

//Spawn Positions
Vector2 SpawnPosition;
float secondsBetweenSpawn = 1;   
float nextSpawnTime;

void Update()
    if (Time.time > nextSpawnTime)
        float SpawningInBetween = Mathf.Lerp(
        nextSpawnTime = Time.time + SpawningInBetween;

        nextSpawnTime = Time.time + secondsBetweenSpawn;
    // ...

I was thinking of going like this:

if(Time.time > Difficulty.GetDifficultyPercent < 0.9) 

but it got a compiler error.

  • \$\begingroup\$ What exactly is that "error" you got? Perhaps we can tell you how to fix it. \$\endgroup\$ – Philipp Sep 18 '20 at 14:24
  • \$\begingroup\$ it just got a red underline under "Time.time > Difficulty.GetDifficultyPercent < 0.9) \$\endgroup\$ – wq wq Sep 18 '20 at 14:26
  • \$\begingroup\$ And what does Visual Studio say when you move your cursor on that red underline and wait for the tooltip to appear? \$\endgroup\$ – Philipp Sep 18 '20 at 14:27

You can control the spawn rate by changing the value of secondsBetweenSpawn to a value between 1.

One way to do that would be via script. For example, you can reduce the time at a rate of 10% every 5 seconds with this line in your Update function:

secondsBetweenSpawn = Mathf.Pow(0.9f, Time.timeSinceLevelLoad / 5f);

But another solution I find far more elegant is to control such game features with a Timeline asset. It allows you to define the progression of variables over the course of the game with curves which you can edit in a visual editor. Note that before you can control the value of a variable with a timeline, you need to make it public.

  • \$\begingroup\$ no wait. your previous ones before you edit it actually kinda works.. is it ok to post your previous ones too hahaha. This works too but once i reset the game (in runTime i mean), it still spawns at the current spawn rate (which is by that time, it spawns alot) Thanks by the way. You really help \$\endgroup\$ – wq wq Sep 18 '20 at 14:43
  • \$\begingroup\$ @wqwq How do you "reset the game"? If you do it by reloading the scene, then Time.timeSinceLevelLoad should work. But if you reset the game via a script which keeps the scene loaded but changes everything back to how its supposed to be, then you need to separately keep track of the time at which the game was restarted. \$\endgroup\$ – Philipp Sep 18 '20 at 14:46
  • \$\begingroup\$ public class GameManager : MonoBehaviour { public void PlayGame() { SceneManager.LoadScene("FirstProject"); } } This is what i put. I'm sorry as i dont know what else to put it here haha \$\endgroup\$ – wq wq Sep 18 '20 at 14:52
  • \$\begingroup\$ Then timeSinceLevelLoad should actually work. \$\endgroup\$ – Philipp Sep 18 '20 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.