- Home /
increase float when another one decreases
Hey im trying to create a float for example called spawnRate for my IEnumerator. The IEnumerator will under a while loop spawn objects. The function will keep spawning objects and before each spawn it will yield a waitforseconds and the float argument will be the spawnRate variable. There is a main object that moves around that will spawn these objects and i want make use of the rigidbody of this main object, its velocity magnitude float variable to control the spawnRate variable. Simply put, when the velocity magnitude of the main object decreases, i want the spawnRate variable to increase. The goal is for each spawn is a linear rate for when objects spawn so logically the rate should decrease once the velocity decreases.
IEnumerator SpawnNet()
{
while(mainObj.GetComponent<Rigidbody2D>().velocity.magnitude > 1f)
{
//code//
yield return new WaitForSeconds(spawnRate);
}
}
Im trying to create a float property for the spawnRate:
private float _spawnRate;
public float spawnRate
{
get
{
return _spawnRate;
}
set
{
//alter the value based on: mainObj.GetComponent<Rigidbody2D>().velocity.magnitude//
//code//
}
}
In the set i should write the code that alters the value of the spawnRate variable based on the velocity of the main objects rigidbody. But im not sure how to do it. As shown, i need to alter the value of the spawnRate based on the mainObjects rigidbody velocity. If you could help me it would be much appreciated
Thanks!
Answer by Kossuranta · Apr 13, 2017 at 10:49 AM
Do know the max velocity of the mainObj? Depending how your game works you might be able to just simply do something like this:
yield return new WaitForSeconds(minSpawnRate + (1 - maxVelocityMagnitude / mainObj.GetComponent<Rigidbody2D>().velocity.magnitude));
At the max speed WaitForSeconds will wait for minSpawnRate and at any lower speeds it will wait for minSpawnRate + 0 to 1.
I have a $$anonymous$$atf.Clamp for the amount of boost you can give the mainObj. The speed is instantiated through AddForce, and the max velocity i can see is at around 47f but its not exact. What is $$anonymous$$SpawnRate and what is its value?
$$anonymous$$SpawnRate is the $$anonymous$$imum time for the WaitForSeconds, for your project it would be $$anonymous$$imum spawn rate that is used when the mainObj is moving at full speed. You should give it some value to your liking like you already did have spawnRate.
Actually I just noticed that my code is wrong as it should be
yield return new WaitForSeconds($$anonymous$$SpawnRate + (1 - mainObj.GetComponent<Rigidbody2D>().velocity.magnitude / maxVelocity$$anonymous$$agnitude));
As you don't have exact max velocity my solution might not be the best way to accomplish this, but if it is generally pretty close to that 47f you said then you could just make maxVelocity$$anonymous$$agnitude = 47f and roll with it.
Doesnt seem to do much, the rate is about the same regardless of the velocity of the boat (the objects are pretty crammed at the end since the main obj slows down and the rate is still the same)