Dream Build Play – Mines and Slot Machine

I’m now wrapping up the implementation of the gameplay elements in the Turn Back the Clocks 4 game for Dream Build Play.  The two features that I had envisioned adding were mines and a slot machine.

The mines just act as a hazard, which destroy your ball on contact.  Programmatically, this was relatively simple to implement.  The mine is just a GameObject with a sphere collider.  When a ball collides with it, the ball is destroyed.  I do a check in the ball’s on collision method to verify that the object that it has collided with has a Mine component (in other words, has been assigned the Mine script).  I have found that this is a better way to determine an object’s type than using the Unity tagging system.  I don’t know if there are any performance differences, but tagging seems really redundant when the object already has a type from the script that has been assigned to it.  Plus, a GameObject can have multiple scripts assigned, but it can only have one tag.

From doing game jams over the five years, I have really learned a lot tricks.  Others may call this “tools in my toolbox”.  I’ve tried to use an array of the tricks that I’ve learned in this game.  One of those is the Blender Cell Fracture add-on.  This Blender feature will take an object, and break it into multiple pieces based on the parameters that you set.  Then you can apply Blender physics to the broken pieces, which will make the object explode over a period of time, which is saved as an animation.  So I took my ball model (another good reason to use a modeled ball in Blender instead of the default Unity 3D sphere) and applied the cell fracture.  I saved this now model and animation as a ball explosion.  This way, when the ball hits the mine, I can destroy the original ball, and instantiate the ball explosion.  This makes things a lot simpler, keeping the ball and it’s logic separate from the explosion.  The only catch is that you’ve got to pass the ball’s material to the ball explosion, otherwise the explosion will just look like a gray ball breaking apart.  I created a method in the ball explosion, which takes the material of the ball as a parameter.  The other issue is that the ball explosion actually has two materials that are generated by the Cell Fracture plugin.  I know I’ve tried setting the array of materials on an object before, but it never seemed to work.  After some research, I found that GetComponent<Renderer>().materials actually returns a copy of the materials on the object.  So to assign new materials, you have to set the Material array back to the materials property and it will work.  Now when a ball explodes, it is the same color (blue, red, or yellow) of the ball that hit the mine.

The other feature that I wanted to have in the game was a slot machine.  I got this idea from watching Pachinko videos online.  In Pachinko, if a ball goes into the target slot, a slot machine will appear on the video screen.  If all three numbers match, then you get more balls.  I liked the concept of getting more balls from the slot machine.  However, I didn’t like the concept of just matching three numbers.  It just felt too random, and the numbers really didn’t have any significance.  Therefore, I decided to have the prizes on the slot reel, which were the balls which could be won.  Then I had the idea of having a multiplier reel, which increased the number of balls that you would win.  The only thing was, I wasn’t sure how this would work with three reels.  Finally, I just decided to have only two reels, which made things simpler and much easier to under stand.  The first reel is the ball type (blue, red, or yellow) and the second reel is the multipler (x1, x2, or x5).  I wanted the better prizes to be more difficult to win, so each reel has five of the low prizes (blue and x1), two of the middle prizes (red and x2), and only one of the best prizes (yellow and x5).  That makes eight total items on each reel.  The chance of getting the best prize on both reels (yellow ball x5) is 1/8 times 1/8, or a one in sixty-four chance.  That seemed about right to me.  I wanted the best prize to be rare, but not so rare that it will never occur.

I created a SlotMachine script, which controls both of the reels.  I decided against have scripts for each specific reel, although that would be helpful if I had a unknown number of reels.  The SlotMachine script has float variables which store the rotation of each reel.  Each reel is set to a random position, and rotates for a random period of time.  It took me a little while to determine which prize was selected when the reel stopped.  I converted the rotation to an index by dividing by 45 (360 degrees divided by 8 prizes) and rounding to the nearest integer (modulo 8 to take care of the case when the rotation is greater than 315 and it rounds up).  I also have to a 90 to the rotation to get the correct index.  This took me a while to track down, and I can only explain it as being an issue when moving from the Z up coordinate system (in Blender) to the Y up coordinate system (in Unity).  Frequently, when a Blender model is imported into Unity, -90 degrees on the X rotation is automatically applied in the transformation.  However, when you’re handling the rotation yourself, you have to add that 90 degree back to get the correct index.  Texture mapping the reels was not too difficult, but I had to make sure that each of the panels in the UV map lined up exactly over the appropriate square in the texture that I created in Gimp.  I made the reel texture 2048×2048, and each of the panels are 512×512.  I wrote down the sequence of prizes in a text editor, and placed them appropriately on the reel.

I also created a slot entry object, which is just a simple little container mesh with three sides.  The container has a mesh collider on the outside, and a trigger box collider on the inside.  If a ball touches the trigger, then a new SlotMachine object is instantiated.  After both reels on the slot machine stop, then the appropriate number of balls are added to the player’s ball queue.  Another problem arose when a new ball touches the slot trigger, when a SlotMachine is already active, which causes multiple SlotMachines to appear on the screen at the same time.  To fix this, I just simply check to see if a SlotMachine already exists using GameObject.FindComponentOfType<SlotMachine>() before creating a new one.  This is a little unfair, because the player will lose a ball when they should get another turn at the slot machine, so that is something that could be improved by adding something like a lid on the slot entry that closes when a SlotMachine is active.

I think I’ve finished with all of the gameplay elements on this game for now, and I will focus on adding more levels, touching some things up, and make leaderboards work for each level.  Also, I’ve got to submit the game to the Windows Store before the end of the calendar year for the Dream Build Play 2017 competition.