var spawnRate = 0.5;
InvokeRepeating("makeAsteroid", 0, spawnRate);
I want ‘spawnRate’ to be faster the faster my object is moving and slower the slower it is moving. Eg, if the object is moving really fast ‘spawnRate’ will be 0.1 and if its not moving at all ‘spawnRate’ will be 1.0. How can I do this?
var spawnRate = 0.5;
function SpawnAsteroids()
{
yield WaitForSeconds();
while(true)
{
MakeAsteroid();
yield WaitForSeconds(spawnRate);
}
}
Start it using
StartCoroutine(SpawnAsteroids());
then you can modify spawnRate as you like, and it will change the number of asteroids accordingly. Do it in FixedUpdate, if there's a rigidbody involved.
If you need it to stop, use
StopCoroutine("SpawnAsteroids");
To spawn lots of things as it speeds up, have something like this-
function FixedUpdate () {
// This will make the spawn delay decrease from 2 seconds when
// the object is stationary, 0.1 seconds when it is moving at
// someMultiplier units per second.
spawnRate = Mathf.Lerp(2, 0.1, rigidbody.velocity.magnitude * someMultiplier);
}