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
- This topic has 3 replies, 2 voices, and was last updated 8 months ago by Terence.
-
AuthorPosts
-
April 28, 2024 at 9:59 am #14363::
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
April 28, 2024 at 6:55 pm #14377::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/
April 28, 2024 at 8:48 pm #14379::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!
April 28, 2024 at 8:54 pm #14380 -
AuthorPosts
- You must be logged in to reply to this topic.