A step-by-step log of how I made a basic Unity/C# game: “Slash’n’crack”!
This article is also available on Medium.
Earlier this week, I started a new series of blog posts to share how I’m making a simple Unity/C# game: Slash’n’crack! In these gamedev logs, I share some tips and tricks on how I implement the various features of the game and I show small demos of the project in its current state.
Also, for this dev challenge, I have a tight time constraint: I only have 8 hours to make the entire game, from start to finish!
So today, let’s continue developing Slash’n’crack and work on our game manager to pop asteroids endlessly and with some random transforms!
Hour #2: Popping obstacles indefinitely!
Features & demo
In the first episode, I prepared the basics of the game and I placed the asteroids manually in the scene… this is nice, but of course we want these asteroids to be spawned automatically so that we can have an infinite arcade game!
To handle this auto-generation, I need to create a game manager in my scene to regularly instantiate new objects. Note that I could re-use object pooling to make the game more efficient, but I first want to implement all the core features.
Therefore, for now, I’ll simply add and destroy my asteroid game objects.
I’ll make sure that the asteroids are spawned at random positions just above the top edge of the screen: they’ll span all across the horizontal axis and get some additional vertical offset to appear in the screen at different moments. I’ll also add some randomisation of the scale and the rotation.
Finally, I have to call my spawning function regularly so that it keeps on “re-populating” my scene.
In the end, this gives me a simple autospawn system that periodically creates new asteroids (which will auto-remove themselves when they exit the screen, like I coded before):
I’ve also taken this opportunity to gradually speed up the game, as well as shorten the spawning periodicity. This is necessary for the “arcade” part of Slash’n’crack: since the game doesn’t end until the player loses, it has to become harder and harder for it to be interesting!
A few details, tips & tricks
My random positioning process is super-simple for now – perhaps a bit too much, even 😉
I just take a random X position between some bounds that match my scene dimensions, and set the Y position to be above the top bound of my camera frame, again with a bit of random offset so that not all asteroids appear at the same time on-screen.
This algorithm is neat because it is super quick to code, but it can quickly make repetitive patterns, or clusters of asteroids; so if I wanted to make a more interesting game, I’d probably need to refine this instantiation distribution to get less colliding positions and more various spawning points.
The scale is a global resizing of the object to get some diversity in my asteroids without having to create dozens of meshes; and finally, the cherry on top: the random rotation! By choosing 3 random orientations for all 3 axes of my asteroids, I’ll easily get a lot of visual diversity for the player 🙂
This will obviously give better results when I add an actual asteroid model to my game, instead of the primitive cube – but at least it’s in place, now!
About speeding up the game, I’ve decided on the two following things:
- the game speed is a linear function of the current time (so the asteroids – and the stars – will move faster and faster as you progress through the game)
- I’ve also defined some “time thresholds” (at 10 and 60 seconds) at which my asteroid spawning calls period gets reduced a bit
These two features make sure that as you keep on playing, the obstacles will come at you quicker and eventually overflow the screen.
Although I have figured out most of what I want to do for this game (in terms of features, gameplay, art style, etc.), I might come up with unexpected ideas along the way… and, of course, I’d be really happy if you’d participate, too, so feel free to leave comments with cool ideas for Slash’n’crack!
I hope you like this new series of Gamedev Logs, and see you in a few days for the next one 🙂