Terrain Generation

So I’ve been working on a terrain generation system for the musicvideogame we are working on. This terrain generation is to allow the game to run for ever in it’s shuffle mode. To start with building it, I needed to have a collection of tiles that could fit together in any order and still look like a consistent road. To do this, I decided to have 5 different pieces to make up these tiles.

Road Bends


These pieces in order from left to right are the left turn, right turn, straight, outward S bend, and inward S bend. These pieces all needed to be oriented the same and have an anchor at the end of the road segment for the next piece. This lets me easily piece them together without any broken rotations. After the pieces were built(which took forever) I needed to create a script that would place them one after another in a general road structure. To do this I needed to have the script randomly pick a piece, and to place it at the same position as the next anchor at the end of the road. It then needed to reassign the next anchor and loop. This resulted with something like this.

Copy of terrain gen


But of course this wasn’t done, the road was always crossing itself and never deleted the earlier pieces. So, I decided to make the road have a limited length. To do this I made each piece be a part of a set array that cycled through. Each time a new piece was created the oldest piece was deleted to make room for it. I also created a system to limit the number of corners the road could take to prevent looping. I did this by only letting the road make a left or right turn every 10 pieces, and when it had the opportunity to create a corner, it was still only a 1 in 5 chance. This new system created a road that moved like this (sorry for the small size). 

terrain gen mkII


So this was now creating a road that would snake around the world all crazy like, but the car wouldn’t follow it. So I needed to make a collection of animations that would move the car along with the road. With these animations, it was extremely important to make them pass through the anchors, otherwise the car would drift away from the road over time. Also with the new animations, I was able to get the code running every time an animation ended instead of in update, meaning that the road could move along with the road. Also to have a reference to the animations names, rather than creating scripts for the pieces of road to hold the names, I added them as tags to the object, letting me reference the object’s tag as the string for which animation to play. This new update to the system let me have the car drive along with the road, meaning in some form, it was ‘done’. But, an issue with unity is that the further away from the origin you get, the less accurate the positions get, which cause shadow to flicker as the road got further and further from the origin. This was a big problem for a game that is focussing on visual pleasing scenes. To fix this, I needed to alter how the road would decide whether to use a left or right turn. To do this, I added a variable that would track the way that the road was heading (cardinal directions based off Z+ being north) and then depending on the direction it was traveling and the position on the x or z axis, it would pick a corner that would lead it back to the origin. But the problem with this system is that id would eventually just create a forever looping circle that would only ever pick left or right and never a mix of the two. So I decided to add a function that would let it randomly pick a corner or straight if the road was within 1000 units of the origin. Which resulted in this wonderful road.

terrain gen Fixed
After this I needed to tweak the anchors and the animations to not have holes in the road or big jutting out walls. But that is all I have done to the terrain generation system so far. I still want to make the road pick a type of terrain based on some sort of curve so that we can have more than the cliff face we have right now, but I need to work on creating the tiles for those first before I can create a system to spawn them as part of the road. But that task is for later, as I also need this system to spawn decorations such as beach furniture and boats. 


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s