Forum begins after the advertisement:


[General] Adding Damage, Kills, DPS

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [General] Adding Damage, Kills, DPS

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #14363
    Cam
    Level 23
    Silver Supporter (Patron)
    Helpful?
    Up
    0
    ::

    Said i would show my code for adding Damage Kills and DPS so here we go, its not perfect but works for a start.

    to start it off ive changed the the stopwatchTime in GameManager to public

    <code>
    [Header("Stopwatch")]
        public float timeLimit; // The time limit in seconds
        public float stopwatchTime; // The current time elapsed since the stopwatch started
        public TMP_Text stopwatchDisplay;
    </code>

    Next ive added a new Script called UIDamageKillsDps

    <code>
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using TMPro;
    
    public class UIDamageKillsDps : MonoBehaviour
    {
        [Header("Kills/Damage")]
        GameManager gameManager;
    
        public int killCount = 0;
        public float totalDamageDone = 0;
        public float dps;
    
        public TMP_Text kills;
        public TMP_Text damage;
        public TMP_Text DPS;
    
        public void Awake()
        {
            gameManager = GetComponent<GameManager>();
            StartCoroutine(UpdateKillCountTextCoroutine()); // Start the coroutine to update kill count text
            StartCoroutine(UpdateDamageTextCoroutine()); // Start the coroutine to update kill count text
            StartCoroutine(UpdateDPSTextCoroutine()); // Start coroutine to update DPS text
        }
    
        public void IncrementKillCount()
        {
            killCount++;
            // Optionally, you can update UI or perform other actions related to the kill count here
        }
    
        // Method to get the current kill count
        public int GetKillCount()
        {
            return killCount;
        }
    
        public void IncrementTotalDamageDone(float dmg)
        {
            totalDamageDone += dmg;
        }
    
        public string GetTotalDamageDoneFormatted()
        {
            if (totalDamageDone >= 1000)
            {
                float damageInK = totalDamageDone / 1000f;
                return damageInK.ToString("0.00") + "k";
            }
            else
            {
                return totalDamageDone.ToString("0");
            }
        }
    
        public void UpdateDPS()
        {
            if (gameManager.stopwatchTime > 0)
            {
                dps = totalDamageDone / gameManager.stopwatchTime;
                DPS.text = "" + dps.ToString("F2"); // Display DPS with 2 decimal places
            }
        }
    
        // Method to update the kill count text
        IEnumerator UpdateDamageTextCoroutine()
        {
            while (true) // Infinite loop to update the text every second
            {
                yield return new WaitForSeconds(1f); // Wait for one second
    
                if (totalDamageDone > 0) // Check if Damage is greater then 0
                {
                    // Update the kill count text with the current kill count
                    damage.text = "" + GetTotalDamageDoneFormatted().ToString();
                }
            }
        }
    
        IEnumerator UpdateDPSTextCoroutine()
        {
            while (true)
            {
                yield return new WaitForSeconds(1f); // Wait for one second
    
                UpdateDPS(); // Update DPS text
            }
        }
    
        IEnumerator UpdateKillCountTextCoroutine()
        {
            while (true) // Infinite loop to update the text every second
            {
                yield return new WaitForSeconds(1f); // Wait for one second
    
                if (killCount > 0) // Check if Kill Count is Greater then 0
                {
                    // Update the kill count text with the current kill count
                    kills.text = "" + GetKillCount().ToString();
                }
            }
        }
    }
    </code>

    Then ive added a text ui for each Kill Damage Dps to hold the Naming then next to that add a Text ui for each of them to be the holder for the counts, i added the UIDamageKillsDps.cs to the GameManager object for now linked the text components, now go get some kills at test it

    null
    #14377
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Thanks for sharing your code Cam. The article for Part 19 is out, as promised last week: https://blog.terresquall.com/2024/04/creating-a-rogue-like-vampire-survivors-part-19/

    #14379
    Cam
    Level 23
    Silver Supporter (Patron)
    Helpful?
    Up
    0
    ::

    Awesome!!!!!!!!!!!!!!!!! already been messing around with its pull your game files to have a play something i found was once all item slots are maxed level you get an empty levelup screen you can not close

    and i know this isnt in the next post but i cant seem to get the new enemy spawner to end and go on the next wave, dont know if was finished but its looking awesome!

    #14380
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Thanks for highlighting the bug Cam. I missed that one!

    I’m planning to fix a few bugs during the Content Creation Stream on Wednesday, and add some details to the article, before I record the content creation portion.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: