<?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 Christian Fredericksen on Medium]]></title>
        <description><![CDATA[Stories by Christian Fredericksen on Medium]]></description>
        <link>https://medium.com/@christian.fredericksen?source=rss-16897956646d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by Christian Fredericksen on Medium</title>
            <link>https://medium.com/@christian.fredericksen?source=rss-16897956646d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 12:16:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@christian.fredericksen/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[Animating Sprites in Unity]]></title>
            <link>https://medium.com/@christian.fredericksen/animating-sprites-in-unity-dff72d8e2aee?source=rss-16897956646d------2</link>
            <guid isPermaLink="false">https://medium.com/p/dff72d8e2aee</guid>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[unity-game-development]]></category>
            <category><![CDATA[unity3d]]></category>
            <dc:creator><![CDATA[Christian Fredericksen]]></dc:creator>
            <pubDate>Sat, 06 May 2023 22:40:58 GMT</pubDate>
            <atom:updated>2023-05-06T22:40:58.200Z</atom:updated>
            <content:encoded><![CDATA[<p>Let’s animate our Triple Shot Powerup sprite</p><p>Animations in Unity are really cool. They add another layer of ‘awesome’ to our games that just make them more visually fun. And they’re really easy to use.</p><p>On the left side of the Gif we have our sprite sheet. This is all the individual frames of animation for our Triple Shot Powerup. As we scroll through these on the left, we can see what this animation looks like on the right.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1023/1*bNfTwK3oJ5OKReoT1n44ZQ.gif" /></figure><p>So, let’s dive in. The first thing we want to do is select the game object we want to animate. Then click on Window &lt;&lt; Animation &lt;&lt; Animation. Then dock this panel next to console.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3PSe0-LUTgCiPFCGDpjSXQ.gif" /></figure><p>In this new animation panel there’s a dialogue box with a create button. Click on that and a file window opens up. We’ll create a new folder calling it Animations and we’ll save this New Animation as Triple_Shot_Powerup_anim.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1021/1*tzRbt65i1g1SmIU_gDZRUA.gif" /></figure><p>When we save this Unity opens up what is called a dope sheet that allows us to create key frame animations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1019/1*WqnxHDRcQl0yDIoe9hBYeA.png" /></figure><p>Make sure you have the game object selected. Then in the Animation panel click on the record button. Then in the Triple Shot sprite folder highlight the all the frames that make up this animation and drag them into the dope sheet. Once there you can click the play button and see in the Scene View that the Triple Shot Powerup is a little more flashy than it was.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l_6gB3BvxjybeR_xbPHpgQ.gif" /></figure><p>Click the record button again to stop recording. When we stop recording a couple things happen. In our Animations folder we have our Triple_Shot_Powerup_anim as well as the Triple_Shot_Powerup controller. This controller is what handles the logic for playing the animation. In more complex animations we’ll have a controller handling the logic for multiple animations. Like a character controller that has several attack combinations will have an animation for each one. The controller will control all of them.</p><p>Selecting the Triple_Shot_Powerup game object you’ll notice in the Hierarchy panel that Unity adds an Animator component for us and already has the controller added to it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/454/1*P6ncHJXjInSJiYoMrPdJog.png" /></figure><p>What does the controller look like? Double click on it and see. It opens up the Animator Window. The green box is the entry point and the orange box is our animation clip.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1019/1*rWBGM2i1aqdWAhWFZC4Tmg.png" /></figure><p>Run the game and you can see that we have successfully added animation to our Powerup.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1019/1*mO0Sbem5Prq1XS_R0vi2_g.gif" /></figure><p>Until next time…</p><p>Code your imagination to life.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dff72d8e2aee" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Starting to Feel Like a Real Video Game]]></title>
            <link>https://medium.com/@christian.fredericksen/starting-to-feel-like-a-real-video-game-e48502b73e8e?source=rss-16897956646d------2</link>
            <guid isPermaLink="false">https://medium.com/p/e48502b73e8e</guid>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[unity-game-development]]></category>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[game-development]]></category>
            <dc:creator><![CDATA[Christian Fredericksen]]></dc:creator>
            <pubDate>Fri, 05 May 2023 02:30:10 GMT</pubDate>
            <atom:updated>2023-05-05T02:30:10.717Z</atom:updated>
            <content:encoded><![CDATA[<p>Add a Triple Shot powerup to the game</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/883/1*Prm6wK9CAx0B_xGdLSxjSw.gif" /></figure><p>We’ve come a long way in just a little while. When we first started this little adventure we didn’t know much and our product was very primitive. Do you remember when things looked like this?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/721/0*zLK6kHn0aEphWXNO.gif" /></figure><p>In this article we are going to cover a lot of territory. At this point we have a single laser. Let’s add a power-up that will temporarily give us a Triple Shot. We want to first create a Triple Shot prefab. We do that by dragging the Laser prefab into the Hierarchy panel and duplicating it twice. Then using the Rect Tool we can relocate these lasers into position. Make sure the lasers’ X position are opposite each other. If one is at 0.8 the other should be -0.8 and they should both be on the same Y position. Z will be 0.0.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*KCCUm2cllV6ifwHtzZW5mw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/1*GcOHqsFuBM1U6SL10U6yYw.gif" /></figure><p>Now that we have this set let’s group them all into a parent object. Create an empty object called Triple Shot and add these 3 lasers to it. This will be a <strong>parent object</strong>. That will be important to know later. It will be far simpler to instantiate this prefab once than it will to call the Laser prefab 3 times.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*w3srNdiUGUol8MdCE6TaHg.gif" /></figure><p>Now, drag the Triple Shot into the prefab folder. And we’re done. Now let’s add some logic to our code to instantiate the Triple Shot prefab. When the game starts we don’t have a Triple Shot and therefore it isn’t enabled. We take care of this in the FireLaser() method in the Player script. First, create a private bool variable and set it to false.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/644/1*jgONdKo3JKw9hELiTHHYyw.png" /></figure><p>Then in the FireLaser() method we include an if condition to see if the Triple Shot is enabled. If it is we will instantiate the _tripleShotPrefab at the player’s position with no concern for the rotation, else we will instantiate the _laserPrefab with the logic that is already there.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g9GdWN7Pk2lxLzk9KDTAHg.png" /></figure><p>Be sure to attach the Triple Shot Prefab to the player script:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/595/1*WOJGtKv3oSqGvuK3xBMI1Q.png" /></figure><p>From here you can run the game and toggle Triple Shot Enabled from the inspector and see it work.</p><p>We have that working so let’s actually add the Power-up icon to our game so we can collect it and activate this feature properly. In the Sprites folder find the Power_Ups folder then the Triple_Shot folder and take the first image and drag it into the Hieracrhy. Rename it Triple Shot and scale it down to 0.5.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/593/1*MtPM2-w2tJepLIYpALvogw.png" /></figure><p>Set the sorting layer to Foreground:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/593/1*RB28cyu1lWVcA099aahMSw.png" /></figure><p>Add a 2D box collider with Is Trigger checked and a 2D Rigidbody with Gravity Scale set to 0 so it doesn’t fall.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*2BKta4K7dx94MsNsyMI3Qg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/603/1*pCsKpnYGfpG4kc4L2uaI6w.png" /></figure><p>Finally create a new C# script called Powerup and drag that onto the Triple Shot Powerup inspector panel. There. All the set up is complete. On to the scripting logic.</p><p>In the Powerup script we’ll assign a _speed variable. And then use that variable to define the powerup’s movement down the screen. If the player fails to collect the powerup and it runs off the screen at the bottom, destroy the powerup.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/365/1*30MwDvhPRH56Ck7uIGOwUw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/762/1*pDcTifPB--lqnlwBh8SIQw.png" /></figure><p>Now, when the player collects the Triple Shot powerup we need two things in the Player script. A method to enable the Triple Shot and a coroutine to shut it off after 5 seconds. Those two things look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/623/1*73jrFAJTLy4iiJ1eeFrT_g.png" /></figure><p>When the TripleShotActivate() method is called it will reset _tripleShotEnabled to true. It will then start the coroutine that will set it back to false after 5 seconds. That is defined in the IEnumerator.</p><p>Now to make all this work, back in the Powerup script we need logic to activate when the Player collides with the Powerup icon. When the powerup detects a trigger it will look for the tag “Player”. If it finds that tag it will get the Player component and check for null. If the player is not null it will start the TripleShotActivate() method and destroy the icon simulating the illusion that it has been collected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/693/1*8c-Cww3ipCOCoxNIXWqQMA.png" /></figure><p>Make sure all your changes in all your scripts are saved and then try it out in Unity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/883/1*Ia44qsfKHBmZr6twP1hmsg.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*sbvjmsNRR4FZrQMcZIkxwA.png" /></figure><p>AWESOME!! <br>When the player collides with the power-up they gain the Triple Shot for 5 seconds before returning to a single laser shot. But notice in the Hierarchy all those clones still hanging out. Let’s get rid of those with this bit of code in our Laser script.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/778/1*YK-2tXBqe25nL1YnvAiOMA.png" /></figure><p>Here we are already checking the single laser position to see if it runs off screen. But we add the second if conditional to check for a parent object which, if you remember from the beginning of this article, is what our Triple Shot prefab is. If transform.parent is not null it will destroy the parent gameObject. And clean up the stragglers in the Hierarchy.</p><p>Whew! That was a lot of ground we covered. But you know what we’ve got? A game that is starting to look like a real game.</p><p>Until next time…</p><p>Code your imagination to life.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e48502b73e8e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Benefits of Prototyping Without Assets]]></title>
            <link>https://medium.com/@christian.fredericksen/the-benefits-of-prototyping-without-assets-eb350920c858?source=rss-16897956646d------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb350920c858</guid>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[unity-game-development]]></category>
            <category><![CDATA[csharp-programming]]></category>
            <category><![CDATA[game-development]]></category>
            <dc:creator><![CDATA[Christian Fredericksen]]></dc:creator>
            <pubDate>Fri, 21 Apr 2023 21:53:22 GMT</pubDate>
            <atom:updated>2023-04-21T21:53:22.397Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*0yGoWroEdjP6NwRiK5dYEw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/883/1*RMqUDVS4R8txXfRWE5Vp4w.png" /><figcaption>From prototype to production</figcaption></figure><p>Hey, friends. Today we’re going to discuss why prototyping your game with primitive objects is such a good idea. It’s real easy to get caught up in the fantasy in our heads about what our game will look like when it’s done. “I want a fighter ship that…”, or “The background should look like…”, etc. A fellow software engineer used to tell me all the time, “Just make the car run. Then add the paint.” That is so true of our game programming.</p><p>Imagine how much time creating your own assets would take. Or imagine what it would cost if you were to hire someone to create the assets for you.</p><p>Now, add to both those “what if’s” the possibility that your game is unstable and crashes or it’s just plain boring to play. So much time and money wasted creating form before function. Neither we nor the studio we may be working for would appreciate that very much. Especially the latter. Bosses *hate* spending money they can’t recoup.</p><p>Prototyping your game with primitive game objects is the better way to approach game development. This let’s us focus on the function part of our game. How it plays, in the early stages is far more important than how it looks. If the game is buggy, or it crashes, or it isn’t a fun experience while using primitives then no amount of “paint” is gonna make any of those things palatable. Having a smooth and entertaining game without expensive assets will go a long way in deciding to invest more time and/or money to zhuzh it up and take it to the production level.</p><p>Equally important is knowing *when* to move from primitive game objects the beautifying assets that are going to make your game really stand out. We built this game in a 3D setting because it’s a bit easier to develop this was. But as we have recently seen, there are a few steps we have to take to convert things from 3D to 2D. If we were to wait *too* long to add our assets, those few steps could multiply exponentially and then changing everything over to 2D could be a tremendous chore.</p><p>There is no one-size-fits-all rule to know when to implement finished game assets. It’s going to be different from one project to the next. But there is an OSFA consensus that prototyping without assets is the best approach.</p><p>Until next time…</p><p>Code your imagination to life.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb350920c858" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[From Prototype to Work of Art]]></title>
            <link>https://medium.com/@christian.fredericksen/from-prototype-to-work-of-art-fa1e566134df?source=rss-16897956646d------2</link>
            <guid isPermaLink="false">https://medium.com/p/fa1e566134df</guid>
            <category><![CDATA[unity-game-development]]></category>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[csharp-programming]]></category>
            <category><![CDATA[gamedevhq]]></category>
            <category><![CDATA[csharp]]></category>
            <dc:creator><![CDATA[Christian Fredericksen]]></dc:creator>
            <pubDate>Sun, 19 Mar 2023 22:57:40 GMT</pubDate>
            <atom:updated>2023-03-19T23:02:27.575Z</atom:updated>
            <content:encoded><![CDATA[<p>Objective: To make the archaic look arcade</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/437/1*tsTGzSzHrBv90N6qGclv_w.gif" /></figure><p>Bravo! Not bad. We have some really good bones to our game. How ‘bout we add a little flesh and blood to it, now? We are now going to move from prototyping mode to production mode.</p><p>This article is going to assume that you already have a membership to <strong>Filebase</strong>. This is the assets library we will be using to add some hi-def pizzaz to our game. If you don’t have <strong>Filebase</strong>, follow this <a href="https://medium.com/@christian.fredericksen/assets-assets-get-your-assets-here-afbb94ec1b69">link</a> for download and install instructions.</p><p>And now that you are all set up, on with the show. You’ll notice in the menu bar of your Unity project you will see a new tab for <strong>Filebase</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*rQtwPiFhz7_fOBrxIkiQsQ.png" /></figure><p>Click on this and then open. There will be a short load period while it searches for updates but you should find a screen like this. Navigate to tutorial and starter files, click it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KCbj0_Htzd218W0eSyQFmg.png" /></figure><p>Then scroll until you find the 2D Galaxy Assests tutorial. Select it and download that package.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/453/1*U52-lkgCrif79JZiu0_rDw.png" /></figure><p>Once downloaded package will automatically be placed in your Project view. This folder contains all the files you need to add explosions, animations, sound f/x and more.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*qNxNcFN5dqLNIx793InIxw.png" /></figure><p>This should put the GameDevHQ folder in your Project View. Navigate to GameDevHQ&lt;&lt;FileBase&lt;&lt;Projects&lt;&lt;Tutorials&lt;&lt;Starter_Files&lt;&lt;2D_Galaxy_Assets.</p><p>We started building this game in 3D so we could easily prototype it. Now we want to convert it to a 2D setting to make use of the sprite files and and the UI and everything else we’re going to want to do in our game. Let’s put a nebular background to our game.</p><p>To start we need to toggle from a 3D to a 2D perspective. Just click the 2D button at the top of the Scene View. This button always reads “2D”, but when toggled you’ll see the difference.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/429/1*W_-4rJzh2W__cLGqahQIhQ.png" /></figure><p>Now, from the sprites folder drag and drop the SpaceBG_Overlay directly into the Hierarchy view and change the name to Background. Fill the Game View with the background by selecting the Rectangle tool and dragging the edges of the image and stretch it out.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/879/1*RPkdRzLlQA7ZWzpYjU6O2w.gif" /></figure><p>Notice in the Inspector panel for the Background that there is a <strong>Sprite Renderer</strong>. <strong>Sprites</strong> are 2D game objects. And as you may expect there are many things we can do with these game objects. At this moment though we are interested in the <strong>Sorting Layer</strong>. This <strong>Sorting Layer </strong>is where we set the order in which our objects are placed in the scene. To better understand this open the <strong>Player_Turn_Left</strong> sprite and drag the first image into the Hierarchy panel and re-name that Player.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*KIz3Gps4PDtuLXcDd4e5Vw.gif" /></figure><p>We have a nifty space ship now. But if you move it around the screen you can see that it is drawn behind or underneath our background.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/883/1*nccWD0S8n948Tt5nhimoYg.gif" /></figure><p>Let’s fix this in the Sorting Layer. Open the dropdown menu and select Add Sorting Layer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/1*33Y1QP8TgwF4Jk-r5jNF5w.png" /></figure><p>Click the + button and re-name New Layer as Background. Make a second layer and call it Foreground.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*euP5IAc9MitVDny-eNlzjg.png" /></figure><p>Go back to the Sprite Renderer ( on the Background object ) and make sure to switch from the default setting to this Background setting. And on the Player’s ( space ship ) Sorting Layer select Foreground. At this point we no longer need the Player ( cube ) so delete that. And drag the Player *script* onto the Player ( space ship ) Inspector panel.</p><p>Much better.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/883/1*eKKLwxIn2h15KdaEmTIlSw.gif" /></figure><p>But it’s a little big. In the inspector adjust the <strong>Scale </strong>on all axes to .75.</p><p>Running the game now you’ll notice that our ship is a better size, but nothing happens when it collides with the enemy cube. That’s because the ship is a 2D object and the enemy cube is a 3D object with 3D collision detection.</p><p>Let’s repeat this process for the Enemy and Laser pre-fabs making sure to set them to Foreground on the Sorting layer and delete the originals. For the Enemy ship use the sprite found at Sprites&lt;&lt;Enemy_Explode_Sequence&lt;&lt;Enemy_Turn_Left and reset the Laser scale to 0.2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/404/1*u--YF62Iz88HZnTK5hk7vQ.png" /></figure><p>Onward to the 2D detection so that ships crash and lasers destroy. <br>This green box is the collision detection area.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/883/1*CbI3KDlCCFKGzDcp5U0OYg.png" /></figure><p>Anything that invades the bounds of this box will trigger the collision detection. As you can see it’s a bit much. In the Box Collider 2D toggle the Edit Collider button…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/607/1*lcjLZ_Qh7Mi85cF2WB7Kww.png" /></figure><p>and tighten up those bounds as close to the ship as you can.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/883/1*04_nUS-fNW4krNqN2uELgQ.png" /></figure><p>And repeat this process for the Enemy and Laser prefabs. When this is done make sure Player, Enemy, and Laser all have their proper tags. Make sure that the Box Collider 2D for each has Is Trigger checked. And finally, the RigidBody 2D on Enemy and Laser has Gravity Scale set to 0 to make sure that gravity is not being used.</p><p>Go into the Enemy script and let’s edit our code to detect 2D collisions and objects.</p><p>Change this line:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/704/1*qtq6HtrCZmCwd_wPWRpWpw.png" /></figure><p>to this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/758/1*OHcKedYzVv29Ay-bRz9Wsg.png" /></figure><p>Simply adding 2D to both of these is all we need to convert from the 3D perspective. It feels like there was a lot of work here. Especially if you were following this like a tutorial, but it really wasn’t much or all that challenging.</p><p>Save everything, jump back into Unity and test it out.</p><p>HOLY WOW! It looks like a real game now! HA-Haaaaaa, yeah!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/883/1*yXCtjetOVlkOYcgKxvXvdg.gif" /></figure><p>We covered a lot of ground today. But boy, oh, boy do we have something beautiful now?</p><p>Until next time…</p><p>Code your imagination to life.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fa1e566134df" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Assets! Assets! Get Your Assets Here!]]></title>
            <link>https://medium.com/@christian.fredericksen/assets-assets-get-your-assets-here-afbb94ec1b69?source=rss-16897956646d------2</link>
            <guid isPermaLink="false">https://medium.com/p/afbb94ec1b69</guid>
            <category><![CDATA[unity-game-development]]></category>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[csharp-programming]]></category>
            <category><![CDATA[csharp]]></category>
            <dc:creator><![CDATA[Christian Fredericksen]]></dc:creator>
            <pubDate>Fri, 17 Mar 2023 22:37:27 GMT</pubDate>
            <atom:updated>2023-03-17T22:37:27.917Z</atom:updated>
            <content:encoded><![CDATA[<p>Objective: Get set up with Filebase to make your games pretty.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OsQedx57m5S-W8LIkuhiBw.jpeg" /></figure><p>This is a kind of companion article. If you have <strong>Filebase </strong>installed on your computer then you can skip this article. If not then you are in exactly the right place.</p><p>Assets are all the pretty things in the game that make it look like a game. The halls of a hospital, or a warehouse, or cityscape…furniture, vehicles, weapons, powerups, sound F/X, animations…all assets. And way more than we want to create ourselves. So we make use of asset libraries like <strong>Filebase</strong>. Other creatives have worked very hard to make these assets available to make us look like Gods in Gamedev. That sounds like a new Netflix show. So, let’s get it downloaded <a href="https://filebase.gamedevhq.com/">here</a>.</p><p>Once you have it downloaded and located it on your computer, you need to drag the file into the Project view of your open project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/314/1*ZreCeNvp7hUJ6YodGiZReQ.png" /></figure><p>When Dropped, you will get a new window that pops up, Click the Import button to start loading Filebase.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/421/1*c2ZYXYxXiaYw0sTpLf8Fkw.png" /></figure><p>After opening Filebase, you may be greeted with this or a similar message.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/569/0*_8D4bXIeNckZxNl2.png" /></figure><p>Now that you are on the latest version, you will want to login so that you can access the assets. The user Login is in the top right corner of the window. You will use your email and password.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/0*Yqcgn_JBf7GaJQbi.png" /></figure><p>Now that you are logged in, you have full access to thousands of high quality asset packs. And you will find all the files you need listed under Projects&gt;Tutorials.</p><p>Until next time…</p><p>Code your imagination to life.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=afbb94ec1b69" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spawning Objects in Unity Without the Clutter]]></title>
            <link>https://medium.com/@christian.fredericksen/spawning-objects-in-unity-without-the-clutter-b4150def2698?source=rss-16897956646d------2</link>
            <guid isPermaLink="false">https://medium.com/p/b4150def2698</guid>
            <category><![CDATA[unity-game-development]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <dc:creator><![CDATA[Christian Fredericksen]]></dc:creator>
            <pubDate>Mon, 13 Mar 2023 05:31:24 GMT</pubDate>
            <atom:updated>2023-03-13T05:31:24.646Z</atom:updated>
            <content:encoded><![CDATA[<p>Objective: Explore another level of script communication</p><p>And they just keep coming!! 😲</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/552/1*-JnX56tfYGQKilxA8B31Rg.gif" /></figure><p>Now is the time when we will do a little tidying up. As it is, when we run our game there is no end to the volume of enemy instantiated. Like those rats this can be problematic in a couple of ways. The first is how much clutter is created in the Hierarchy panel. Every 5 seconds we instantiate another enemy clone and it shows up there.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*ont3HLZlRJFkMtLXRK2xSg.png" /></figure><p>As the game grows there will be other things in the Hierarch we want to see and if this clutter is here, well, you get the idea. So, let’s fix this and introduce a few new ideas. The first idea is <strong>parenting</strong>. Think of the instantiation of each object as a ‘birth’ and that object is a ‘child’. The Spawn_Manager just wants to manage, it doesn’t want to be a ‘parent’. So we have to create a ‘parent’.</p><p>Create a new Empty Object on the Spawn_Manager and call it Enemy Container. This will be the ‘parent’ and it will appear nested like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*FOQozKtqyykbPc2ZbaSkjw.png" /></figure><p>Now, over in the Spawn_Manager we will make a reference to this Enemy Container:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/762/1*6TE1RAsuNSxnX8Bth4JZuA.png" /></figure><p>And back in Unity drag and drop the Enemy Container onto the Spawn_Manager script:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1003/1*P8aADAPHsBWflUHrjodBxg.gif" /></figure><p>At this time our game is instantiating enemy prefabs but we can’t do anything with them because we have no reference to them. Remember, with OnTrigger/CollisionEnter the object reference is stored in ‘other’ and ‘collider’. So, how do we reference these prefabs? Simple. Assign it to a variable. Let’s go back to the Spawn_Manager and make these additions to our code in the SpawnRoutine() function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vcb550NzWrd6-ZUadX6gQg.png" /></figure><p>Create a _newEnemy GameObject and set it equal to the prefab that is instantiated. Now we have an object that we can reference and modify like any other. And we can assign _enemyContainer.transform as its ‘parent’. And upon ‘birth’ it is shuffled off to its new home. You can see this by saving your work and running the game in Unity. You can open and close the drop down as needed and keep the Hierarchy clutter free.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*dg6A5IChgpgQS8jJHfytvA.gif" /></figure><p>There. That takes care of that. But, what happens when our player dies? The SpawnRoutin() funtion is still running and instantiating new enemies. That’s quite unnecessary so we need to find a way to stop spawning. This is our next level of script communicating. Our Player script needs to tell the Spawn_Manager script that the player is dead and enemy objects should stop spawning.</p><p>We do this by adding a reference to the Spawn_Manager in the Player script:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/948/1*BCOfj-9WFsHREctWJSO1fQ.png" /></figure><p>We want this line of communication established from the beginning so, in the Start() function let’s add:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lhestJslVhLeCrxvxWu8OQ.png" /></figure><p>Where we:<br>(1) — take the _spawnManager reference we made and assign its value. Here we use Unity’s <a href="https://docs.unity3d.com/ScriptReference/GameObject.Find.html">GameObject.Find()</a> function. This function takes in a string — the name of the object. Once we find the proper object we can then run the GetComponent&lt;T&gt;() function we recently learned about. The component type we are looking for has to match the name in the .Find() function — SpawnManager.</p><p>(2) — run a null check</p><p>(3) — error out if null.</p><p>We have the line of communication between the two scripts established. Now, what we are trying to do is have the Spawn_Manager stop spawning. Right now the script is running from the start. Let’s create a private bool variable called <strong>_stopSpawning</strong> and set it to false. This will be our “off switch”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/732/1*ngb68qjZqYIViMqyjWxDmw.png" /></figure><p>Right now our <strong>while loop</strong> has a ‘<strong>true</strong>’ condition in it. Let’s change that to make use of the variable we just made:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/757/1*2r8x9kRo3-7E6fsarjpnvA.png" /></figure><p>And now we will create a public method called <strong>OnPlayerDeath()</strong>. We make it public so that the <strong>Player script</strong> can access it. And in this function we will reset the value of <strong>_stopSpawning</strong> to ‘<strong>true</strong>’. When called, this function changes the condition in the <strong>while loop</strong> thereby stopping the function and no more enemy clones will instantiate:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/609/1*_7ghHDPkuCG-oalqsPOWoA.png" /></figure><p>And finally, we call on this function in the player’s <strong>damage()</strong> function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*QyOGH_xUE1DdLR5d4k3SoQ.png" /></figure><p>If _lives reaches 0, notify the <strong>_spawnManager</strong> with the <strong>OnPlayerDeath()</strong> function. The <strong>Spawn_Manager</strong> will stop instantiating new enemy clones and the player gameObject will be destroyed. In the gif below you can see the player collide with 3 enemy clones, get destroyed and, instantiation stops. Whatever clones were left are all that scroll on the screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/707/1*1wo78hBA156e-ndz2MxHDg.gif" /></figure><p>There we go. We have our enemy spawning with less clutter. We learned about <strong>parenting </strong>(lol, Idk if this makes me a better dad)<strong> </strong>and a little more script communication through the <strong>GameObject.Find()</strong> function.</p><p>Until next time…</p><p>Code your imagination to life.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b4150def2698" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coroutines With Unity]]></title>
            <link>https://medium.com/@christian.fredericksen/coroutines-with-unity-d70d416fde84?source=rss-16897956646d------2</link>
            <guid isPermaLink="false">https://medium.com/p/d70d416fde84</guid>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[csharp-programming]]></category>
            <category><![CDATA[unity-game-development]]></category>
            <dc:creator><![CDATA[Christian Fredericksen]]></dc:creator>
            <pubDate>Sat, 11 Mar 2023 01:58:28 GMT</pubDate>
            <atom:updated>2023-03-11T01:58:28.237Z</atom:updated>
            <content:encoded><![CDATA[<p>Objective: Understand how to use coroutines to pause their function for a specified time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/519/1*zltPB40XJfJ54ZZFZDOo6w.png" /></figure><p>What is a coroutine? According to the Unity <a href="https://docs.unity3d.com/ScriptReference/Coroutine.html">documentation</a>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*g-hydb03nYk1FUvDJSwhdQ.png" /></figure><p>Alright, let’s just get into it. We’re ready to start spawning some enemy game objects. In our game we want to have our enemy to spawn at random locations at the top of the screen every 5 seconds. This is made easy in Unity by using the IEnumerator type to create our coroutine. The IEnumerator in Unity is the tool we use when we need a function to do something, wait a few seconds and do it again. Or do something else. The point is we can tell this function to pause and wait before moving on.</p><p>In the Spawn Manager script let’s make a reference to the enemy object we want to spawn:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/695/1*TekmfDakpOOCYwqfqj3dxA.png" /></figure><p>Let’s think through this in pseudocode. We want to:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*51AYMqGmHYoz1sZ16gB2eA.png" /></figure><p>So we’ll create an IEnumerator SpawnRoutine() function. Knowing we want our enemy to appear in random X locations, we create a variable for that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*si2P5Lgk2BQgRIJFMuMZ-A.png" /></figure><p>With that and the logic our coroutine function will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0GRLM3UT1TIkCmC-wqeyQQ.png" /></figure><p>Coroutines should have their logic in a loop. This while loop is infinite because true is always true and therefore once executed will continue to run. So:<br><em>while ( some logic is true )<br>{<br> Instantiate ( an enemy, at a new (_randomX location, just above the top of the screen, on the 0 point Z axis ), set the rotation);</em></p><p><em>yield return *pause* for (5 seconds);<br>}</em></p><p>In this case, when the 5 seconds is up it will execute the code again. Now, in order to use this coroutine we have to use Unity’s StartCoroutine() function to call our SpawnRoutine() function. Like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/1*-0GpQCtM6bVJszTrptbOMA.png" /></figure><p>Top to bottom our Spawn Manager should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/841/1*PJqoTPSNn87n9E9AiqYryA.png" /></figure><p>Now, the last thing we need to do to make this work is to put a reference to the enemy prefab on the Spawn Manager’s script in the Inspector. This way the Spawn Manager knows what to spawn.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1020/1*8EecjX1RfQvzMDoMyki9oA.gif" /></figure><p>Ctrl+(S)ave your work. Hop into Unity and run it. If all is well it should look something like GIF below. Every five seconds a new enemy is spawned and they all reappear at a random spot at the top of the screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/719/1*QNr-DYRXo21RrUpmOpsm7w.gif" /></figure><p>In this example we see just a tiny bit of what can be done with coroutines in Unity. Now, image shooting one of these enemy and we hear an explosion, see a flash, pieces scatter and a power up appears. All can be accomplished using coroutines.</p><p>Until next time…<br>Code your imagination to life.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d70d416fde84" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spawn Manager Setup]]></title>
            <link>https://medium.com/@christian.fredericksen/spawn-manager-setup-db8b1283ca2d?source=rss-16897956646d------2</link>
            <guid isPermaLink="false">https://medium.com/p/db8b1283ca2d</guid>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[unity-game-development]]></category>
            <dc:creator><![CDATA[Christian Fredericksen]]></dc:creator>
            <pubDate>Sat, 11 Mar 2023 00:13:18 GMT</pubDate>
            <atom:updated>2023-03-11T00:13:18.060Z</atom:updated>
            <content:encoded><![CDATA[<p>Objective: Set up a place holder in Unity to spawn enemies</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/730/1*bjwuFsLIwxFEClQqlLif7A.gif" /></figure><p>So, what is a <strong>Spawn Manager</strong> in <strong>Unity</strong>? As you know, in <strong>Unity </strong>we have <strong>Game Objects</strong>. We also have <strong>Empty Objects</strong> and those serve as containers. They hold information. In this case it is going to hold a <strong>script </strong>that will <strong>instantiate </strong>or <strong>spawn </strong>new <strong>Enemy Game Objects</strong> for us to shoot at. Let’s set one up for our space shooter. It’s really easy and we’ll be out of here in no time.</p><p>First, in the <strong>Hierarchy</strong> panel right click and select <strong>Create Empty</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/1*WHJL0EA7EGa28Tm0kjdvrA.png" /></figure><p>And name this <strong>Spawn_Manager</strong>. Notice in the <strong>Scene View</strong> that there is a reference to an object in there. But there is not, nor will there be, any physical object or interaction with it. It’s just an invisible filing cabinet.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ds8hIvaebWebWOKzNOZ51g.png" /></figure><p>Now make yourself a new <strong>C# Script</strong> and call it <strong>SpawnManager</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*JKcQih638Qq4LbXqai7n3g.png" /></figure><p>Finally, drag the script onto the <strong>Spawn_Manager</strong> <strong>game object</strong> and we’re done. That’s it. We have our <strong>Spawn Manager</strong> set up. Next…some coding to instantiate or spawn enemies every couple seconds.</p><p>Until then, code your imagination to life.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=db8b1283ca2d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Script Communication in Unity using GetComponent]]></title>
            <link>https://medium.com/@christian.fredericksen/script-communication-in-unity-using-getcomponent-f497d973c4ee?source=rss-16897956646d------2</link>
            <guid isPermaLink="false">https://medium.com/p/f497d973c4ee</guid>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[unity-game-development]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[csharp]]></category>
            <dc:creator><![CDATA[Christian Fredericksen]]></dc:creator>
            <pubDate>Sat, 11 Mar 2023 00:08:17 GMT</pubDate>
            <atom:updated>2023-03-11T00:08:17.994Z</atom:updated>
            <content:encoded><![CDATA[<p>In an earlier article I promised we would cover Player lives and damage. Now, would be a good time to deliver on that promise as it will be part of the bigger lesson today.</p><p>Communicating between scripts is a crucial element in Unity programming. Having one game object reference another and be able to access its public methods gives us great flexibility in our programming. So, let’s get straight to it.</p><p>In our <strong>Player </strong>script we will create a private variable for <strong>_lives</strong> and give it a <strong>Serialized Field</strong>. Here we have our <strong>_lives </strong>set to 3. Pretty standard for a game.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/530/1*K19fMatwfIyUlcZg_882oQ.png" /></figure><p>Now, we have to have some way of keeping track of how many lives we have. Let’s create a <strong>damage()</strong> function that will remove 1 life each time we crash into an enemy and destroy the <strong>Player </strong>object when it reaches 0 lives.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*lDRHKyBSrXqke9WxHQuCaA.png" /></figure><p>Now that that is set up let’s move over to the Enemy script and revisit the <strong>OnTriggerEnter(Collider other)</strong> function. Remembering that “other” contains reference information of the other game object, when “<strong><em>other.tag == “Player</em></strong>”<strong> </strong>we are able to utilize Player’s public methods here. In this case the <strong>damage()</strong> function. And we do this using Unity’s <a href="https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html"><strong>GetComponent </strong></a>function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/937/1*nu8-c2AmJaWhEKmV3GU5qQ.png" /></figure><p>If the other tag is equal to “Player” then we create a variable of type Player, name it player and assign its value by calling <strong>GetComponent&lt;T&gt;</strong> on <strong>other, </strong>T being the component you want, in this case the Player. Then we want to null check player and make sure it exists. If that checks true then we can call the damage() function on player.</p><p>Jump back into Unity and try it out. You’ll see in the example, albeit not very exciting, when the player experiences 3 collisions the Destroy() function is executed and the Player object disappears.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/884/1*VAoZRdNe0iRDW4XW4ItJTA.gif" /></figure><p>And there it is. A pretty simple explanation of GetComponent and how to use it.</p><p>Until next time,</p><p>Code your imagination to life.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f497d973c4ee" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[OnCollisionEnter vs. OnTriggerEnter — When to Use Them]]></title>
            <link>https://medium.com/@christian.fredericksen/oncollisionenter-vs-ontriggerenter-when-to-use-them-1b544a0bb7dd?source=rss-16897956646d------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b544a0bb7dd</guid>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[unity-game-development]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[game-development]]></category>
            <dc:creator><![CDATA[Christian Fredericksen]]></dc:creator>
            <pubDate>Sat, 11 Mar 2023 00:06:21 GMT</pubDate>
            <atom:updated>2023-03-11T00:06:21.323Z</atom:updated>
            <content:encoded><![CDATA[<h3>OnCollisionEnter vs. OnTriggerEnter — When to Use Them</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/1*JYnzoZHO2n-toDRiCdlCWA.png" /></figure><p>So, here we are scratching the surface, just getting into the physics of gaming. This is a perfect time to talk about collision detection in Unity.</p><p>At this point in our development we are concerned about two types of collisions. The first type are “hard surface” collisions like a basketball bouncing down the court, or your race kart crashes into a wall and gets knocked back onto the track, or maybe the bumpers in the pinball game change colors every time the ball collides with them. These kinds of collisions have physical reactions in the game environment.</p><p>Unity gives us tools to detect different types of collision events. For these “hard surface” collisions we want to use the OnCollisonEnter function. If we look at the <a href="https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html">Unity Scripting API</a> we see that:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*Kw58OERkQPgzYcuA9a3AxA.png" /></figure><p>Basically, when a Rigidbody enabled game object touches another game object with an active Rigidbody or collider, Unity’s physics engine calls the OnCollisionEnter() and the logic it contains is executed. See <a href="https://docs.unity3d.com/Manual/CollidersOverview.html"><strong>Collision callbacks for scripts</strong></a><strong> </strong>in the Unity manual.</p><p>Let’s show this in a simple example of an Obstacle object changing color when a Player object collides with it.</p><p>First, let’s create a few 3D Objects. I used all cubes and named them Player, Obstacle, and Floor.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*XFoy4mRc8c92LDwsgq2K0g.png" /></figure><p>Be sure to create a tag for the Obstacle object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/335/1*IgczbesjvhNcqwaOZ_Z2Eg.png" /></figure><p>Then give them all a _mat file and issue each one a color.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*oSN0YtJydFilDg0yfMPsAw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/291/1*xDGDy-TDdF4Mse0-CYPNGA.png" /></figure><p>You’ll also notice that I attached a C# script to the Player object. And that is where we will write our color changing code using the OnCollisionEnter() function.</p><p>Now, remember the documentation tells us that one of our objects must have a Rigidbody component while the other only needs a collider. So, lets add a Rigidbody component to the Player making sure “<em>Use Gravity</em>” is checked and “<em>Is Kinematic</em>” is not checked.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/595/1*qHHI7zRQxwiGhFidEzE7Gg.png" /></figure><p>Finally, the Box Collider for both Player and Obstacle need to have “<em>Is Trigger</em>” not checked.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/592/1*YXA-sCNRYRKB8NruyEuB8Q.png" /></figure><p>Open the script in Visual Studio and delete the start() and update() functions as we won’t need them for this exercise. Start typing in “<em>void OnCollisionEnter</em>” and the suggestion will appear.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/604/1*zAqy_gQzU0elP0yjRF0--A.png" /></figure><p>Select it and it will autofill the shell of the function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/807/1*Gx0bliifagRDuG_ykpRgCQ.png" /></figure><p>As you can see, OnCollisionEnter passes in a Collision class variable called “collision”. I change this to “other” just to keep it straight in my head that the information contained in this variable is in reference to the other object, not this Player.</p><p>Now, what do we want to do here? Don’t forget to pseudocode:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/691/1*LTzOwij1C7n5R2nqYLyYiQ.png" /></figure><p>So we need an if statement that checks the contents of ‘other’ to see if it is the Obstacle.</p><p>So, if the other’s transform has a tag called “Obstacle”,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/556/1*KVUfaw86ONn4y919-H2HPw.png" /></figure><p>then dig into the other’s transform, run the GetComponent fuction in search of the Renderer component,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*tXE5NTISMVrYNDwFfuzh3A.png" /></figure><p>tap into the material then the color, and set its value to a Random(ly) chosen color.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/509/1*Ewb7ah9GypDrAZrt5hNtQA.png" /></figure><p>At this point the script should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YdA0npdRGFDFKHyfyURJpQ.png" /></figure><p>Now, ctrl+S(ave) your work. Hop back into Unity and try it out.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/885/1*aijlCFuqeL5z3fDpZQSP_Q.gif" /></figure><p>AWESOME!</p><p>Now let’s demonstrate OnTriggerEnter. For this example we will create the illusion of having “picked up” a pellet. Let’s say our player is eating pellets like Pac-Man, or gathering coins like Sonic, or a power up. We want to make it look like we have collected something. On <a href="https://docs.unity3d.com/Manual/CollidersOverview.html">this</a> page of the Unity manual if you scroll to the bottom, you’ll discover a couple of different matrixes detailing which collider combinations will detect collisions and which combinations will trigger events.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wSRANoLLYfMU1TuRtJuuiw.png" /></figure><p>Let’s create a new game object. A sphere. Rename it “Pellet”. Also, create and assign the tag for that as well. Let’s skip the _mat file this time and, following the matrix, we will check the “<em>Is Trigger</em>” box on the Pellet’s Sphere Collider.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/611/1*7sdxCISAKrEVGFhnbSuEPQ.png" /></figure><p>Back in VS Code we will make use of the OnTriggerEnter() function. Thinking through it the same way we did OnCollisionEnter let’s write this code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/1*DzyVCIUqFzo_u8ZW1BRSNw.png" /></figure><p>Here we see that when the logic runs our code will look for the tag “Pellet” and if it finds that tag our code will destroy the game object making it look like we have collected it. Ctrl+S(ave) your work and check it out in Unity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/885/1*LUMTGp5hru8fq-HurT3WtQ.gif" /></figure><p>And there you have it folks. A couple examples demonstrating a couple use cases and the difference between OnCollisionEnter and OnTriggerEnter. I hope this has shed some light on these two functions.</p><p>Until next time,</p><p>Code your imagination to life.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b544a0bb7dd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>