I’m trying to make a randomly-generated bridge that explodes when a timer counts down and reaches zero.
The bridge is made up of instances of a platform prefab, and is generated with the following code:
public void RoundGen() //Function for generating the level
{
// Set the detonation time to a random integer between 1 and the round number
Time = Mathf.Round(Random.Range(1f, GameController.round) * 10) / 10;
float Target = Mathf.Round(Random.Range(0.5f, 5 * Mathf.Sqrt(GameController.round))*10) / 10; //Set the "correct answer" (Target speed) to a number rounded to the 10ths place
if (GameController.round < 21) //If the level is below 20
{
Target = Mathf.Round(Target); //Round the target to the nearest whole number
}
while (Target * Time != Mathf.Round(Target * Time)) //While the product of target and time variables is not whole
{
Time = Mathf.Round(Random.Range(1f, GameController.round) * 10) / 10;
Target = Mathf.Round(Random.Range(1 / GameController.round, 5 * Mathf.Sqrt(GameController.round)) * 10) / 10;
} // Reassign the two variables until the product is whole
detonationTimer.text = Time.ToString("f"); //Update the detonation timer
Distance = Mathf.RoundToInt(Target * Time); // Set the distance based on the detonation time and target speed
BlockBehavior.Blocks = GameObject.FindGameObjectsWithTag("Platform");
for (int i = 0; i < Distance; i++)
{ // Instantiate the amount of floor blocks needed to build the bridge (Distance) meters long
Instantiate(FloorBlocks, (offset + (Vector3.right * i)), Quaternion.identity);
}
}
Countdown script and coroutine:
void BombCountdown()
{
StartCoroutine(BombCountdownCoroutine());
}
IEnumerator BombCountdownCoroutine()
{
float BombTimer = RoundGenerator.Time;
while (BombTimer > 0 && !PlayerController.fallOver)
{
Display.text = (Mathf.Round(BombTimer * 100) / 100).ToString("f");
BombTimer -= Time.deltaTime;
yield return new WaitForEndOfFrame();
}
ExplodeTrigger = true;
Debug.Log("Countdown finished.");
}
Explosion script and coroutine:
void Update()
{
if (Countdown.ExplodeTrigger) // When the countdown is finished
{
Piece1.GetComponent<Rigidbody>().isKinematic = false; // Remove all constraints
Piece2.GetComponent<Rigidbody>().isKinematic = false;
Piece1.GetComponent<Rigidbody>().useGravity = true;
Piece2.GetComponent<Rigidbody>().useGravity = true;
Piece1.GetComponent<Rigidbody>().AddForce(new Vector3(Random.Range(-15f, 15f), Random.Range(5f, 10f), Random.Range(-15f, 15f)), ForceMode.VelocityChange); // Throw the pieces in random directions
Piece2.GetComponent<Rigidbody>().AddForce(new Vector3(Random.Range(-15f, 15f), Random.Range(5f, 10f), Random.Range(-15f, 15f)), ForceMode.VelocityChange);
StartCoroutine(Explosion()); // Start the explosion coroutine
Countdown.ExplodeTrigger = false;
}
}
IEnumerator Explosion()
{
float TempTimer = 1f;
while (TempTimer > 0)
{
TempTimer -= Time.deltaTime;
yield return new WaitForEndOfFrame();
}
foreach (GameObject blk in Blocks)
{
Destroy(blk);
}
Debug.Log("Blocks destroyed.");
yield return new WaitForEndOfFrame();
}
Piece1 and Piece2 are children objects of the prefab.
When I try this code,
- only the first block is blown up.
- the clone isn’t destroyed.
- The code only works once i.e. once the next level generates, the code no longer works.