<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Adam Herd on Medium]]></title>
        <description><![CDATA[Stories by Adam Herd on Medium]]></description>
        <link>https://medium.com/@adamherd?source=rss-1f5d31b3b1ed------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*7KlbtFivbBtiQxxEP9ygJQ.jpeg</url>
            <title>Stories by Adam Herd on Medium</title>
            <link>https://medium.com/@adamherd?source=rss-1f5d31b3b1ed------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 23:49:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@adamherd/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Binary Search — One Insanely Simple Python Implementation]]></title>
            <link>https://medium.com/@adamherd/binary-search-one-insanely-simple-python-implementation-95e1a661cf00?source=rss-1f5d31b3b1ed------2</link>
            <guid isPermaLink="false">https://medium.com/p/95e1a661cf00</guid>
            <category><![CDATA[binary-search]]></category>
            <dc:creator><![CDATA[Adam Herd]]></dc:creator>
            <pubDate>Sat, 06 Nov 2021 15:52:02 GMT</pubDate>
            <atom:updated>2021-11-06T15:52:02.390Z</atom:updated>
            <content:encoded><![CDATA[<h3>Binary Search — One Insanely Simple Python Implementation</h3><p>Binary search is one of the most useful searching algorithms out there. It’s main use is to find the position of a target value within an array. <strong>Note</strong>: the array <strong>must be sorted </strong>for binary search to work.</p><h3>How Does Binary Search Work?</h3><p>A common explanation for this algorithm would be:</p><ul><li>Compare target value to middle element of array.</li><li>If they are not equal, we can eliminate one half of the array, since all values to the right of the middle are greater than it and all values to the left are smaller than it. This means that if the middle value is greater than the target value, we can eliminate the right half of the array since all those values will be greater also.</li><li>We repeat this process until we have found the target value.</li><li>If at anytime the remaining half is empty, then the target is not in the array.</li></ul><h3>Implementation</h3><p>The code below is a Python implementation of binary search:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/412/0*L8lm0FyqfI18z50_.png" /></figure><h3>Visualization</h3><p>Now, here’s a quick visualization of the search algorithm courtesy of <a href="https://www.cs.usfca.edu/">https://www.cs.usfca.edu/</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*II7KMLR8k4DIVHJ72dIHzg.gif" /></figure><p>You can see how we begin with the middle element of the array and, seeing that it is smaller than the target, compeltely eliminate the left half of the array. Then we find the middle element of the remaining array and do the same thing again. The process repeats until the target is reached or the remaining array is empty.</p><h3>Time Complexity of Binary Search</h3><p>The time complexity of this algorithm is O(n). In the best case, time complexity would be O(1), where the central element of the array is the target we’re looking for.</p><h3>Space Complexity of Binary Search</h3><p>The space complexity of this algorithm is O(1). This is because no matter how big the data we’re working with, we always keep track of the same number of variables.</p><h3>References</h3><ul><li><a href="https://en.wikipedia.org/wiki/Binary_search_algorithm#:~:text=In%20computer%20science%2C%20binary%20search,middle%20element%20of%20the%20array.">Wikipedia</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=95e1a661cf00" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How I Made a Simple 2D Mobile Game in Unity]]></title>
            <link>https://medium.com/@adamherd/how-i-made-a-simple-2d-mobile-game-in-unity-f837db7e02fd?source=rss-1f5d31b3b1ed------2</link>
            <guid isPermaLink="false">https://medium.com/p/f837db7e02fd</guid>
            <category><![CDATA[mobile-games]]></category>
            <category><![CDATA[android-developers]]></category>
            <category><![CDATA[mobile-development]]></category>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[unity2d]]></category>
            <dc:creator><![CDATA[Adam Herd]]></dc:creator>
            <pubDate>Thu, 09 Sep 2021 08:04:29 GMT</pubDate>
            <atom:updated>2021-09-13T09:06:50.553Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi there, I’m going to share with you how I made a very simple android mobile game using the Unity game engine. I am by no means an expert with this game engine, having made two or three small projects before, but I feel I can provide some good advice when it comes to making your own.</p><p>The game I made is called Stack ’em High. Some quick deduction will tell you that this is a game where the objective is to stack objects on top of one another as high as you can. Originality at its finest.</p><p>When you begin the game, a winch will move back and forth across the screen with a plank of wood hanging on the end. When the user taps the screen, the winch will drop the plank. You continue this process, stacking as many planks as you can without the stack falling over. If it does, you lose. All the while, a score counter is keeping track of how many planks you have, and your high score will be recorded so you always have something to try and beat.</p><p>I created every part of this project, from the artwork, the animations, and the actual coding of the project itself, as well as publishing at the end.</p><p>So, in this article, I am going to talk through all of these processes and how I did them to hopefully provide some insight and useful tips for those who wish to make their own mobile games.</p><p>I will not cover things in chronological order as they were implemented. I tend to work on whatever I feel like at the time. However, in this article, I will break it up into sections: <strong>artwork, coding, animations, music/sound effects, publishing</strong>.<strong> </strong>This will make it easy to jump to and from different sections for you.</p><p>So, let’s begin!</p><p><strong>Setup</strong></p><p>When you boot up the Unity hub, you will see a list of projects. This will be blank if you have not made anything beforehand. Click on the <em>New</em> button to the right in blue. Here, you will be given the option to make a 2D or 3D game. Stack ’em High is a 2D game, so I chose that. Then I gave it a name and clicked <em>Create</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/955/1*IbWIEogk9sVIid3LTshS2Q.png" /></figure><p>Some general things it’s good to do before starting: set the aspect ratio to 16:9. This is a problem for mobile games. There are so many devices with different screen sizes that it’s hard to cater to them all without something messing up. I’ve found that 16:9 works well most of the time, so I defaulted to that. Doing this at the beginning will save you so much time later on readjusting the size of all your assets to fit a new aspect ratio. Not fun.</p><p>You can change the aspect ratio by clicking on the dropdown under the game tab. There’ll be many listed there. My game is played in portrait mode, so I chose a 16:9 portrait.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Lo5VFUTvaaVSk07mNNbwcQ.png" /></figure><p>For my game, I needed three scenes. A home scene where the main menu will be, a game scene where the actual game is, and an end screen where your score and high score will be displayed when you lose, and a restart button to begin the game again.</p><p><strong>Artwork</strong></p><p>Now that the setup is done, we’re going to take a step back from Unity for a moment and focus on the artwork. Unity has some default art you can work with but I like having the real thing there as I’m working on the game.</p><p>There are many tools you can use to make game art/pixel art. Some of them are free as well, like Gimp. However, for my game I used photoshop. Adobe has a photography deal where you can get photoshop for around €12/month.</p><p>Quick disclaimer, I am by no means an artist. In fact, I would say I’m pretty bad at pixel art. I would need to practice a lot more to get to a competent stage, but maybe you’re better than I am and you can make better art for your game. For now, this is how I did it.</p><p>Once I booted up photoshop, I chose my settings for the art. The size is not so important as this can be adjusted at any time during the process and after. For starters, I chose a 40 x 40 px canvas with 72 pixels per inch resolution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R8o8rVvqyaDa_VkzqVthQQ.png" /></figure><p>Once created, there was some setup needed before drawing any art. Firstly, make a new layer that will be the layer you draw on. This is done by pressing the plus box in the bottom right of the screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/291/1*NC15dAR3HQkbSHeBqAvpdg.png" /></figure><p>Now, when drawing pixel art, it’s a good idea to have some grids on screen so we can better see our art and know where we’re clicking. To do this, go to the <em>Edit </em>tab. Under <em>Preferences</em> find <em>Guides, Grid, and Slices</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/431/1*2j43rnyeLuTPHjFfUVvZmQ.png" /></figure><p>I changed the <em>Gridline Every </em>value to 1 pixel and the <em>Subdivisions </em>value to 1 also. Click okay and exit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1004/1*UlAHhNtTdkKcm5Bi5FltmQ.png" /></figure><p>Now to see the grid, I pressed <em>Ctrl + </em>‘. This will draw lots of little squares all across the canvas as you can see in the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*of1a6K37Db4H3mlAzx3eRA.png" /></figure><p>Next, I selected the pencil tool on the left and adjusted the size to 1px to fit with the size of our grid boxes. The size can be adjusted via the brush settings tab on the right or via the toolbar up the top.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/785/1*JHJpJuNhF9mnmyJH1nLytQ.png" /></figure><p>Finally, now you’re ready to draw. Simply click on the boxes with whatever colour you like and make your art. I needed four main pieces of art for this project which is not a lot. Firstly, I needed a background. The game is set outside, so it made sense to have some kind of sky background. Then there were the planks themselves, the winch, and finally the ground where we began stacking the planks.</p><p>If when you use the pencil tool everything looks blurry at the edges, then go back to the <em>Preferences </em>tab and under <em>General </em>change the interpolation to <em>Nearest Neighbour</em>. This will preserve hard edges and get rid of any blurry quality to the art.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1005/1*td5Eao8FBMHt9w1Xwz-ltA.png" /></figure><p>Below are images of the individual pieces of art.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u3gEs7hEA4s3iy6B2O28qg.png" /><figcaption>The Plank</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s_5CCPjWKZkL6AvDB6zrbw.png" /><figcaption>The Ground</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Irad6UcWssIbBW747fT-Nw.png" /><figcaption>The Winch (this is where my amateur art skills are really highlighted)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VmDlnI0Xr8vt2MVm_w7a2A.png" /><figcaption>The Background</figcaption></figure><p>The process for making the background was different from the others because I didn’t need pixel art as such. All I needed was a gradient blue background with clouds. This could be done much quicker another way.</p><p>Simply pick the gradient tool on the left and two colours. I went with a light blue fading to a slightly darker blue as you move up, just like the sky itself. Once this was applied, I got a chaotic looking brush from the <em>Brush Settings </em>tab and chose white. Then I just clicked in various places to make the clouds. No more work was needed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/782/1*YJypeeQ70qtcsAasNECZyA.png" /></figure><p>You can see how the brush looks in the image above. Exactly like a cloud. It would have been a lot of unnecessary work to do that pixel by pixel like the other art.</p><p>I’ve adjusted the size on a lot of them to fit with the art itself. This is easily done through the <em>Image </em>tab at the top. Click on <em>Image Size </em>and work away.</p><p>Now that all the art is done, the next step was to export and import it to Unity. To export, go to the <em>File </em>tab and under <em>Export</em> click <em>Quick Export as PNG</em>. Now get that saved file and drag and drop it into the Unity assets tab. It’s a good idea to set up a few folders to keep your work organized. I put my art in a folder called Pixel Art<em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NrcFcHWW0Rii024LTy-86w.png" /></figure><p>If you notice your art is looking blurry when close up, then you can change some settings. Click on the image and change the filter mode to point and the compression to none and hit Apply. This should remove any blurry aspect of your art.</p><p>Lastly, for the plank object, I made it a prefab. To do this, I dragged it into the hierarchy on the left, then dragged it back down to the project docker below. This turned it blue which is how you know you’ve made a prefab. Prefabs allow configured game objects to be reused and retain all their configurations, such as any scripts or components that are attached to them. This plank object will be reused a lot, so it needs to be a prefab.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/177/1*8GluQXQ-kJ0Iy0XQ2ueX_Q.png" /></figure><p><strong>Coding</strong></p><p>I predict this will be the longest section of this article by far.</p><p>There isn’t a huge amount of code that goes with this game to be fair, but I want to make sure I explain it all as clearly as I can.</p><p>Firstly, let’s run through all of the features this game needs, so we can better talk through the code for it after.</p><p><strong>Home Scene</strong></p><p>For the home scene, we’re going to have a title and two buttons; a button to start the game, and a button to quit the game.</p><p><strong>Game Scene</strong></p><p>For the game scene, we’re going to have a winch that moves left and right across the screen. When it reaches a left boundary, it will move right, and when it reaches a right boundary, it will move left again.</p><p>Then we are going to have a plank of wood that spawns at the bottom of the winch at whatever current point the winch is at. The plank is then going to fall when the player taps the screen, and another will spawn in its place a second later. There will be a score counter in the top left that will track the user’s score as they play.</p><p>The game will end when the stack falls over.</p><p><strong>End Screen</strong></p><p>For the end screen, we’re going to display the high score as well as the current score from the last attempt. We’ll then have a restart button that will restart the game scene. And also, we’ll have a message taunting the user about their failure.</p><p>And that’s all the features this game is going to have. Like I said, it’s very simple but it’s going to use some features that will be universal across a lot of mobile games that you will make, such as a score and high score counter, spawning system, collision detection, and movement between scenes, as well as text animation and music.</p><p>So, let’s dive into the code now. We’re gonna go scene by scene, beginning with the home scene.</p><p><strong>Home Scene</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bapy_IBfxOdby-PWrj_hnw.png" /><figcaption>The Home Scene</figcaption></figure><p>This is very simple. First, I made some title text in photoshop. This doesn’t really need a detailed explanation, I just made what I thought looked good. You can do the same. I placed this text at the top of the scene.</p><p>Next, were the two buttons. I created a new TextMeshPro button by right clicking and hovering over the <em>UI </em>tab, then changed the text to Start Game. I copied and pasted this to make another button, calling this one Quit Game.</p><p>Now we need some code to actually do something when these are clicked.</p><p>I made a script called Buttons and wrote the following code inside:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/416/1*mwzHEgmV_jJcbKChUHi5sw.png" /></figure><p>So, when the Start Game button is pressed, the function changeToMainScene will be called. This function will then load the main scene, which is the game scene. Simple.</p><p>And when the Quit Game button is pressed, the application will shut down. Perfect.</p><p>Now, there’s one more thing we need to do here. We need to assign each button to their respective functions so they are called when the button is pressed.</p><p>To do this, we make a new game object and attach our Buttons script to it. We then click on the button and come down to where it says “On Click”. We drag and drop the game object with the script in it there and select the function we want to call from the dropdown. We do this for both buttons and now they both work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/514/1*tGhKUzi1WHSeuX9WfsqE1A.png" /></figure><p>That’s pretty much it for the home scene. The only other thing we need to do is animations, but that’ll be covered in the animation section later on.</p><p><strong>Game Scene</strong></p><p>So, now we move onto the game scene. This will be the most extensive scene in terms of code as you can imagine, but I will try and explain as best as I can.</p><p>Firstly, we’ll set up the scene how we want it. This is how I did it below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/1*EEt_HZVwgP4BClK8i1DddQ.png" /></figure><p>As you can see, we have the winch in the centre of the screen at the top, and the plank hanging off the end of it. We have our background and ground that we made in photoshop as well.</p><p>Firstly, we need to make the plank actually fall when the game is played. To do this, we’re going to click on the plank and add a component called RigidBody 2D and use the following settings on it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/421/1*qdnn_hIKRpiqXt75i6hkDA.png" /></figure><p>As you can see, the gravity scale is currently set to 0, this is so the plank doesn’t fall as it’s hanging on the winch. We only want it to fall when the user taps the screen.</p><p>To do this, we need a script. I made a new script called MakePlankFall and added the following code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*AFOUs23TDBInPbrYzBVpNA.png" /></figure><p>Firstly, we have an if condition that will only pass when the screen is tapped. Then, we get a reference to the rigidbody that we just made, and set the gravity scale to 1. This will make the plank fall when the screen is tapped.</p><p>However, the plank just continues to fall off screen and doesn’t stop when it hits the ground. This is because we need to add collision so the plank and the ground can collide. To do this, click on the object and add a component called Box Collider 2D. We do this for both the plank and the ground. Now, when the plank falls it collides with the ground and stops. Perfect.</p><p>Now, we need to implement the movement of the plank and winch back and forth across the screen. To do this, we first make a new script called MoveCable and add the following code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*4hlsWWGNaqe5ULHPN28nkA.png" /></figure><p>This code causes the cable to move back and forth across the screen and when a certain boundary on the x-axis is met, it will change direction.</p><p>To get this to work with the plank as well, we add the following code to the MakePlankFall script.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lvMc68O3nbK29coEiq1NKA.png" /></figure><p>Now, our winch and plank will move back and forth across the screen together.</p><p>At the moment, we only have the one plank, and when that falls, there’s no spawning system in place to continue the game. So, now, we’re going to implement spawning.</p><p>So, we need to spawn a plank wherever the winch currently is on the screen. We also want there to be a bit of a delay from when one plank falls to when another takes its place. To do this, we use an enumerator which will wait one second before spawning another plank when the user taps the screen. And remember, when the user taps the screen, we set the gravity scale of planks to 1 so they fall. So, before we spawn a new one, we need to reset this to 0. Then we use the instantiate function to spawn a plank at the current position of the winch which we constantly keep track of in the update function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/572/1*zyesUK8qe5f5VRYFc2IdHg.png" /></figure><p>The Begin function is called at the end of our if statement checking for a user tap.</p><p>Now, when we tap the screen and a plank falls, another will take its place a second later and we can tap again to stack this one atop the first.</p><p>At this point we have most of the game loop done. There’s just one more crucial thing we need to add. A game over trigger. When the stack falls over, it needs to trigger a game over.</p><p>My way to do this was to just continually check if any of the planks were below a certain Y value and if they were, we would switch to the game over scene.</p><p>I’ve done this with the following code below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/273/1*eOMIlYk3BM5SyYFNGbVSlA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*K_DPAmSxFBNTXLmvqmqxPg.png" /></figure><p>You can see I have the scene transition in a file called Crossfade. This is because I have an animation that takes us between scenes. Don’t worry I’ll cover it all in the animation section. For now, you just need to focus on the “SceneManager.LoadScene(levelName)” line as this is what takes us between scenes.</p><p>With the main gameplay out of the way, the last things we need for the game scene are UI. Firstly, as the stacks of planks get higher, we need to move the camera and other UI elements accordingly. This means the background and winch, so that they stay the same no matter how high the stack goes. We do not need to move the ground element for obvious reasons.</p><p>To do this, I tested to see at what point the stack got too close to the top of the screen and chose that to be the point when we moved up. The code for this is very simple. I have a count value that increments each time a plank is dropped. When the count reaches 11, everything moves up and we reset back to 0. I am sure this is not the optimal way to do it but it works for the game.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/611/1*HqAjtMaVaroXPtScJb0skw.png" /></figure><p>You can see I’m calling a bunch of different functions here, so let’s move over to their respective files and see what they do.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*MykRjUjg-Lx087NbMbpY4Q.png" /><figcaption>Attached to the winch object</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HE4TBats1EQELXVHo1RvyA.png" /><figcaption>Attached to the background object</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qeAdHwnG8aDxGWHIuxpcbw.png" /><figcaption>Attached to the main camera</figcaption></figure><p>Each script is attached to the object it’s going to move, and then it’s just a case of shifting the Y position of the object up a little. It took some playing with to get a shift that worked fine, but the values I have here work perfectly.</p><p>And now it’s on to the score. This one is pretty easy too. We create a canvas and a TextMeshPro text element and set the text input to 0. Then, we create an empty game object in the scene and create a new C# script, attaching said script to the game object. You need a script attached to a game object for it to do anything.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SGtqG7Ted_u6o1B2h_5S3Q.png" /></figure><p>In our new score file, we have the following code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/658/1*2d8XWqTZLR-8zZvZ5pqv2g.png" /></figure><p>This will increment the score counter each time the player taps the screen. It will also set our player prefs to the current score. This is a value we can retrieve easily from other files and will be useful when setting our high score, as we can compare values to see which is highest. Lastly, I have a buffer here called timeBetweenTaps that ensures the user can’t just continually tap the screen rapidly and have their score shoot up. There is a delay much like for when a new plank spawns, that will only count a tap every second. Anything less than that won’t be counted.</p><p>Well, that’s mostly everything done for our game scene except the transition, but as I said, we’ll come back to that. Now, let’s move on to the end scene.</p><p><strong>End Scene</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/357/1*C9Mvvx2PSmHVecaPuxUqJA.png" /></figure><p>This is the layout I’ve gone for. Very simple, but I think it looks nice. Now we have a few elements here. The text at the top can be left alone until we come to the animations. The Restart text is a button, so we need to configure that.</p><p>Remember from before, to make a button we right click in the hierarchy and create a UI element TextMeshPro button. We change the text in this button to Restart.</p><p>Then we create a new game object, attaching a new script to it called Restart. And in this script we just have a function that will load the main scene again, our game scene.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/358/1*cmE2tfx_PIqdssKVyfo36A.png" /></figure><p>Drag and drop the game object into the on click section of the button, and find the restart function we just wrote. All done. Now, when that button is tapped, it will reload the game scene and you can play again. A full game loop.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hjd5cq4SZBIpUABSuRLGuw.png" /></figure><p>Lastly, we have the score and high score elements. I simply created TextMeshPro text elements and changed the text to what you can see in the image. I made the numbers independent elements from everything else as we will want to change these and having them as their own text elements makes it a lot easier.</p><p>You know the drill now. Create a new game object and attach a new script to it. This script contains the following code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/663/1*V2xS59B14aWCd_-fIZ6rhQ.png" /></figure><p>This code will take the values we have saved in our player prefs for the current score and highscore and display them on screen. So, the player can see their most recent score as well as the highest score they’ve gotten.</p><p>To find out if a current score is the new high score, we have a GameManager script with the following function inside.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*j5KxkmUx-btByoalk7BDIg.png" /></figure><p>This will check if the current score is higher than the high score and if so, set the new high score to be the current score.</p><p>This function is called inside the MakePlankFall script here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/273/1*eOMIlYk3BM5SyYFNGbVSlA.png" /></figure><p>You can see, it’s called when a plank falls below our Y threshold, i.e the game ends because the stack fell over.</p><p>And that’s the code for the end screen. Actually, it’s the code for most of the game. Phew, thought I’d never get there.</p><p>The next Step is to move on to the animations. I promise this will be shorter. Here we go.</p><p><strong>Animations</strong></p><p>Quick disclaimer. This was the first time I had ever used animations in Unity, so they may not be AAA worthy, but I think they’re serviceable for the type of game I’ve made here.</p><p>The animations are very simple too as we have no characters in which we need to animate or anything, just some text and scene transitions.</p><p>Firstly, let’s start with the home screen. As you can see from the video below, we have some planks moving up and down and our game title sliding in frame from off screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*egujtgLN_HoKwI4JKZvaiQ.gif" /></figure><p>Let’s start with the game title.</p><p>This may be hard to explain in writing so I’m going to link to a great video on YouTube that you can watch if you’re confused: <a href="https://www.youtube.com/watch?v=CE9VOZivb3I">https://www.youtube.com/watch?v=CE9VOZivb3I</a></p><p>It can be very easy to mess up animations so I’d highly suggest checking out the video above for a more in depth visual look at how to make these animations.</p><p>Click on the object you want to animate and move over to the animation tab at the bottom. If the tab isn’t there, then simply go to Window -&gt; Animation -&gt; Animation and just dock it down at the bottom.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/771/1*MT6I39FY_-7KTdX8bL4oUg.png" /></figure><p>When you first click on an object you’ll see it gives you a button called Create. Click this and name your animation.</p><p>I have my title object off screen by default and this animation will move it on screen.</p><p>To begin animating, click the red record button to the left side. I want the animation to last one second, so I slide the time slider at the bottom to the one second mark.</p><p>Now, I just dragged the title object down into frame and left it where I wanted it. This creates keyframes in the animation below. If you drag the slider back now, you’ll see the object move back up out of frame because you’re reversing the animation.</p><p>To actually have an animation work, we need an animation controller. Luckily, Unity creates one for us when we make a new animation.</p><p>This is what mine looks like for the title scroll. Pretty simple and it works great.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SNtup00-SH5n1jMq378T4w.png" /></figure><p>And for the pieces of wood jumping up and down, I do exactly the same thing as the title. Click on them, create an animation of them moving up, and then create a second animation of them moving back down to where they were. I then link these two movements in the animator as you can see in the two images below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TMoF9YEJ___d83gu7zijiQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uq_qe1ILIqj89PR6pO3cEA.png" /></figure><p>There’s two different animators. One for the wood going up and back down to its original position. And the second for the wood going down and back up to its original position. So for animations in total.</p><p>There’s definitely a more efficient way of doing this but as I said it was my first attempt at animation.</p><p>This makes one continual animation of the wood going up down, up down, up down.</p><p>That’s all for the home screen, let’s move on to the game scene.</p><p>There’s only one animation here, the slight fading to black when the scene starts and ends that can be seen below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*L2uuyeEZaWW5tFCjEzBKIQ.gif" /><figcaption>Transition for starting the game</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*j3peTr_QTNgt9jQT6MRszQ.gif" /><figcaption>Transition between game scene and end scene</figcaption></figure><p>To do this, I created a new canvas and a black rectangle as a child of that canvas.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9nen1LwS0FFlFDFuAa6jUg.png" /></figure><p>Like before, I clicked on the rectangle and made a new animation. This time, we just need to adjust the alpha of the rectangle so that it becomes transparent over however long you want. I chose one second again.</p><p>I then made a new animation which is the reverse of the first one. The rectangle fades back in from nothing. This animation plays when the user loses and transitions them to the end screen.</p><p>You can see the animator for this below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/542/1*iXmKAwi9JvvE2_G_FNSvZA.png" /></figure><p>For the fade between game scene and end scene, we need this code from before:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*K_DPAmSxFBNTXLmvqmqxPg.png" /></figure><p>And this function is being called in the MakePlankFall script here:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/273/1*eOMIlYk3BM5SyYFNGbVSlA.png" /></figure><p>You can see I’m referencing a trigger here. This is what we’ll use to play the animation when we want it played. You can make a trigger like so in the image below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/1*vu1wQgNdQLZsjlYpEawMNA.png" /></figure><p>And that’s it for the game scene.</p><p>Lastly, we have the end screen. There’s not much to say on this. As you can see below, it uses a similar animation to the planks on the home screen. Just some text jumping up and down.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*b84LAPrsC1wH_57ACcvGig.gif" /></figure><p>That’s all for the animations. Again, if you don’t understand anything, I’d highly recommend you check out the video I linked at the start of this section. It taught me how to animate and explained it much better than I can in this article.</p><p>The next section is music!</p><p><strong>Music</strong></p><p>I just wanted some casual background music for the game. I’m not yet skilled enough to make it myself, so I found some non-copyrighted music on youtube and downloaded it as an mp3, then dragged and dropped it into Unity.</p><p>To use it, I moved to the home scene and created a new game object and added a component called Audio Source. In the <em>audioclip </em>section, I dragged in the mp3 I had just imported. So this will play when the home screen loads, however, at the moment the music will stop when the user starts the game, as it’s only loaded in this one scene.</p><p>To correct this so it plays across all scenes, we create a new C# script and attach it to the game object. Inside, we write the following code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*upVXIA1jQZWEJiXAO6tdAQ.png" /></figure><p>This will prevent the game object from being destroyed when we load a new scene, and the music will continue to play. I also checked the <em>Loop </em>box under the audio source so that when the song finishes it will just play again.</p><p>Lastly, I added a sound effect whenever a plank hits another plank. Again I needed to get an mp3 into Unity. This time, I made it myself.</p><p>Using my microphone and Audacity, I slammed my fist off my desk to make a thumping sound. I then imported this sound into Unity.</p><p>Now, to get it to play when a plank hits another plank, we need to move over to the game scene. Once there, we find our plank object and add an audio source to it, dragging in the mp3 of my fist slam.</p><p>Then I added another Box Collider 2d to the object but made this new one a trigger. This will call the function <em>OnCollisionEnter2D</em> whenever something touches the plank, i.e another plank falling on top of it.</p><p>The following code below then causes the sound effect to be played.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/782/1*QZzyLJ161ps9ksItLjYpLw.png" /><figcaption>First we need to get a reference to the audio source as can be seen at the end of the start function here.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*nPu2WqbWqGOdKlR1BXgRkg.png" /><figcaption>Then in the collision function attached to the collider we just made, the following code plays the sound effect.</figcaption></figure><p>And that’s all for the music section.</p><p>The game as a whole is pretty much done now.</p><p>I am sure there are bugs and optimization problems with it that I have not seen, so there may certainly need to be updates in the future, but this is the way it is now. If you can think of any optimizations yourself, then please do let me know as I’d love to hear them.</p><p>I’m always trying to learn and get better so any suggestions would be very much appreciated.</p><p>Lastly, now, we move on to the publishing section. This one can be a real pain if you don’t know what you’re doing and took me some time to figure out.</p><p>So, here we go.</p><p><strong>Publishing</strong></p><p>To publish a game on Google Play, we first need to do a few things in Unity.</p><p>Make sure you have the platform set to Android and all your scenes are loaded at the top.</p><p>Then, go to File &gt; Build Settings &gt; Player Settings &gt; Other Settings and scroll down until you see something similar to the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/967/1*E78NeMUBZREKWo947tcgxA.png" /></figure><p>You must name your package. This doesn’t matter too much, just have it make sense.</p><p>Then you pick a version. Just enter in some number. Same with bundle version code.</p><p>For the minimum and target API level, Google Play now requires your target API to be at least 30. If you don’t have this installed already, you can download it from Android Studio and copy paste the folder into Unity. There’s a bit more involved here so if you are stuck with this then let me know and I’ll try and help.</p><p>Lastly, make sure you tick ARM64 under Target Architectures.</p><p>Now, go to <em>Publishing Settings</em> which is on the same page and click <em>Keystore Manager</em>. Create a new keystore here. You’ll have to give it a name and a password.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FB8-iVYwUgtr3XCIOP7xZg.png" /></figure><p>Once that’s done, select the keystore here:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1008/1*WJABjoiu6ip5OD1MyJki6Q.png" /></figure><p>Now, provided I haven’t missed any steps, you should be able to export the game and have it accepted by Google Play.</p><p>To do this, go back to Build Settings and make sure you tick <em>Build App Bundle (Google Play)</em>. Then click Build and give your build a name. Once built, it should have the extension <em>.aab</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/635/1*y2DJDiQqQNdt2E6iY9Ufqw.png" /></figure><p>Once that’s done, I went online to the Google Play Console. You must pay a one time fee of $25 to publish games. But after that, you can publish as many as you want and never have to pay again.</p><p>Once in there, I clicked on the Create App button in the top right and followed their instructions. They will give you a step by step guide on everything.</p><p>Here is the published app below in Google Play Console:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n8unXuewQYJ7n6Vx45hkNA.png" /></figure><p>And that’s all! We’re done at last. I’m impressed if you made it this far and I hope you got at least one thing of value from this article. It has taken a really long time to put together but I very much enjoyed the process.</p><p>Thank you for your time and good luck with your own projects.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f837db7e02fd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>