- Home /
Spawning System
So I'm making a game where you are a grenade trying to cross a road without getting hit by any cars. Kinda like crossy road. Here is how it looks like: /;l
So I cant think of a good system that would spawn cars, like how would I manage the speed? By animation or add force? How would I say where to spawn the cars and when. When to destroy the cars, when to spawn the road etc... Really been struggling with this...
Thanks in advance :D
Answer by SiniKettu_ · Jun 19, 2020 at 08:27 PM
Cars speed
I would recommand you to use transform.position += Vector3.forward*speed
. If you want to have random speeds, you can do something like this :
//script on every car
float thisCarSpeed;
float defaultSpeed;
float randomValue;
void Start()
{
thisCarSpeed = Random.Range(defaultSpeed-randomValue, defaultSpeed+randomValue)
}
void Update()
{
transform.position += Vector3.forward*thisCarSpeed;
}
So each car has its own random speed in range.
Spawning system
I would use a coroutine like this :
//global script which manage car speawning from car's prefab.
public GameObject[] carPrefabs; //all your car prefab (because it seems like you have several cars prefab)
public float spawningRate; //delay between each car spawn
public Tranform[] spawningPoints; //create some points on each road. Then they will get forward with the camera. I assume that your camera never go back.
public float spawningOffset;
void Start()
{
StartCoroutine(Spawning());
}
void Update() //moves the spawn points along the road
{
//keep the 'foreach' which works !!! the first works if the road is along the x axis and the second works if the road is along the z axis. I dunno in wich orientation you set up the road.
//FIRST
foreach(Tranform point in spawningPoints)
{
point.position = new Vector3(Camera.main.tranform.position.x+spawningOffset, 0, 0);
}
// THE SECOND
foreach(Tranform point in spawningPoints)
{
point.position = new Vector3(0, 0,Camera.main.tranform.position.z+spawningOffset);
}
}
IEnumerator Spawing ()
{
while(true) //makes the while repeating itself forever.
{
Transform randomSpawningPoint = spawningPoint[0,
spawningPoint.Length];
Vector3 spawingPosition = randomSpawningPoint.position;
Instantiate(carPrefabs[Random.Range(0, carPrefabs.Length), spawningPosition, Quaternion.identity] //the Quaternion.identity can be wrong, it depends of how do you created your game (in wich orientation your road is ?)
//make sure the prefab have the script i also wrote about (cars speed)
yield return new WaitForSeconds(spawningRate);
yield return new WaitForEndOfFrame(); //this one avoids an infinite frame (= game crash) if spawningRate = 0)
}
}
I hope this is useful Tell me if you get an error with it, i didn't test it
If you want to stop the spawns you can create a boolean in the global variables section and check if this boolean is true at the beging of the while(true)
, and if this bool is actually true then the coroutine instantiate a car.
So when you uncheck this bool from the inspector or from another script, cars won't spawn, and when you recheck the bool, the cars spawn again.
Hey! Thank you for replying :D this looks so scary xD I will experiment and let you know what happens!
Hey I am pretty confused with what this does ' IEnumerator Spawing ()'
An "IEnumerator" is just, in a nutshell, like a function (which starts with the 'void' keyword) but it's allow to wait (WaitForSeconds(x), WaitForSecondsRealtime(x), WaitForEndOfFrame(), WaitUntil(), WaitWhile()...)
(We use this because we cannot use these waiting methods in a "normal" 'void')
This is called a coroutine.
Answer by mahmouds · Jun 21, 2020 at 05:23 PM
@SiniKettu_ I am getting errors with: spawningPoint[0, spawningPoint.Length];
When I remove the zero I don't get any errors. Thanks!
When I remove the zero I get no errors but it doesn't spawn anything...
Actually thats my bad I do get this error when i remove the zero: "Indexoutofrangeexception: index was outiside the bounds of the array. " I does let me play the game though it just gives this error and doesnt spawn anything
Your answer
![](https://koobas.hobune.stream/wayback/20220613001337im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Enabeling components 1 Answer
Unity 5.2 camera not recognised 1 Answer
Make unity camera appear all screen width 1 Answer
Switching camera to follow object 0 Answers
How to access camera preview frames with MediaCapture Class in Unity? 0 Answers