0

I'm trying to make objects (meteors) that spawn in my game increase in spawnrate over a certain duration, but I can not for the life of me seem to be able to allow it to do so. I started by using an invokeRepeating but since you place that in the start method none of the variables can be increased since it's only called once at the start. Therefore I changed it to a coroutine instead but I am still struggling heavily. This is my first time dealing with Coroutines.

My code is as follows:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MeteorSpawner : MonoBehaviour
{

[SerializeField] GameObject meteorPrefab; //get the prefab to spawn
[SerializeField] float SpawnRate = 0.5f; //amount of time until second spawn
[SerializeField] float minTras; //minimum place to spawn
[SerializeField] float maxTras; //maximum place to spawn
[SerializeField] Transform meteorPos;

float maxSpawnRate = 1f;
float minSpawnRate = 0.1f;
float spawnrateincrease;
// Start is called before the first frame update
void Start()
{
    StartCoroutine(AddingMeteoriteRoutine());
    spawnrateincrease = (maxSpawnRate - minSpawnRate) / 40;
}

private void Update()
{
    
    SpawnRate = spawnrateincrease * Time.deltaTime;
    Debug.Log(SpawnRate);
}


private IEnumerator AddingMeteoriteRoutine()
{
    while (true)
    {
        yield return new WaitForSeconds(SpawnRate);
        SpawnMeteors();
    }
}


void SpawnMeteors()
{
    float devider = Random.Range(1, 3);
    float randomspawnposition = Random.Range(0, 3/devider);
    meteorPos.position += Vector3.right * randomspawnposition; 
    Instantiate(meteorPrefab, meteorPos.position, meteorPos.rotation);
    meteorPos.position -= Vector3.right * randomspawnposition;
}
}

would anyone have any idea on how to actually properly make this function. Apologies for any spaghetti code that you might've just read.

Rick
  • 11
  • 2

0 Answers0