on create with code challenge 2 how can I code the 2 bonus bits. Make the spawn interval a random value between 3 seconds and 5 seconds and Only allow the player to spawn a new dog after a certain amount of time has passed
This is my script.
public class SpawnManagerX : MonoBehaviour
{
public GameObject[ ] ballPrefabs;
// Start is called before the first frame update
void Start()
{
InvokeRepeating(“SpawnRandomBall”, startDelay, spawnInterval);
}
// Spawn random ball at random x position at top of play area
void SpawnRandomBall ()
{
// Generate random ball index and random spawn position
int ballIndex = Random.Range(0, ballPrefabs.Length);
Vector3 spawnPos = new Vector3(Random.Range(spawnLimitXLeft, spawnLimitXRight), spawnPosY, 0);
// instantiate ball at random spawn location
Instantiate(ballPrefabs[ballIndex], spawnPos, ballPrefabs[ballIndex].transform.rotation);
}
You generate a random timer with the same Random.Range you’re already using.
float randomTimer = Random.Range(3f, 5f); //random float between 3 and 5
Then you can just count it down by subtracting Time.deltaTime from it each update. When it is equal or less than 0f then you act.
If you need to make sure a certain amount of time has passed, after setting randomTimer to the random value, you can check if it is >= that certain amount of time which needs to have passed. If false, then set randomTimer to whatever that certain amount of time which needs to have passed is.
Hello. While performing this task, I encountered a similar problem. The task hint states -" Hint - Set the spawnInterval value to a new random number between 3 and 5 seconds in the SpawnRandomBall method"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnManagerX : MonoBehaviour
{
public GameObject[] ballPrefabs;
private float spawnLimitXLeft = -22;
private float spawnLimitXRight = 7;
private float spawnPosY = 30;
private float startDelay = 2.0f;
//private float spawnInterval;
private float targetTime;
// Start is called before the first frame update
void Start()
{
//InvokeRepeating("SpawnRandomBall", startDelay, spawnInterval);
targetTime = 2;
}
private void Update()
{
targetTime -= Time.deltaTime;
if (targetTime <= 0)
{
SpawnRandomBall();
targetTime = Random.Range(3, 6);
//Debug.Log(targetTime);
}
Debug.Log(targetTime);
}
// Spawn random ball at random x position at top of play area
void SpawnRandomBall()
{
int ballIndex = Random.Range(0, ballPrefabs.Length);
// Generate random ball index and random spawn position
Vector3 spawnPos = new Vector3(Random.Range(spawnLimitXLeft, spawnLimitXRight), spawnPosY, 0);
// instantiate ball at random spawn location
Instantiate(ballPrefabs[ballIndex], spawnPos, ballPrefabs[0].transform.rotation);
//Debug.Log(targetTime);
//targetTime = 0;
}
}
When do I want a new random number? → when the SpawnRandomBall function is called.
When do I want to execute the SpawnRandomBall again? → When I have a new random number generated.
Does my Start function get updated? → No, it only triggers the start of the game and calls the functions I type in there.
I fixed it like this: Get rid of the repeat invoke in the start function and make it just an invoke to trigger the function to spawn a ball.
Invoke(“SpawnRandomBall”, startDelay).
Then in the SpawnRandomBall you want to set a random number between 3 and 5 every time this function is called.
spawnInterval = Random.Range(fastSpawn, lateSpawn);
Once you know the next interval you just call the same function again, but with the random interval number.
Invoke(“SpawnRandomBall”, spawnInterval);
Every time the SpawnRandomBall is called it will generate a new number and call the function again after that many seconds. Not sure if this is THE solution, but it works very neat.
Hi all,
But…
Isn’t i a problem to recurse again and again…
For a game which dure for long and for witch the time repeating is short…
Will it not overlaod a recursive heap?
// Start is called before the first frame update
void Start()
{
int spawnInter = Random.Range(1, 25);
InvokeRepeating(“SpawnRandomBall”, startDelay, spawnInter);
}
// Spawn random ball at random x position at top of play area
void SpawnRandomBall ()
{
// Generate random ball index and random spawn position
Vector3 spawnPos = new Vector3(Random.Range(spawnLimitXLeft, spawnLimitXRight), spawnPosY, 0);
// instantiate ball at random spawn location
int ballIndex = Random.Range(0, ballPrefabs.Length);
Instantiate(ballPrefabs[ballIndex], spawnPos, ballPrefabs[ballIndex].transform.rotation);