<?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 Alex Somerville on Medium]]></title>
        <description><![CDATA[Stories by Alex Somerville on Medium]]></description>
        <link>https://medium.com/@mrlovelies?source=rss-2e294212e4bf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*IdnPjmEItKBXk2z7_kuB4g.jpeg</url>
            <title>Stories by Alex Somerville on Medium</title>
            <link>https://medium.com/@mrlovelies?source=rss-2e294212e4bf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 19:59:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mrlovelies/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[The Highs and Lows: A Quasi-Literate Interpretation of Empathy]]></title>
            <link>https://mrlovelies.medium.com/the-highs-and-lows-a-quasi-literate-interpretation-of-empathy-edfa2a0ba4bc?source=rss-2e294212e4bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/edfa2a0ba4bc</guid>
            <category><![CDATA[humanity]]></category>
            <category><![CDATA[self-improvement]]></category>
            <category><![CDATA[life]]></category>
            <category><![CDATA[empathy]]></category>
            <dc:creator><![CDATA[Alex Somerville]]></dc:creator>
            <pubDate>Wed, 03 Aug 2022 05:03:07 GMT</pubDate>
            <atom:updated>2022-08-03T05:03:07.167Z</atom:updated>
            <content:encoded><![CDATA[<p>Living life is a fucking roller coaster. Even the simple act of existing takes you all over the place on a daily basis. You have moments where you feel like everything is in place and there’s nothing that could possibly get better, and moments where where you feel like there’s nothing lower. These moments last literal moments, hours, days, months... You get the picture.</p><p>These &quot;moments&quot; are always very personal and each and every person responds to them differently, for any number of reasons. The roller coaster of your life is a ride that you ride by yourself... But that doesn&#39;t mean you&#39;re alone. Your highs can bring others up during their lows and vice versa. This is how empathy works. People are designed to be attuned to those around them, especially those they are close to personally, and in many cases will emulate your current arc. I see this as a sort of mental health survival mechanism to help make sure you&#39;ve always got someone there who you can find support in as you careen down your latest low.</p><p>Why, then, do so many of us actively seek to sever our connections with those around us in such vicious ways? We go out of our way to condemn a person&#39;s accomplishments and mock their joy, pulling them down, forcing them under, and then proceeding to keep them down by dismissing their lows. I&#39;ve been guilty of this in my life and it&#39;s never put me back on track to an upswing or given me any sense of self fulfillment. It&#39;s only ever forced me further down in some misguided, bullshit idea that I can somehow find my way up on the backs of others.</p><p>To keep others down, you have to stay there yourself and when you do so, you become disconnected with no hope of being uplifted by the people you&#39;re stepping on when they someday find their way back up.</p><p>As I sit and write this, I realize that this analogy is well past the point of being heavy handed. I think the point I intended to make has been lost to excessive exposition. If there&#39;s anything I could ask even one person to take away from this, it&#39;s that empathy is the most critical aspect of humanity to being able to successfully navigate the highs and lows of our lives and our existence. While empathy is a part of the human design, it is a learned skill, and can thus be, at best, forgotten and left to wither, and at worst, suppressed and perverted. Practice empathy and connect in whatever way you can. Your highs will be higher. Your lows will probably be just as low... But at least you can find solace and hope from the highs of those around you.</p><p>Be good a good person. We&#39;re all in this shit show together.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=edfa2a0ba4bc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unity Dev: Homing Missiles]]></title>
            <link>https://mrlovelies.medium.com/unity-dev-homing-missiles-4d76c7fbdbd?source=rss-2e294212e4bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/4d76c7fbdbd</guid>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[gamedevhq]]></category>
            <category><![CDATA[game-development]]></category>
            <dc:creator><![CDATA[Alex Somerville]]></dc:creator>
            <pubDate>Wed, 07 Apr 2021 21:48:05 GMT</pubDate>
            <atom:updated>2021-04-07T21:48:05.413Z</atom:updated>
            <content:encoded><![CDATA[<p>We can’t just rely on our ship’s laser to get the job done. This is a hostile alien fleet after all. A <em>never-ending</em> hostile alien fleet. Some jobs require a more… heavy handed approach.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0L0IBlP7bolVIPWP7tZpIA.gif" /></figure><p>There’s a couple things I want out of a good alternative firing method.</p><ul><li>Area damage, otherwise known as an AOE (area of effect). When my missile explodes, it should potentially take out multiple enemies at once.</li><li>Some degree of intelligence. My missiles should track down enemies and move towards them by course correcting.</li><li>Should be a time bomb. Since all the enemies are moving around pretty quickly and then warping back to the other side of the screen, I don’t want my missile flying around the screen like a chicken with its head cut off.</li><li>It should look like a, y’know, missile.</li><li>Should have an ammo system and be usable with a right mouse button click or the north button on a gamepad.</li></ul><h3>Asset Collection</h3><p>I can really only pretend to dabble at pixel art (or any art in general), so I used the wonderous reaches of the <em>internet</em> to find some asset packs I could use in my game. <a href="http://itch.io/">itch.io</a> is a truly spectacular resource for these types of things and can really put a nice shine on your game. Which, of course, got me pretty excited so I swapped out a number of the existing assets in my game with some retro pixel goodness (my favourite kind of goodness). You can find the asset packs I’m using at the following links:</p><ul><li><a href="https://livingtheindie.itch.io/pixel-sidescroller-spaceships">https://livingtheindie.itch.io/pixel-sidescroller-spaceships</a></li><li><a href="https://ansimuz.itch.io/spaceship-shooter-environment">https://ansimuz.itch.io/spaceship-shooter-environment</a></li></ul><h3>Firing Missiles</h3><p>Basics first. We create a new GameObject using our missile sprite, give it a 2D collider (set to Is Trigger) and a rigidbody (remembering to turn gravity off for it). I’ve started using the Polygon Collider 2D for this project as it allows for more accurate colliders based on the shapes of my sprites.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/317/1*UZdApKW6EUFVJZqX5JuwUQ.png" /></figure><p>Make a new C# script for the missiles and attach it, then turn the GameObject into a prefab. For now, we can set it up to just move the missiles to the right as soon as they’re spawned.</p><p>Next up, we need some user input to spawn a missile instance. I’ve created a new Action in my Player Action Map for just this purpose.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/615/1*Itf7xtvOpIyJpG3GOmYPMg.png" /></figure><p>Hook up your new OnFireMissile event in your Player Input component, and add create the code that’ll Instantiate your missile into the scene. There’s a bit extra in this screen that assumes a basic ammo counter for the missiles and a relevant display in our UI Manager.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9jcTlSxlARHmnJTkiQfatQ.png" /><figcaption>Player.cs</figcaption></figure><h3>“Smart” Missiles</h3><p>This part has a couple different aspects to it, but is also the most rewarding!</p><p>First, to get the missile to attempt to follow a specific enemy, we need to tell it enemy it should be following. We’ll do that by collecting a List[] of all the enemies that are active at the moment the missile is fired. From there, we’ll take the list of enemies and iterate through each of them. As the script calculates the distance of each enemy (relative to where your ship was when you fired the missile), it will compare the value against what is currently set as the closestDistanceSqr . If that enemy has a smaller distance than what is currently stored in that value, it becomes the new closestDistanceSqr and its transform will be stored for use once this loop is done.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VAPAFtKCTqTxKuYCxhyemQ.png" /></figure><p>Upon hitting the last enemy in the list, we now have our bestTarget which is the transform of the enemy that was closest to us when we fired out missile. Seems like a long time ago, yeah? Don’t worry, it all happens insanely quickly.</p><p>Now our Player has information about where the nearest enemy is to it, but we need our HomingMissile to have that information so it knows where the go.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V6bw8TMOYrm0wKxJm9YIeg.png" /><figcaption>Player.cs::OnFireMissile()</figcaption></figure><p>Pretty, simple, we just create a variable inside of our OnFireMissile() method which gets the value generated from our GetClosestEnemy() method as a Transform. We then fetch our HomingMissile component and call a script called MissileTarget() where we simply load up a variable in our HomingMissile script with the important information.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rSzBQHmAzfGPjEmODHhiIA.png" /><figcaption>HomingMissile.cs</figcaption></figure><p>Now we’ll expand on our Missile’s current movement, which is just driving it straight forward (to the right). The general idea is that we’re going to find the Cross product between the direction our target is in and the direction that the rocket is facing. When we first Instantiate it, it’s going to be facing and moving Vector3.right</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ix_0mghcfAC6AG5ck2kH-w.png" /></figure><p>For a much better explanation than I could ever hope to provide about the math going on here, I recommend checking out <a href="https://www.youtube.com/watch?v=0v_H3oOR0aU">How to make a Homing Missile in Unity</a> on the <a href="https://www.youtube.com/channel/UCYbK_tjZ2OrIZFBvU6CCMiA">Brackeys</a> YouTube channel which, you’ll see, had a heavy hand in my solution here. As long as the HomingMissile object has a target to seek out, it will rotate itself as it moves through space, creating for some interesting paths. If it has no target, or loses its target, it will just carry on straight in the direction it was already headed.</p><h3>More Explosions!</h3><p>What good is a rocket if all its doing is aimlessly following an enemy around?</p><p>We’ll set up an IEnumerator in order to create a countdown for the value of a _timeToExplode variable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uEWOF6z11nBzr9Xgh0POKA.png" /></figure><p>This will Instantiate an object which contains an animation of an explosion and is set to be quite a bit larger than the actual Missile sprite. In addition, we have a collider that encompasses the explosion animation, making for a large exploding area that will nuke any enemies that happen to be in the area. Silly enemies.</p><p>Use the IEnumerator in a StartCoroutine() in the HomingMissile’s Start void to begin the countdown to ultimate destruction.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4d76c7fbdbd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unity Dev: Post Processing]]></title>
            <link>https://mrlovelies.medium.com/unity-dev-post-processing-884e2583bccc?source=rss-2e294212e4bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/884e2583bccc</guid>
            <category><![CDATA[gamedevhq]]></category>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[game-development]]></category>
            <dc:creator><![CDATA[Alex Somerville]]></dc:creator>
            <pubDate>Fri, 02 Apr 2021 13:03:41 GMT</pubDate>
            <atom:updated>2021-04-02T13:03:41.616Z</atom:updated>
            <content:encoded><![CDATA[<p>Everyone loves Instagram filters, right? Well why not have them on your video game?</p><p>Post-processing is the art of adding visual effects to your game to really add some atmosphere and identity to what you’re building.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tlB0VOcFQDAPeq64MITAiQ.png" /><figcaption>Before</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I6ySzXm0SCN2YKLo0sK9iA.png" /><figcaption>After — Bloom, Grain, Chromatic Abberation, Motion Blur</figcaption></figure><p>In the above pictures, you can see a subtle change on account of the post-processing effects that I’ve already. I won’t waste your time explaining what each one does, but instead help you walk through adding your own post-processing volume so that you can play around with them yourself!</p><ol><li>Go into your <strong>Package Manager</strong> and install the <strong>Post Processing</strong> package from the Unity Registry.</li><li>In your Hierarchy, right-click, <strong>3D Object &gt; Post-process Volume</strong></li><li>Open this new object in the <strong>Inspector </strong>and add a new layer called “Post Processing”. Don’t forget to set it as this Object’s layer once you’re done.</li><li>Check off “Is Global”, and then create a new Profile by clicking “New” next to the Profile property.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/339/1*1Kbz6mqBhfXGxR8mIJczmQ.png" /></figure><p>5. Now head over to your <strong>Main Camera </strong>object and add a new <strong>Post-process Layer</strong> component to it. Set the Layer to your newly made “Post Processing” layer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/337/1*BfY5kWiM7IXG4Yki7OdBUg.png" /></figure><p>6. Back on your <strong>Post-process Volume</strong> Game Object, you can now add, and see, a number of different visual effects. Play around with different combinations and custom values to find an effect that works for your game.</p><p>That’s it! Have fun with your post-processing explorations. Be careful, there <em>is </em>such a thing as “too much”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IRwbQsTdjEL4Qo2O2GhWqw.png" /><figcaption>My eyes!</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=884e2583bccc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unity Dev: STARS!]]></title>
            <link>https://mrlovelies.medium.com/unity-dev-stars-ce18d43dd55?source=rss-2e294212e4bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/ce18d43dd55</guid>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[gamedevhq]]></category>
            <category><![CDATA[game-development]]></category>
            <dc:creator><![CDATA[Alex Somerville]]></dc:creator>
            <pubDate>Thu, 01 Apr 2021 13:02:33 GMT</pubDate>
            <atom:updated>2021-04-01T13:02:33.900Z</atom:updated>
            <content:encoded><![CDATA[<p>Particles. Sweet, beautiful, generated particles.</p><p>We’re in space, so obviously we need some stars. We could do this by finding a repeatable transparent image of stars, but we already covered that in <a href="https://mrlovelies.medium.com/unity-dev-moving-backgrounds-b8e78f4a7d5e">Unity Dev: Moving Backgrounds</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R2jnQFRMQzAHQg2Tbckfjg.gif" /></figure><p>Having never used a particle system before, I did my research. Much of the code output here is from: <a href="https://guidohenkel.com/2018/05/endless_starfield_unity/">Guido Henkel’s Endless Scrolling Starfield</a> and this is primarily my attempt at explaining what’s going on here.</p><p>At the end of it all, we’ll have three objects that are all running off the same script so that we can create a parallax effect. For now, make an empty gameobject to act as the container for our starfields and one empty object for our first starfield.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/173/1*WdKLbkLeJ-sgHlejAa_UxQ.png" /></figure><p>Create a <strong>Particle System </strong>component on your first Starfield child object and turn off everything but the <strong>Renderer </strong>option. Expand the <strong>Renderer </strong>option and select “Default — Particle” for the Material. We’ll handle everything else with code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/330/1*mCu82D8L99mABSgu6XAUYw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/319/1*SHZeYaoKan0qeF8L0GELSQ.png" /></figure><p>Create a new C# script and attach it to your Starfield object. I called mine “Starfield” (Original, right?)</p><p>Let’s kick things off with some variables.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/379/1*z89Kb4E6EdLu4EQjTCFF0g.png" /></figure><p>All of these are pretty self explanatory. We’re giving ourselves some values that we can tweak in the Unity editor to fine tune our Starfield effect. By giving some thought to our expectations, we want to be able to adjust the number of stars, give them a slight variance in size, control the area that they’ll actually cover, and also, as a bonus, give them a slight red shift the further back they are (or in this case, the smaller they are since we don’t have true depth).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/696/1*p5izVk2UVQsv-BZmbemRMw.png" /></figure><p>This seems a bit complex, but let’s go step by step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/539/1*h0wMSx4Y730cw63ITQLjqQ.png" /></figure><p>We’re grabbing the transform of our background camera where we decided to “film” the background action from in my <a href="https://mrlovelies.medium.com/unity-dev-moving-backgrounds-b8e78f4a7d5e">Moving Backgrounds</a> article because we’re going to want a reference to it when we start to move these stars. Don’t forget to add a matching tag to this camera object! We then create a variable to store each particle that gets generated in a list using our _maxStars value. Next up, we store a reference to our <strong>Particle System</strong> component and then null check it with Assert.IsNotNull()</p><p>Finally, we create an offset for our x and y axes so that we can generate these stars from the middle of our particle object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*08AczbPwIDksPvKHJ7vIVg.png" /></figure><p>This section seems complex, but to put it very simply, we’re defining the parameters for each and every star in our Starfield in this for loop, giving them a location to appear, a size within our variance, and if they have that slight red colour shift or not. Then we put them in place with SetParticles()</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*L5j6uW_uXxh1_aR8YDEVhg.png" /></figure><p>This is a function we define and use in the bulk of the script. We’re defining a starting position for our stars by finding a random point in our Starfield based on the width and height we provided for it while using our offset to spread them out from the center of our rectangle.</p><p>At this point, you can now run your game and you should be seeing a field of stars. If it’s not showing up in your Game window, have a look in the Scene window. You may have to do some tweaking on the object’s position.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zSyE8TJkKqN9fYiGu9UeuQ.png" /><figcaption>Stars are pretty!</figcaption></figure><p>Great! We’ve got our stars generating but we want to give them some motion to really bring our world alive.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/933/1*lc6FUHuDp4x6INRSaeC5jw.png" /></figure><p>The general idea here is relatively easy. We’re going to continually shift the Starfield object to the left. As that’s happening, we’re going to loop over all the Particles (Stars) and track their positions individually. When a given star is outside of the camera view (camera position less the _xOffset ) then we’ll add our _fieldWidth value to our Star’s position, then subtract the Starfield’s position from the star’s position to move it to the right of the camera’s view.</p><blockquote>Note: You’ll need to add a new _speed variable to your script for the code above to work. My default is 0.5f for the speed.</blockquote><h3>Parallax</h3><p>For bonus points, duplicate your Starfield a couple times and start mucking around with the variables we exposed from the script.</p><p>In particular is the _speed variable. With multiple Starfield objects, you can create a parallax effect by having each Starfield set to a different speed. Keep in mind that the further back something is supposed to be, the slower it will go in relation to everything else.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ce18d43dd55" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unity Dev: Moving Backgrounds]]></title>
            <link>https://mrlovelies.medium.com/unity-dev-moving-backgrounds-b8e78f4a7d5e?source=rss-2e294212e4bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/b8e78f4a7d5e</guid>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[gamedevhq]]></category>
            <category><![CDATA[game-development]]></category>
            <dc:creator><![CDATA[Alex Somerville]]></dc:creator>
            <pubDate>Wed, 31 Mar 2021 13:01:17 GMT</pubDate>
            <atom:updated>2021-03-31T13:01:17.894Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve been feeling like our game environment has been a bit stale lately, which is why I switched up the background in my last article. It worked for me for maybe a day but it was <em>still</em> missing something. That something was movement. Motion creates emotion, as they say. So let’s get moving.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4eOnEpjsDlL_E9QZ8RHLrQ.gif" /></figure><p>After knocking my head around on the idea of taking this repeatable nebula image I’d found, duplicating it, and then moving each one after the other as they move across the camera… well, it seemed like a whole bunch of moving parts, so to speak. The less moving parts there are, the fewer issues we’re eventually going to run into when we have to start debugging.</p><p>At this point, it occurred to me that I could use my NebulaBG sprite as a texture. Textures, by default, repeat which is exactly what we’re going for.</p><p>That still begs the question, “How do I make it MOVE?”</p><h3>Setup</h3><p><strong>Sprite -&gt; Texture:<br></strong>Click on your background sprite in the Project view so that you can see its properties in the Inspector. Change the Texture Type to “Default”. Double-check that the Wrap Mode is set to “Repeat” and that’s it!</p><p><strong>Background Camera<br></strong>After a bit of research, I came across a tip that I found to be quite handy. You can create a secondary camera whose sole responsibility is to capture the things happening in the background of your game. This way, those objects aren’t cluttering up your Scene view when you’re trying to work with the more prominent objects. So long as your second camera has the same “Target Source” as your main camera, you’ll see both sets of objects in the same screen in your Game view.</p><p>Since this is a 2D game, we’re going to set our secondary camera’s projection as “Orthographic”, same as our Main Camera. Set the Depth of the secondary camera to be lower than your Main Camera or you’ll cover up your other game objects. Set the position of the secondary camera to (0,0,-10) and then arbitrarily move it up on the y-axis so that it’s out of the way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1010/1*XuhDypA8GcHUwc8VIESeAQ.png" /><figcaption>Two cameras</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TP71jEP7ozHRPT6_wogfGA.png" /><figcaption>One output</figcaption></figure><p><strong>Quad Creation<br></strong>Create a new<strong> 3D Object &gt; Quad </strong>in your Scene as a child of your secondary camera. It’ll look like a flat square at first. You’ll want to tweak the scale so that it covers the viewable area of the camera.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ifhCyslfXGL9vmsVqf1yxw.png" /></figure><p>Once this is done, your Game scene will look like your Player object and UI floating on top of white nothingness. This is your Quad. Find the sprite you converted to a texture earlier, and simply drag and drop it onto the Quad in the scene. Boom! You’ve got yourself a nice background again.</p><p><strong>Texture Motion<br></strong>Create a new C# script to attach to the Quad. I called mine “ScrollingBG”.</p><p>There’s only a couple steps to this script.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/627/1*p_yYoB6hEjNUkRkJdQG0QQ.png" /><figcaption>ScrollingBG.cs</figcaption></figure><p>We define a _speed variable so that the speed of the background scroll can be tweaked. This will also allow us to have multiple backgrounds that move at different speeds to create a parallax effect provided we’re using sprites with transparency in them.</p><p>Then we create a variable for the Renderer component of our object so that we can affect its material. GetComponent&lt;&gt;() as normal with a sensible null check for safety.</p><p>Moving into the void Update() we set the material’s mainTextureOffset to use our Time.time that has elapsed in the game to increase the offset as more time goes on. Our _speed variable lets us increase or decrease the effect as needed.</p><p>It’s honestly stunning how much of an improvement this <em>simple </em>addition makes. With the background moving, I’m immediately more engaged and immersed in the experience as a player.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b8e78f4a7d5e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unity Dev: Logical Animations]]></title>
            <link>https://mrlovelies.medium.com/unity-dev-logical-animations-9e391068728b?source=rss-2e294212e4bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/9e391068728b</guid>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[gamedevhq]]></category>
            <category><![CDATA[game-development]]></category>
            <dc:creator><![CDATA[Alex Somerville]]></dc:creator>
            <pubDate>Tue, 30 Mar 2021 13:03:51 GMT</pubDate>
            <atom:updated>2021-03-30T13:03:51.122Z</atom:updated>
            <content:encoded><![CDATA[<p>Previously we’ve set up animations for our Powerup objects, but they’re just a set of sprites animating in a never-ending loop. That’s obviously not going to work for everything we want to animate.</p><p>For example… let’s blow up some alien ships.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HqMLTPNjbEmI3Rp2U1h92w.gif" /><figcaption>Ka-BOOM!</figcaption></figure><h3>Thinking it Through</h3><ol><li>We want the alien ships to explode only when they’re hit by a laser or are hit by our Player.</li><li>We don’t want this animation to loop endlessly.</li></ol><p>And… that’s it really. So let’s take a look and see how that’s done.</p><p>Double-click your Enemy prefab to open it up in your Scene window. Find your “Animation” window and hit “Create”.</p><blockquote>Note: You’ll want to be sure that you’re adding the animation to the object that your Enemy’s Sprite Renderer component are on. I have my Enemy object as a parent to another object called “Enemy_Sprite” which is where the Sprite Renderer lives.</blockquote><p>This will prompt you to save the animation controller and accompanying animation clip so do so in your <strong>Assets &gt; Animations</strong> folder and call it “Enemy_Destroyed_anim” (or whatever you like, I’m not your boss).</p><p>I have a collection of sprites to use for this animation, so find yours, select them all and dump them into the Animation window.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pV_eKLVA8GfyMxYXZpCYNg.png" /><figcaption>Frame by Frame Explosion</figcaption></figure><p>Now we’ve got our animation, but if you run your game you’ll just see progressively more and more infinitely repeating explosions flying across your screen which is not… exactly what we want, though it is kind of fun.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WeLLqC0DSesssBGgxiXKBw.gif" /></figure><p>First thing’s first, let’s stop the looping animation. Open up your <strong>Animations</strong> folder and view your animation clip in the Inspector. It’s the one that has an icon that looks like a triangle going really fast.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/345/1*uubAJz2TcEmaVLeP-RccVw.png" /></figure><p>From here, simply untick “Loop Time” and that’ll stop the animation from looping.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/275/1*6Qc3GbKFzx3C9LkPVPCEWw.png" /></figure><p>Next up, we want to only trigger this animation when a laser or our ship collides with the Enemy. Double-click the animation controller whose icon looks like 2 squares playing an eternal game of tag, and you’ll see something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/677/1*IXNxecbDTqtlYxeT-vR5Zg.png" /></figure><p>This is the place where we can add logic to our animations. As it is, the entry point will drop you directly into the Enemy_Destroyed_anim node (which represents our <strong>Animation Clip</strong>), play the clip, and then be done because we turned off Loop Time.</p><p>First step is to create the idea of triggers. Open the <strong>Parameters</strong> tab and use the <strong>+ </strong>symbol to create a new Trigger called OnEnemyDeath</p><p>Create a new Empty state by right-clicking anywhere in this area and then right click on the new node and choose “Set as Layer Default State”. Now our enemies won’t jump directly to their explosion animation, but we still need to add our logic in.</p><p>Right-click the Empty node again and “Make Transition”. Attach the transition arrow to “Enemy_Destroyed_anim” by clicking on that node.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Pt6hUCYvxNraOFS75S1blg.png" /></figure><p>Clicking on the white arrow representing the transition between “Empty” and “Enemy_Destroyed_anim” will give us access to some settings in the Inspector that we want to tweak.</p><p>Check <strong>off</strong> “Has Exit Time” and change the value of “Transition Duration” to 0. This will ensure that, as soon as our Trigger is activated, we’ll go right into the explosion animation. If this weren’t done, we’d have an awkward moment where we hit the enemy with a laser… and then it takes a couple seconds to realize it and only then proceeds to go into an overly dramatized Shakespearean death scene.</p><p>Finally, add a Condition. Since we’ve only established the one Parameter, it’ll auto-populate.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/272/1*QuquEgmEs6uE2Uc9RE1y_g.png" /></figure><h3>Time for Code</h3><p>Begin by getting a reference to your Animator component in your Enemy script.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/167/1*biOQFYhxdf9GlLRp8KmoIQ.png" /><figcaption>Enemy.cs</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*0iiaEbeV6_EEfzCZVVmgsw.png" /><figcaption>Null check, null check, null check</figcaption></figure><blockquote>Note: I use GetComponentInChildren to fetch my Animator component because my Sprite Renderer, and now Animator, components are on a child object to my Enemy object.</blockquote><p>It’s as simple as adding _anim.SetTrigger(“OnEnemyDeath”); where it’s needed.</p><p>One last important thing. We’ve done all this great work, and if you were to run your game, you’d see absolutely <em>none </em>of it. As soon as we hit an Enemy with our Laser, we Destroy() that Game Object and with it goes the Animator we just lovingly crafted.</p><p>Have no fear, it’s a quick fix. The Destroy() method accepts an addition, optional argument which will dictate how long after it’s called to actually destroy the object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*o5l35WKoa4FFLR_yF3dYEw.png" /><figcaption>Enemy.cs::OnTriggerEnter2d</figcaption></figure><p>Happy exploding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9e391068728b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unity Dev: Delegates & Events]]></title>
            <link>https://mrlovelies.medium.com/unity-dev-delegates-events-2235798161ce?source=rss-2e294212e4bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/2235798161ce</guid>
            <category><![CDATA[gamedevhq]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[unity]]></category>
            <dc:creator><![CDATA[Alex Somerville]]></dc:creator>
            <pubDate>Mon, 29 Mar 2021 13:01:10 GMT</pubDate>
            <atom:updated>2021-03-29T13:01:10.102Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CiHDS6Bv3b7wIfS_MPYrSQ.png" /></figure><p>We’re going to take a short break from adding new features to our game to clean up some of the existing code. When we set up the Restart functionality with our refactor of the new Input System implementation, we made note that we created an OnPlayerDeath() function in 3 different components so far.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/249/1*Gbz6BLuJ6m4z_H5V-DMDeg.png" /><figcaption>Player.cs</figcaption></figure><p>Seems like a lot of repetition and it puts a lot of responsibility on the Player object. We’re manually running multiple functions from different components at a certain point that just happens to be when the player has lost all their lives. Wouldn’t it be great if there was a system that we could make that would run these functions <em>because </em>the player died? Yes, yes it would, and that’s where delegates and events come in.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/338/1*RmM3RCM7GumHGrDHgUrUJQ.png" /><figcaption>Player.cs</figcaption></figure><p>This is a basic implementation of a delegate and an event. When breaking down what we’re attempting to do here, it’s good to have at least a conceptual understanding of what events and delegates do.</p><p>A <strong>delegate </strong>can be thought of as a variable that stores a series of methods that we want to run whenever something specific happens.</p><p>An <strong>event </strong>is basically the trigger that tells our delegate to run all the methods that are subscribed to it. We can set a series of functions from different components to subscribe to an event.</p><p>So we’ve got our Player object creating a delegate and an event. Fantastic! Now we need have our components subscribe to this delegate with their functions. We’ve got a handy reference for where we need to subscribe in our Damage() method in Player.cs.</p><p>SpawnManager, UIManager, and InputManager all call their own OnPlayerDeath() functions from here. We want to subscribe to our event in each of these components. Using OnEnable() , we add our component’s OnPlayerDeath() method to the event. We’ll also unsubscribe from them in OnDisable() to be tidy and ensure we’re safeguarding against weird bugs later.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/342/1*J0pmLlzhqNMjwgUb29FuFQ.png" /></figure><p>We’ve successfully created a delegate with a coordinating event and give it a different function from each of our 3 components that are going to be affected when our player dies. Now we just need to actually trigger the event.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/208/1*e9QK2f7Uia27QRRaNO0FPg.png" /><figcaption>Player.cs</figcaption></figure><p>The player dies when their _lives variable is less than 1, so this is a fine spot to trigger our onPlayerDeath event. Delete the 3 lines calling each individual function and replace it like in the image above. The question mark is called a null conditional operator. This syntax will null check the onPlayerDeath event and then Invoke (or trigger) the event. Your 3 components are actively listening for this to happen, and when it does, they’ll fire off their subscribed methods accordingly.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2235798161ce" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unity Dev: Multiple Action Maps (New Input System)]]></title>
            <link>https://mrlovelies.medium.com/unity-dev-multiple-action-maps-new-input-system-64b1eeece7e8?source=rss-2e294212e4bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/64b1eeece7e8</guid>
            <category><![CDATA[gamedevhq]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[unity]]></category>
            <dc:creator><![CDATA[Alex Somerville]]></dc:creator>
            <pubDate>Sun, 28 Mar 2021 13:02:21 GMT</pubDate>
            <atom:updated>2021-03-28T15:12:14.448Z</atom:updated>
            <content:encoded><![CDATA[<p>In yesterday’s article, we added some UI to direct the player to press the “Enter” key or the Start button on a gamepad to restart the game when they’ve died. Wait though… our Input Actions are tied to our Player object and when we die, we destroy it! So how do we continue providing input?</p><h3>Refactoring OnMove() and OnFire()</h3><p>Let’s begin by making an empty Game Object called “Input_Manager”. Add a coordinating C# script “InputManager” and attach it to the object. Create a new Player Input component on the object as well and set its Behaviour to “Invoke Unity Events”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/342/1*Rxsp0AhBWmNkcD3vUx0zCg.png" /></figure><p>The general idea here, is that we’re going to add another Action Map dedicated to controls that can be used when the Player is dead. To do this, we’re going to shift the responsibility of knowing which Action Map to use over to a persistent Game Object. You can go ahead and delete the Player Input component on your Player object as well.</p><p>Let’s define our new Action Map. In your InputActions folder, double-click you “Controls” item to open the Input Actions editor and add a new Action Map called “Death Menu”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*aoRY2dToJxhw70LcFnYUYw.png" /></figure><p>Now, here’s the really cool thing about this refactoring of our control systems. We don’t even need to modify our code to keep our Movement and Firing mechanisms working. Expand the <strong>Events &gt; Player</strong> in the Player Input component on your Input_Manager, use the + button under “Move” and “Fire” to create new events. Drag your Player object into these, and select the OnMove and OnFire from the Player list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/340/1*ALSYJMTS5Au1K6Rz7mvhAg.png" /></figure><p>Run your game and everything should work exactly the way it did before. This is great, because we obviously don’t want our existing functionality breaking in an attempt to add new functionality.</p><h3>Switching Action Maps</h3><p>Now, when our player dies, we show a screen saying “Game Over” and, more importantly “Enter/Start to Restart”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_69t90-ZCn0C2WDeRtk-oQ.png" /><figcaption>Apparently I’m not very good at this game</figcaption></figure><p>We’ve already got our “Death Menu” Action Map for just such an occasion. There’s a couple steps involved in making this happen.</p><ol><li>Load up a reference to our InputManager in Player.cs (use GameObject.Find()or GameObject.FindWithTag() and then GetComponent&lt;&gt;() ) and null check!</li><li>Load a reference to our Player Input component on our InputManager in InputManager.cs with GetComponent&lt;&gt;() . Null check null check null check.</li><li>Use the PlayerInput component to switch Action Maps when the player dies.</li><li>Make a void OnRestart(InputAction.CallbackContext context) inside of our UIManager.cs. This is where we’ll run our scripts to restart the game.</li><li>Setup the Player Input component on the Input_Manager object to use the Restart function from UIManager in <strong>Events &gt; Death Menu</strong>.</li></ol><p>Step 3 is the one we’re mostly unfamiliar with in this case, so let’s have a look at that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/658/1*ka7WjxItNq5nI9vvvE90gA.png" /><figcaption>InputManager.cs</figcaption></figure><p>We’ve created another void OnPlayerDeath() for the Player.cs to call from our InputManager. We’ve referenced our Player Input component inside of _playerInput and proceed to set the currentActionMap by using the FindActionMap() method which accepts a string. In this case, the name of our new Action Map is “Death Menu”.</p><p>After establishing your reference to the InputManager in your Player script, you can call its OnPlayerDeath() function when the player dies.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/251/1*iRxznLxx38WzjmOShnyrxw.png" /><figcaption>Player.cs, public void Damage()</figcaption></figure><p>Play your game. When you run out of lives, you’ll see your GAME OVER screen, but behind the scenes, your InputManager will switch over to using the “Death Menu” Action Map, allowing the player to restart the game and go again!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=64b1eeece7e8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unity Dev: Basic UI]]></title>
            <link>https://mrlovelies.medium.com/unity-dev-basic-ui-7ed52519bf5b?source=rss-2e294212e4bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/7ed52519bf5b</guid>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[gamedevhq]]></category>
            <dc:creator><![CDATA[Alex Somerville]]></dc:creator>
            <pubDate>Sat, 27 Mar 2021 17:23:40 GMT</pubDate>
            <atom:updated>2021-03-27T17:23:40.624Z</atom:updated>
            <content:encoded><![CDATA[<p>These days, it’s up in the air whether players even want a user interface. A number of games include the option to completely hide the UI of games to increase immersion. That said, we’re going to create a simple display to show our player’s score and the lives they’ve got left.</p><blockquote>Note: If you’ve been following along, you’ll notice that the background is substantially different than what I was previously using. It was done by simply swapping out the image. I was also playing around with a Particle System, which I’ll do a write up on as soon as I’ve got a better handle on it.</blockquote><p>Our first step, as always, is planning. So what information do we want to provide our player to help them along the way? Let’s consider a few options.</p><ol><li>What’s the fun in a game if we can’t brag to our friends about how well we’re doing? Each enemy ship kill generates some points so we should give that some screen real estate. <strong>Score Display</strong></li><li>Our Player can only take so much damage. There’s no God Mode (yet), so we should make an indicator letting the player know how many lives they have left. <strong>Lives Display</strong></li><li>So there’s no God Mode. That must mean there’s a Game Over scenario, which will happen when our lives reach 0. <strong>Game Over Screen</strong></li><li>If the game ends, we need some way of restarting it so that our player can try to beat their high score. We can use our UI to inform them of how to trigger a restart. <strong>Restart Screen</strong></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vu8pybMo35M2q44LVgRkfA.gif" /></figure><p>We’ll cover how the <strong>Score Display</strong> here which should give you enough context to set up the others.</p><h3>Score Display</h3><p>To start your UI, create a Text element from your object creation menu. This will create an object called “Canvas” and this is where all your UI elements are going to be stored. In addition, a “Text” object will be created which you can call “Score_text” and play with the configurations for different fonts, colours, and general text editing options.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/342/1*EUT-WZ1TT7bUoGzQhVaD3Q.png" /></figure><p>Next up, create an empty object called “UI_Manager” and a coordinating C# script called “UIManager”. Attach the script to the object and open it up. We’ll be using this script to, well, manage our UI items.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/294/1*TGGFIgW9z2yRLrkbHQGQ7g.png" /></figure><p>Start of simple by making a variable for your new Text object and then attach “Score_text” to the UI_Manager object in the inspector. In void Start() we want to default the text to read “Score: 0”, so we’ll add a line to do just this using <a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated">string interpolation</a>. The string interpolation isn’t really necessary for this bit, but I like to do it anyway to highlight the fact that the 0 is the section of the text that is going to be changing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/230/1*knOfNuZIeI44tdE3f6McNg.png" /></figure><p>Finally, we create a function in the UIManager to update the text of the score as it changes. We’ll let it accept a integer value as an argument which will be the Player’s total score that we’ll be receiving</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/334/1*7RqufeSULuzplVaiq6hPLQ.png" /></figure><p><strong>Player.cs<br></strong>Inside of your Player script, we need to create a reference to our UIManager so that we can communicate with it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/212/1*C-CDp8FXssg_wRYE7DnKBw.png" /><figcaption>Don’t forget to populate this variable using GetComponent&lt;&gt; and null check it!</figcaption></figure><p>From here, we’ve got a couple functions in our Player script.</p><p>EnemyKill() is called from the Enemy script whenever a Laser collides with an Enemy object. It then creates a random int between 10 and 20. This int is then passed to our AddScore() function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/313/1*ueAIbFCosprxdhp6b5P7SQ.png" /></figure><p>AddScore() takes the argument it’s been passed and adds it to our global _score variable. The next line then asks our UIManager to run it’s UpdateScore() function and gives it the newly updated _score value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/257/1*2PTmPWoajwQb_amqR2AAMg.png" /></figure><p>And that’s it! If you run your game, you’ll now see that your UI updates accordingly whenever you destroy an enemy ship.</p><h3>More UI</h3><p>Now that you’ve got your UIManager set up, how can you use it to establish the other elements you need to show your player? How can you swap out images to create a Lives counter? How can you use existing code to show the Game Over text when our Player _lives == 0 ?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ed52519bf5b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unity Dev: Powerups]]></title>
            <link>https://mrlovelies.medium.com/unity-dev-powerups-eb5f26934820?source=rss-2e294212e4bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb5f26934820</guid>
            <category><![CDATA[learning]]></category>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[unity3d]]></category>
            <dc:creator><![CDATA[Alex Somerville]]></dc:creator>
            <pubDate>Fri, 26 Mar 2021 13:03:13 GMT</pubDate>
            <atom:updated>2021-03-26T13:03:13.396Z</atom:updated>
            <content:encoded><![CDATA[<p>There once was a little ship that shot little lasers. The ship was happy for some time until it realized it could be… more. Welcome to powerups.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q2ESuCSHA_wewD8xII363w.png" /></figure><p><strong>Triple Shot:</strong> Is one laser ever enough? Of course not. We need a powerup to give us 3 lasers at once for a little bit.</p><p><strong>Speed Boost: </strong>If you move faster, you kill more alien ships. So why not?</p><p><strong>Shield: </strong>It’s like an extra life for free! On top of that, it gives us an excuse for another cool visual.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RPMfPBmbAqw5uoXbQZcE5g.gif" /></figure><h3>Thought Process</h3><p>There’s two parts to a powerup system from my perspective. There’s the “physical” power-up that the Player is going to collect, and then there’s the actual effects that the powerup provides to the Player.</p><h3>Initial Setup</h3><p>First up is creating the items that are going to be collected. We’ll focus on how to do this for the Triple Shot powerup, but the same principles apply for them all. Once again, I’m using some handy assets from the program I’m doing with <a href="https://gamedevhq.com/">GameDevHQ</a>. The first frame of each sprite animation set will serve as the basis of the object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/393/1*hevoJF-G9vi9bC8XUhtpvA.png" /></figure><p>Drag the image into your scene and rename the object to “Triple_Shot_Powerup”. Create a new C# script for your power-ups (we’re going to use the same one for all three) called Powerup.cs and attach this to your new object. The Powerup object will also require a Box Collider 2D component set as a Trigger.</p><blockquote>Note: So far, only our Enemy objects have a Rigidbody 2D because they were the main object our Player and Laser objects would interact with. However, now we need the Player to be able to pick all these items up, so you’ll want to apply a Rigidbody 2D component to the Player object. As always, don’t forget to turn gravity off! We’re in space, after all.</blockquote><p>Once you’re done the legwork with the Triple_Shot_Powerup item, drop it into your Prefabs folder so that we can spawn it into the scene.</p><p>Some quick work in the Powerup.cs will get item moving across the screen while also ensuring it gets cleaned up from our Scene once it’s left the viewable area.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*1ex8-Q8m2r0G6ghPtS2-nw.png" /><figcaption>Remember to have a [SerializeField] with your _speed variable so your game designer’s can tweak the speed of the Powerup objects</figcaption></figure><p>I already <em>know</em> that I’m adding additional Powerup types so we’ll save ourselves some time by coding the Powerup script in a modular fashion.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/714/1*X75DOQARwN6SoIgnnNpndw.png" /></figure><p>Introducing a Powerup ID variable will help us when it comes to activating the appropriate powers on our Player. We’ll be creating a list variable with all of our Powerup prefabs in it which acts as an array and will be zero indexed. This means, basically, that we’ll start counting our items from 0 instead of 1. Your Triple_Shot_Powerup will have an ID of 0.</p><h3>Pickin’ Up the Power-up</h3><p>Since we’re already in our Powerup script, let’s get it so our Player can pick up the powerups.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/627/1*ReF9ouHOkPKoSNvuT409vw.png" /><figcaption>This is my final version of this script. You can use this as a reference, but it’s important to remember that you’ll be seeing errors in your IDE and your game won’t run if you don’t have coordinating functions established in your Player.cs</figcaption></figure><p>We’ve seen the OnTriggerEnter2D(Collider2D other) void before so we already know that other is going to give us information about the Object that’s colliding with us. In this case, we want to run some code when an object with the Tag of “Player” hits us (by “us”, I’m referring to a Powerup prefab. We’re in their script, so we can think of it as “us” when thinking through our coding).</p><p>First up, we’re stashing our Player component in a local variable and null checking it to help figure out what’s going wrong if our code doesn’t work.</p><p>Now you’ll see a new concept, which might seem scary at first glance, but it’s <em>basically </em>an if statement. You can read more about switch statements <a href="https://www.tutorialsteacher.com/csharp/csharp-switch">here</a>.</p><p>When a Player collides with a Powerup, it’s going to have an ID attached to it. In this case, the ID will be 0 because we haven’t set up the other Powerups yet. This means that our code is going to dive into the Player component we stored, and run the ActivateTripleShot() function that we… still need to define! Let’s get going on that.</p><h3>Making the Triple Shot</h3><p>A Triple Shot is… exactly what it sounds like. We’re going to fire three lasers at the same time while our Powerup is in effect.</p><p>Drag 3 laser prefabs into your scene under a blank object called “Triple_Shot”. Organize them in such a way that it looks good coming out of your ship, then drag the Triple_Shot object into your prefabs. Once we get them firing, you may need to tweak the positioning and you can do that by double-clicking the prefab and making your adjustments.</p><p>In Player.cs we’ll need a couple variables to make this Powerup.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/435/1*6wcxdQFE6bZ0fgt2n_3uDQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/468/1*4KKJPsxbJIhWB6OpgCoRcw.png" /></figure><p>We need a reference to the new Prefab we’ll be firing, we need to know if the Triple Shot is active, and we need to know how long it’s going to last.</p><p>In your Unity editor in the Player inspector, populate the variable with your Triple_Shot prefab and we’ll be good to continue.</p><p>We were already referencing player.ActivateTripleShot() in our Powerup.cs so we need to create that. From there, the Triple Shot activation really only requires 2 things. Tell _isTripleShotActive to be true, and then begin a Coroutine that will turn it off after the _tripleShotDuration time is over.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*grGRRMYRvVhbRB0JTyWp4g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/466/1*yHz0yWmXRlUJMT7KOa_bgA.png" /></figure><p>Now, we could simply modify our InstantiateLaser() script to use a simple if statement, but I wanted to make it a little more open ended to introducing othering Laser variations later on. I modified InstantiateLaser() to accept an argument of the prefab I want it to Instantiate.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D_4PTAXHPrgOnvtgsDkgaQ.png" /></figure><p>From there, I modified our OnFire() event with an if statement to fire the Triple_Shot prefab if _isTripleShotActive is true.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*jPb2537COOPr22G95Wqx3w.png" /></figure><p>Now you can get on with the Speed Boost and Shield powerups. What do you think the code for them would look like? What variables do you need to change, or prevent from changing, to emulate their effects?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb5f26934820" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>