Writing a Unity Game

May 27th, 2017

In the last few years, I would visit the B&N bookstore and look at the game programming books. I once had aspirations to build a highly detailed railroading game with more realistic train and car movements than any retail game of that sort. I even worked out some of the logic for managing trains, cars (boxcars, not Toyotas), track, and destinations, while having clever path-finding mechanisms, etc. But my more recent inclinations have been to just get anything working in a 3D rendered world.

Image result for rectorsquid train

Image result for rectorsquid railroad

Train Game Experiments

This is where Unity comes in…

I’ve played more than a few games programmed using Unity. Most recently it was Human Fall Flat, a game that is hilarious fun for two people. I finally broke down a few days ago and installed the Unity tools.

Image result for human fall flatI was surprised that Unity is not a framework like a C++ wrapper of DirectX or a cross-platform toolkit like QT. It is actually an IDE driven construction tool where you can create a fully runnable program without writing a single line of code. At least I think so since I have not tried to build my project yet and run it as an executable. But you can defined enough of the environment and objects to have something interesting happen while only dragging-and-dropping objects into the editor and then clicking on properties to make them do basic stuff. No code is needed to make a ball roll down a hill.

image

Unity IDE

I worked through some of the Unity ‘Tanks’ game tutorial that is shown in something like 8 parts on YouTube and with the project files all available with Unity when it is installed. After tiring of it and making a mistake that I didn’t feel like fixing, I decided to try something of my own. This is where 3D Lunar Lander comes in. Read on…

Lunar Lander was a stand-up arcade game in the 1980’s that used a vector graphics display and some custom controls. The game displays a small lunar lander vehicle that is clearly falling from orbit over a rugged non-moon-like landscape. With limited fuel, the object is to land on a landing pad to score points. Do that and you get a new landscape and another chance to land on a different set of pads. All of this is shown like a side-view platform game drawn in thin lines. Eventually, there is no fuel left and the game is over.

image

Atari Lunar Lander

The first challenge in using Unity was to get something moving. I opted to put a cube into my scene and a flat surface for it to land on. This was easy and hitting the play button in the Unity IDE resulted in the cube falling onto the surface. I Added a script to the cube so I could add inputs and some values for thrust, pitch, and roll. The Thrust was easy to handle by just applying a force vector to the RigidBody part of the cube. A RigidBody make a game object respond to stuff in the game world. I think that buildings and rocks don’t need a RigidBody. Hitting the play button and then hitting the up arrow resulted in the cube rising above the surface. Voila, a game is born!

First Unity Experiment

The next challenge, that I spent hours on and is the most interesting part of all of this, is the lander controls. I wanted to pitch the lander when the A or D keys are pressed (forward and backward respectively) and roll the lander when the W and S keys are pressed. This doesn’t work like I wanted because the built-in rotation math rotates the pitch and then the roll. But after the pitch is changed, the roll axis is tilted and the roll doesn’t wok as expected. After a good nights sleep, the idea came to me to think of this differently. Think of it like trying to mimic the movement of a joystick. A joystick may have pitch and roll numbers coming out of the electronics but superficially, the stick is tilted in a direction and nothing more. The clever idea was to create a game object and stick the lander in it. When the keys are pressed, the pitch and roll values are used to come up with an angle to lean and an amount to lean in that direction. I simply rotate the game object to the desired angle and rotate the lander inside of it the opposite direction. the end result is that the roll axis is rotated to be along the direction of lean and applying just a rotation of just the one axis on the desired amount works perfectly. The lander never seems to rotate but the axis of the lean rotates.

First Unity Experiment

Of course there were other challenges to this. If the code does interpolation (LERPing) then the changes to the lean direction and lean angle can screw things up and cause jumps in the movement. If the amount of lean is changing to zero, I simply leave the direction of the lean at it’s previous value. That way, leaning to a straight up position from the leaned backwards position doesn’t cause a glitch when the new lean angle becomes zero.

The next steps in any order will be to:

Add a particle system to draw rocket flames.
Add a landing pad and detect a successful landing.
Add an explode-able version of the lander and blow it up if the lander lands off the pad or lands too hard or too leaned.
Sounds.
Terrain.
A game management system to load multiple levels and to provide a start screen and stuff of that sort.

Speaking of terrain, I have not figured out a good way to make terrain. I want to go with a low-polygon style for the terrain in case I decide to build this for mobile platforms. Right now, it’s just running on Windows. I tried to use the built-in Unity terrain system and it’s ok. The problem is that I have no idea how to add sides to a small section of terrain to make it look like a “tile.” In the image below, you can see that there are sides to the terrain (someone else’s) but that’s not that easy to achieve, so far.

image

Terrain ‘Tile’

image

Low Poly Terrain ‘Tile’

I think I’ll worry about the tile terrain thing after I get the landing pad system working. In fact, I can worry about the terrain tile thing last if I want to. But I would like to solve it so the game starts to look like I expect it to look when finished.

It will be an interesting challenge to finish the game even if it has just one level and no start screen or level selection.

Wherever this takes me, it’s been a few few nights getting the lander to fly!

Leave a Reply

You must be logged in to post a comment.