top of page
Writer's pictureKane Adams

Object spawning

Updated: Dec 6, 2022

After getting the mainland space to generate, the next important part of a generation is getting objects to be able to spawn on that land, this meant that the object was spawning on top of the land and is important for a lot of different jobs, with them consisting of spawning in environmental objects such as trees (the main focus for now), to random pickups to even NPCs within the world.


The first obstacle faced with getting the generating to work is learning how to generate objects onto an uneven terrain. The way I have focused on this is by using a raycast from above the current chunk and if there was a collider hit that contains the terrain layer, then a cube was instantiated at the point spawned with an angle so that the object isn't partly spawned under the ground.

This took some work as at first I was setting the raycast to only check the central chunk and move the spawned trees to the correct chunk, which meant some cubes were floating and others were below the ground due to there being a hill or water in the central chunk. Despite this bug, through playing around I enjoyed using the momentum from running off the hills and trying to land on the cubes (more on this later).


Cubes flying above the hills due to instantiating on central chunk then moving them to assigned chunk
Floating cube bug

The next issue faced was the cubes were not spawning on all chunks, this bug became more apparent later but was caused by changing the Z-position of cubes to the X-position value, which would result in some groups of cubes being spawned away from the terrain.


Cubes would spawn away from correct terrain due to changing Z-position to X-position's value
Cube placement bug

Once those bugs were fixed, I wanted to make sure the cubes spawned within a set region so that there weren't trees within the lake and on top of the hills, so I experimented to work out the y-values where it changed to the different sections which we 5.64 and 12. To ensure some space between the regions, I checked whether the raycast point was between 6 and 11. To make sure enough trees are being spawned I added a for loop that counted to a random value between 100 and 250.



Cubes being spawned within set regions for the current chunk
Spawning colourful cubes


Creating trees

Now that I had object spawning working, it was time to get trees to spawn instead of cubes. Due to not being an artist, I made trees by scaling the y up and adding bigger cubes to the top, then adding brown material for the trunk and green for the leaves.


A tree prefab consisting of 3 different size cubes and 2 materials
A programmer tree

When I added trees, I altered the code for the tree rotation so that all trees have the leaves on top but also allowed them to still be angled by 5° as to create some variants.


Spawning of trees within a terrain chunk
Tree spawning

After the addition of trees, I decided to also add rocks to create more variety of objects within the game.


A rock prefab consisting of multiple cubes and a grey material applied
A programmer rock

For the rocks, I re-implemented the angle to be based on the hill angle.


Both trees and rocks can be spawned within a terrain chunks
Rock and tree spawning

After asking for feedback from coursemates and my lecturer, there wasn't much comments on the work itself, more on the point of how much it resembles Minecraft in look. I will take this as I am on the right path with my work and should I have time later in development, I will look into getting tree generation with the use of L-systems working.


Now that I got trees to randomly generate within a scene, I will be looking into getting them to spawn using a procedural generation algorithm. The reason for this change is one issue that is met with using random generation and raycasts is that objects can spawn close together to the point where they merge as one. After talking to a coursemate they suggested looking at Poisson distribution, a type of generation that makes sure objects that are to be generated are a minimum distance away from each other. I plan to have a look at this in the coming week.





Game idea

As mentioned previously, I enjoyed using the momentum from running up hills to help traverse over the objects within the scene and this has helped me come up with a potential idea to have pickups spawn randomly on top of trees and hills and have the objective to collect as many as possible.


Alongside that idea, I also had thoughts of potentially adding some basic AI that follows the player to attack them with the navigation controlled by a NavMesh that is created after the terrain is generated. However, before I work on any of these ideas, I will focus on getting the main areas of the project working first.

12 views0 comments

Recent Posts

See All

Comments


bottom of page