There have been a few improvements made to the Lunar Lander 3D Unity game since my last post. The previous to-do list looked like this:

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.

The latest version of the code has four of those items mostly working.

Latest Lander Test

The particle system for the flames comes from the Unity Asset Store. It is a low-poly particle system resource I bought for a few dollars. I think that I really only need to write the code for a particle system once I need something custom. I suspect that the code is minimal anyhow since the Unity particle system handles most of the work.

The landing pad is a low-poly (low-polygon-count) cylinder with my own graphic added. The lights are the ends of small cylinders that have their light set to “unlit” or some similar setting that keeps the tiny bit of their sides or the edge between top and sides from being a different color from the top. The flashing is just a few lines of C# code to change the color every so often.

The rocket sound is another purchase, this time from a sound effects site called soundsnap. Although I bought a particle emitter that I could have written myself, I have no idea how to get or make sounds for the game. Purchasing is the only way to get what I need for the sound effects. The script for the lander adjusts the volume based on the thrust level, making it good for throttle feedback. And since the emitter changes the rocket flame emission from the thrust level, flying is pretty easy with a joystick.

image

Game Controller

Something that wasn’t on the original list was joystick support. It was easy to hook an Xbox One S Bluetooth game controller to the PC and use that. I use the right trigger for the throttle and the left stick for the lander tilt.

On a side note, I had a discussion with my dad about using the left stick for the pitch and roll control and the right trigger for the throttle. When I use a full size joystick and throttle control that I have for the desktop computer to fly with MS Flight Simulator X and other games, I hold the stick in my right hand and throttle with the left. And if I’m flying a helicopter in a game with that stick and throttle, I reverse the throttle so I’m pulling back/up on it to increase the throttle, just like the cyclic control in a real helicopter. And finally, in a real airplane like a small Cessna, which I have flow and have have a proper license to fly, I would always hold the yoke in my left hand with my right hand near or on the throttle. So there is no “right” way to pick which hand or finger uses which control stick, knob, or button. They all seem to feel right and completely natural to me.

Also on the original list was detecting a successful landing. This was hard. Really hard. To detect a landing, the code needed to get called when a collision is detected between one of the lander feet and the landing pad. I unfortunately call the lander feet “pads” making it a bit hard to tell what’s going on at times. I’ll need to fix that. Anyhow, instead of trying to deal with calls to OnCollisionEnter(), which seems obvious, I opted to write code for OnCollisionStay() which is called every frame and describes all of the colliding objects. The reason why OnCollisionEnter() doesn’t work is because there is no way to use OnCollisionExit() with it to keep track of collisions. What I ended up doing is naming my feet with a number and then when there is a collision in one of the child objects, like the collider objects inside of the feet, I move up the object tree and get the number for the foot. I then set a flag in an array for that foot so I can later figure out how many feet are touching. The tricky thing is that when OnCollisionExit() is called, it doesn’t seem to tell me which collision object is no longer colliding. I just clear the array and set a flag to an “I don’t know what’s touching” state to avoid incorrectly making sounds when a foot is already touching. It’s probably all wrong but it was the best I could do given no knowledge of what the collision detection functions are supposed to tell me. At least it is possible to tell with the array if all four feet are down!

I was also playing with some terrain related Unity features. I created a height map using some other programs and make sure that the terrain is flat along it’s entire edge (all four edges of a square). The terrain sits on a box of the same color making this look like the tile-in-space that I wanted. It’s nothing to brag – the terrain is boring at this point. But at least it works.

image    image

Indicators

Oh yes, and I added a HUD or GUI, whatever you want to call it, to show fuel, various speeds, angles, and other info. After the video was made is when I added a lean angle indicator. The 2D elements are super easy to work with in Unity, especially since I could create a game object to hold the entire right-side indicators and then place each individual part inside of that with relative positioning.