I forgot to make a blog post during the week we had no class so this one will cover two weeks. We needed something to increase playtime and replayability, add spice and just generally make the game more enjoyable. And we figured random levels could just be that. So here’s the stuff I did so far to get our current result. Keep in mind a lot of stuff isn’t in the playable build yet because it’s still in the testing fase.
Random generation
Step 1 Making the road.
First things first. You need a road from point a to point b. These points are still hard-coded but can be easily changed to random. The random road is a list of vectors made using (for now) one bezier point. That one (technically 2) dimensional road then gets an offset on both sides to form the mesh used for pathfinding.
Step 2 Making the grass.
The grass is basically the entire level minus the road. This sounds easy but unfortunately unity has no boolean operations for meshes. So I had to make one myself basically. Getting the vertices wasn’t hard. It’s just the vertices of the road plus the 4 corners of the map. But the indices that’s a whole different story.

(^Example triangulation, Gray = road, red = unwalkable)
First I had the road split in 4 quarters. Each side triangulating to a different corner. This unfortunately lead to crossing edges because the road didn’t always curve in the middle. So I had to make a function that using the random bezier point decided which vertices should connect to which corner. That involved some trial and error since I’m not a math wizard. But it works now. The reason we needed it was to show the base where it couldn’t go. This didn’t work right away though and I secretly blamed Tom’s pathfinding scripts. But I was to blame. His scripts where basically pure perfection. I just accidentally generated a tilted mesh instead of a flat one, which caused the issues. And I almost spend a week to find this mistake. Perfect anecdote to discourage someone from ever wanting to program.
Step 3 Placing nests
The enemy nests placement was pretty easy. It just chooses either north or south to decide which part of the road to be on. And the it just chooses a random z value in that area. The x value is not completely random since I didn’t want like 5 boxes right next to each other. And the numbers of nests is also set, for now.
Step 4 Placing props
At this point there are only two kind of props being spawned. Road fences and longer nest fences. The road fences are set on the side of the road using a probability which makes for like an average of 2 or 3 fences next to each other. Getting the right angle was the hard part. Since it basically needed the differential of the road curve. It’s using an estimated differential based on the previous and next vertex based on the random point on the road edge where it spawns. The second type of fences are placed around nests. It uses a bounding box to see if it fits in the level. And it chooses a random side around the nests to place on or two fences. It doesn’t check to see if it overlaps with the road yet. So I’ll probably set it inactive for now. The code for other props is still in development since I have to make sure no stuff overlaps without making that check to cpu intensive. It’s for Webgl after all.
Besides the random level stuff I also made the shotgun spreadshot and fixed a bug with the double wielding pistols being held in the same hand because of its parenting. And ad finem made a zoom for the controller.