<?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 Bardent on Medium]]></title>
        <description><![CDATA[Stories by Bardent on Medium]]></description>
        <link>https://medium.com/@bardentstudios?source=rss-5482191d9887------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*F7XiVctDOODvQnpM</url>
            <title>Stories by Bardent on Medium</title>
            <link>https://medium.com/@bardentstudios?source=rss-5482191d9887------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 00:26:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bardentstudios/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[Part 34 — Stats — 2D Platformer Player Controller]]></title>
            <link>https://medium.com/@bardentstudios/part-34-stats-2d-platformer-player-controller-7cd46f4d2a82?source=rss-5482191d9887------2</link>
            <guid isPermaLink="false">https://medium.com/p/7cd46f4d2a82</guid>
            <category><![CDATA[games]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[platformer]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[unity]]></category>
            <dc:creator><![CDATA[Bardent]]></dc:creator>
            <pubDate>Mon, 27 Sep 2021 08:58:41 GMT</pubDate>
            <atom:updated>2021-09-30T17:41:45.930Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>Part 34 — Stats — 2D Platformer Player Controller</strong></h4><p>Before we begin, I just want to note that there are no parts 1 through 33 written out. This is a new thing I am trying to accompany the YouTube series that can be found here:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FPux1GlFwKPs%3Flist%3DPLy78FINcVmjA0zDBhLuLNL1Jo6xNMMq-W&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DPux1GlFwKPs&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FPux1GlFwKPs%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/7b27ae37490e1f039d7f9391a02d9240/href">https://medium.com/media/7b27ae37490e1f039d7f9391a02d9240/href</a></iframe><p>You can also find the state of the project <a href="https://github.com/Bardent/Platformer-Tutorial/tree/57256b4a053ce11cf35aa54f4d305f7f3e907c36">before</a> and <a href="https://github.com/Bardent/Platformer-Tutorial/tree/medium-Part34">after</a> this article on GitHub.</p><p>In part 33 we created our IDamageable and IKnockbackable interfaces that determines how we interact with objects that we can damage and knock around with our attacks. Today we are going to focus on adding our Stats core component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/1*h8T6P4JFP-d9vZD2jveqfQ.png" /></figure><h3><strong>Bug Fixes:</strong></h3><p>But before we start all that, it’s time for some BUG FIXES — better known as: What did I forget to do last time?</p><p>Last time, in our Combat.cs script, we implemented our IKnockbackable interface.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/267ee537a46a47c7636b2609eef5c6a5/href">https://medium.com/media/267ee537a46a47c7636b2609eef5c6a5/href</a></iframe><p>We mentioned that we wanted to not only stop the knockback when the entity lands on the ground, but also if a timer runs out before that happens. This is why we created the private float knockbackStartTime; variable and assign it to the current time in the Knockback() function. I forgot to actually make use of it in the CheckKnockback() function. So, here is what we need to do.</p><p>First, in our <strong>Combat.cs</strong> script, let’s declare [SerializeField] private float maxKnockbackTime = 0.2f; This will be the maximum amount of time the knockback can take place and we will stop the knockback after this if the entity has not yet landed on ground again. Then in our CheckKnockback() function, we simply need to add a time check to the if statement. So, we want to set the knockback to false if the character is grounded <strong>OR</strong> the time has passed. So, we simply add || Time.time &gt;= knockbackStartTime + maxKnockbackTime to the if statement parameters and surround it and the ground check with brackets. We end up with:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf49988618512e72285896767974654b/href">https://medium.com/media/bf49988618512e72285896767974654b/href</a></iframe><p>And that’s it for things I forgot to do, now let’s move on to things I wish I did earlier.</p><h3><strong>List of Logic Updates</strong></h3><p>Let’s consider our <strong>Core.cs</strong> and <strong>CoreComponent.cs </strong>scripts.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/202aa12906561757ea0afd39d1b63e9e/href">https://medium.com/media/202aa12906561757ea0afd39d1b63e9e/href</a></iframe><p>Our Core gives our entities access to some other useful and reusable components. Some of these components, like Movement and Combat , make use of LogicUpdate() functions. With our current approach, every time we add a new <strong>Core Component </strong>that uses it, we need to remember to call it from our <strong>Core </strong>LogicUpdate() . Instead what we are going to do create a new interface called ILogicUpdate that we are going to implement in our <strong>CoreComponent.cs. </strong>Then in our <strong>Core.cs, </strong>we are going to create a list of type ILogicUpdate that our core components can add themselves to. We can then simply loop through this list in the Core’s Logic Update and call the item’s Logic Update functions. The code will now look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c335c1ec55a799412ec29551fc8d8a0/href">https://medium.com/media/0c335c1ec55a799412ec29551fc8d8a0/href</a></iframe><p>Now we can update our <strong>Combat.cs </strong>and <strong>Movement.cs CoreComponents. </strong>We simply need change their Logic Updates to override the Logic Update from the base class. public override void LogicUpdate() . We can now run the game and ensure everything is still working as expected. We are not ready to move on to the focus of the tutorial.</p><h3>Stats:</h3><p>We can start off by creating our new script in <strong>Assets&gt; Scripts&gt; Core&gt; CoreComponents </strong>folder.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/423/1*aegJ6ak5Ie__45iJbJ2Qeg.png" /></figure><p>We can then open it up, get rid of all the generated code, and make it inherit from CoreComponent instead.</p><pre>using UnityEngine;<br><br>public class Stats : CoreComponent<br>{<br>}</pre><p>Our focus today is going to be health, so let’s go ahead and declare two fields for our maximum and current health.</p><pre>[SerializeField] private float maxHealth;<br>private float currentHealth;</pre><p>We add [SerializeField] in front of our max health so we can set the health in the inspector. Next let’s set our current health to our max health when the game starts. The best place to do this is in the Awake() function.</p><pre>protected override void Awake()<br>{<br>    base.Awake();<br><br>    currentHealth = maxHealth;<br>}</pre><p>Remember that we are using protected override as we have declared our Awake function as a virtual function in our base <strong>CoreComponent </strong>script. Next we need to create two public functions that we can use to increase and decrease our health. Both of these functions will return void and take in a float parameters that is the amount to increase or decrease our health by.</p><p>In our DecreaseHealth(float amount) function, we want to start by subtracting the amount from our current health. Then we want to check if our health is less than or equal to zero, in which case the entity will die. In this case we will also set the current health to 0 as our health should never be less than that.</p><pre>public void DecreaseHealth(float amount)<br>{<br>    currentHealth -= amount;<br><br>    if(currentHealth &lt;= 0)<br>    {<br>        currentHealth = 0;<br>        Debug.Log(&quot;Health is zero!!&quot;);<br>    }<br>}</pre><p>Currently we are not doing anything when the health reaches zero other than stating it via a debug statement. Eventually here is where the entity will let the system know that it has died and the system will respond appropriately. In the case of enemies, this could be subtracting a counter for a wave, or updating a quest tracker, and in the case of the player this could be triggering a respawn.</p><p>In our IncreaseHealth(float amount) function, we will simply increase the current health by the amount, and then clamp our health between 0 and our max health.</p><pre>public void IncreaseHealth(float amount)<br>{<br>    currentHealth = Mathf.Clamp(currentHealth + amount,<br>                                0,<br>                                maxHealth);<br>}</pre><p>Here Mathf.Clamp takes in 3 parameters. The first parameter is the value we wish to clamp. Note that this value is not directly affected and that the clamped value is only returned by the function. The second parameter is our minimum value, and the last parameter is our maximum value.</p><p>Now that is the basics for the <strong>Stats.cs</strong> script. Later on we will be adding more stats to it, like poise, which will affect when an attack stuns an entity. We might want to change up our approach and make it more generic, allowing us to create any stat we want without having to change the code, but that is out of scope for now.</p><p>Here is the final <strong>Stats.cs </strong>script:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fcc4b76101e440668b2d9d3a5a79fa42/href">https://medium.com/media/fcc4b76101e440668b2d9d3a5a79fa42/href</a></iframe><p>Now we can add our <strong>Stats </strong>to our player by creating a new empty game object under the Core game object. We can rename it to stats and then in the inspector click on “Add Component”, search for “Stats” and click on it. We can also set our max health to be 100 by default.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/391/1*4n_HQGNsgwLxFrh5PMrpFQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/423/1*GeihI1HvmK3A2bt8_J2V9A.png" /></figure><p>We can then add <strong>Stats</strong> to our <strong>Core. </strong>So, in <strong>Core.cs </strong>we can declare a new field of type <strong>Stats</strong> and then create the property for it. Underneath our combat field we can say:</p><pre>private Stats stats;</pre><p>Then underneath our combat property we can say:</p><pre>public Stats Stats<br>{<br>    get =&gt; GenericNotImplementedError&lt;Stats&gt;.TryGet(stats, transform.parent.name);<br>    private set =&gt; stats = value;<br>}</pre><p>Next, we just need to set our reference to stats in the Awake() function.</p><pre>stats = GetComponentInChildren&lt;Stats&gt;();</pre><p>Finally, our <strong>Core </strong>will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0942539ab10688c9f41a0034a783bd42/href">https://medium.com/media/0942539ab10688c9f41a0034a783bd42/href</a></iframe><p>Now that we have access to <strong>Stats</strong> from our core, we can go ahead and damage our entity from the Damage(float amount) function in our <strong>Combat.cs</strong> script. To do this, we simply call the DecreaseHealth(float amount) function when we get damaged. So we add:</p><pre>core.Stats.DecreaseHealth(amount);</pre><p>And then our combat script will finally look like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/74762d264d1109fe0be87346a31a705c/href">https://medium.com/media/74762d264d1109fe0be87346a31a705c/href</a></iframe><p>And that’s it. This is how we connect up our damage function to some sort of stat. To test it out, let’s go up to an enemy and let it hit the us for a bit and ensure we get the debug log stating that our health is 0.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/228/1*VHB57j2UimwXvo5JNYDm2w.png" /></figure><p>We should also go ahead and add the <strong>Stats</strong> game object to both of our enemies. For now I set both their max health to 20 for testing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/193/1*NoUMuZ6bWZb_wkQ-FRhYjg.png" /></figure><p>Don’t forget to also test on your enemies.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/233/1*31elA5UtIR3xr8O_1L6Dgw.png" /></figure><p>Next time we will take a look at spawning particles efficiently when an entity gets damaged.</p><p>I hope you found reading this enjoyable and that you learned something new. This is a new format for me but I am excited about the possibilities. If you have any feedback, feel free to reach out to me on my <a href="https://discord.gg/uHQrf7K">discord server</a>.</p><p>If you want to see the rest of the tutorials up to this point, you can find them on my <a href="https://www.youtube.com/c/Bardent">YouTube Channel</a>.</p><p>And finally, I just want to say thank you to all of my supporters over on <a href="https://www.patreon.com/Bardent">Patreon</a>. You guys drive me to worked harder on this series and it means a lot!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7cd46f4d2a82" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>