making enemies spawn faster based on how long the player has been alive

Ive been messing around with the tutorial survival shooter game and wanted to make it more challenging the longer you stayed alive by making the spawn time for the enemies decrease more and more the longer the player survives. I wanted to experiment with this as both a linear and a exponential decrease in spawn time to see which one worked better. however, I could not figure out how to code this in as I am very new to programming and am a complete skrub with it, so any help or advice would be greatly appreciated. thanks in advance.

Something like this?

using UnityEngine;

public class DecreasingSpawnRate : MonoBehaviour
{
    public float spawnRate = 10f;
    public float spawnRateDropSpeed = 0.001f;
    public float spawnRateDeceleration = 0.001f;
    public float minSpawnRate = 1f;

    float lastSpawnTime;
    float accumulatedTime;

    void OnEnable()
    {
        accumulatedTime = 0f;
        lastSpawnTime = 0f;
    }

    void Update()
    {
        accumulatedTime += Time.deltaTime;

        spawnRateDropSpeed += spawnRateDeceleration * Time.deltaTime;
        spawnRate = Mathf.Max(spawnRate - Time.deltaTime * spawnRateDropSpeed, minSpawnRate);
   
        if(accumulatedTime - lastSpawnTime > spawnRate)
        {
            lastSpawnTime = accumulatedTime;
            Spawn();
        }
    }

    void Spawn()
    {
        Debug.Log("Spawned at "+accumulatedTime);
    }
}
1 Like

spawnRate defines the initial spawn rate in seconds, and if you set spawnRateDeceleration to zero the rate will decrease linearly.