How to organize an Infinite runner level blocks
Hi there!
I'm working on an Infinite Runner game. I have created many level blocks(sections) with different obstacles and pick-ups for the player. They are positioned and placed manually, in such a way, to give the player a challenge. I thought that making every block a prefab and placing every obstacle and coin as a child of that prefab is going to be easier for me..because the player is going to run and these block prefabs are going to be randomly spawned in front of him and then destroyed after the player gets past them. It worked well.
Then I wanted to make a change on my coins. So naturally I took the coin prefab, made the changes, and applied them hoping that the change is going to take effect on all my blocks... Well..it didn't. Then I found out that nested prefabs don't work in Unity, so now I have to reorganize everything again?
How do I create organized level blocks, and then make them randomly spawn in front of the player? Is there any other option besides prefabs? Don't tell me I have to use a script for every block?
Think about Banana Kong, Temple Run, Subway Surfers. The levels seem random, but it's an organized randomness with certain portions of the level repeating themselves. That's what I'm trying to do, and I don't know how to organize it...
Thank you very much. I hope you guys can help me.
you shouldnt destroy and spawn the objects constantly since its pretty heavy on the cpu and can course laag ins$$anonymous$$d you could spawn all the objects in the start and have a loading screen and then reuse the objects also known as object pooling heres a link https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling
Answer by OncaLupe · Nov 23, 2015 at 06:52 PM
Instead of putting the coins/pickups directly onto the prefabs, you can instead have empty game objects there as markers. When the section is spawned, you then spawn in the items at those locations.
This also gives you the ability to adjust how many coins or pickups are spawned on the fly. This way one section can have none/few/many coins/pickups without needing different prefabs for them. For instance, if you had just given the player a speed pickup, then spawn in a section with a marker for a pickup, you can decide if you want to give another so soon, give a different pickup, or skip it.
The best way (IMO) to do this is to have a simple script on each section that holds arrays with all markers in them (one for coins, one for pickups, etc). The spawner script can then use this to easily access the marker locations without needing to use GameObject.Find or whatever.
Also, it might be a good idea to look into object pooling since you're doing a lot of creation/destruction of objects. Instead of creating each section/coin/pickup and destroying them once past, you create a pool of each (guessing how many you may need max at any one time and adding a little buffer on top) and set them inactive. When needed you take one and position it then set it active.
http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling
Thank you so much for taking your time for this great reply!
That was a great insight. I will follow your advice :)
One more question. How should I organize these markers though? Let's say these markers are a prefab that has a script with public bools (checkboxes) for the type of object that the marker should spawn. I then put these markers into position, under my section object. I then save the section object as a prefab so I can use it whenever.
Now I want to make a change to my marker prefab (add another checkbox for example). That change won't affect any of the sections, because all the prefab connections are lost.
So how do I proceed? Should I create an empty game object with a single script that contains information about every single marker's position and everything? Is there any easier way?
Thank you.
I'm unable to help you regarding the marker position thing, but I was going to say, for the checkboxes - why not use an enum? Just have something like this:
public enum ItemsPossibleToSpawn { None, Health, Time, NitroBoost }
public ItemsPossibleToSpawn whatItemToSpawn = ItemsPossibleToSpawn.None;
That way, when you want to spawn an item there, you just run:
void SpawnAnItemHere() {
switch(whatItemToSpawn) {
case ItemPossibleToSpawn.Health:
// Code to spawn the prefab here //
break;
// Repeat the above "case" statement until all items in the enum have been done excluding the "None" item. //
}
}
Enums are way easier to track than having 20 odd checkboxes and having if else loops that are massively complicated.
Scripts are a separate asset that the objects point to. If you change a script then all objects use the updated script. You'd only need to change the markers if you wanted to change what would spawn there. Probably wouldn't even need to make the markers a prefab, just create a new empty game object and drop the script on it.
Personally though, I wouldn't even put a script on the marker object, they'd just be an empty object. As mentioned in my answer, I'd have a script in the root of the section prefab with arrays for Coins and Pickups. Drag the markers to the proper array for what you want to spawn there. If you have multiple pickups, the code would decide what to spawn when the time comes.
If you did want to be able to pick what would show up ahead of time, then you can put simple scripts on the markers and the arrays would point directly to those scripts. Those scripts could then either have bools as you mentioned, or an enum like $$anonymous$$Coburn said. Enum would probably be simpler to code, but bools would give you the possibility to set multiple items to choose from if you wanted.
This is turning into a discussion. Unity Answers is for answers. :)
If you want to discuss approaches on some features or functions, visit the forums here.