Getting our background off the ground.
|
Hey! This week, I’ll be talking about an asset I made a while ago, a modular scenery generator / spawner. This’ll be a coding post themed around graphics but not really. The game I’m working on with my group, “Burn Witch, Burn!”, is a top down shooter with a scrolling background. While making the backgrounds for the game, I deliberately wanted to avoid making the objects in them, such as the trees, rocks, and houses, part of the background. I wanted to avoid it because:
Due to the reasons outlined above, I made all the background in the game without any objects and instead opted to create a script that spawns objects randomly and can be reused for all the different objects and backgrounds we had. ![]() The script itself was written to be modular, and receives most of it’s values from the editor in Unity, meaning it can accommodate a number of different configurations and purposes without altering the base script. The script works by receives the possible spawn values from the editor in unity in the form of a Y axis value for the spawn height, used so that objects can be set to spawn at a height that is off screen, and two X axis values, determining the minimum and maximum values that the object can spawn in on the X axis. The script then takes these three values and uses them to create a spawn point for the scenery object. It sets the spawn height as the y value which is usually set to be a height that is on the top of the background and off screen. It sets the x value of the spawn point by randomly generating a number between the minimum and maximum value that was input earlier, to make objects spawn randomly and not in a straight line. For the timing of when to spawn an object, the script is also fed a minimum spawn time value and maximum spawn time value. Both of these values are used to control how often an object spawns. Different objects, like trees, require a low spawn time is to recreate something like a dense forest, while a higher spawn time is required in something like a village because you generally don’t want overlapping houses and such. After receiving a minimum and maximum spawn time, the script randomly generates a value between them and then uses an IEnumerator function with the line of code “yield return new WaitForSeconds” along with the value in order to wait for that amount of time before spawning another object. This is repeated infinitely.
To decide which object is spawned the script also receives an array of game objects from the unity editor, and randomly picks one of them to spawn, making it possible to have the same script in different levels spawning different objects with different timings. The last thing I will cover in this post is how I set up the scenery objects that the scenery spawner spawns. Each scenery object is a sprite that has a mover script that moves it down with the same speed as the background so it looks like it’s on it, each object also has a collider so that it can be checked when it exits the boundary box for the game, and then be destroyed. There is probably more stuff that can be discussed in regards to the scenery spawning but I feel I’ve explained the important stuff. For now, adieu! |



