- Home /
Trying to figure out how to display floating damage numbers with armor taken into account
So I have a script that works, the damage gets subtracted by armor and the HP gets updated, however I am not sure how to upgrade my floating damage to display the updated damage value. So far it only displays clean damage. (with no armor taken into consideration). So if my damage is 10 and armor is 5, it won't display 5 as damage but still 10, while the enemy does indeed take 5 damage. Here's the script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using Cinemachine;
public class EnemyStats : MonoBehaviour
{
#region Variables
public Armor armor;
public Animator animator;
public HealthBarEnemy healthBar;
public GameObject damageText; //The floating damage text variable
public ParticleSystem enemyVFX;
CinemachineImpulseSource impulse; //Reference to Cinemachine camera shake effect
Material material; //Reference to the dissolve material
public int maxHealth = 100;
int currentHealth;
float fade = 2.5f; //The duration of dissolve effect
bool isDissolving = false;
#endregion
#region Start Method
void Start()
{
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth); //Sets the enemy max health on the enemy HealthBar UI element
material = GetComponent<SpriteRenderer>().material;
impulse = GetComponent<CinemachineImpulseSource>();
}
#endregion
#region Methods
public void TakeDamage(int damage)
{
currentHealth -= damage - armor.armorAmount;
animator.SetTrigger("Hurt");
impulse.GenerateImpulse(); //Calls on the camera shake method in the Cinemachine Impulse Source
enemyVFX.Play();
currentHealth.ToString(); //Converts the current enemy health string so it can be displayed using text
ShowFloatingDamage(damage.ToString()); //Shows the floating damage number in text after converting it from integer
healthBar.SetHealth(currentHealth); //Updates the enemy HealthBar UI to the enemies current health after damage
if (currentHealth <= 0)
{
Die();
}
}
void Die()
{
Debug.Log("Enemy died");
healthBar.gameObject.SetActive(false);
animator.SetTrigger("Death");
AudioManager.instance.Play("EnemyDie");
isDissolving = true; //Activates the dissolve shader
GetComponent<Collider2D>().enabled = false;
}
void ShowFloatingDamage(string text) //The method that shows the floating damage text
{
var go = Instantiate(damageText, transform.position, Quaternion.identity); //Instantiates the damage number above enemy
go.GetComponent<TextMeshPro>().text = text; //Gets the TextMesh text component for the floating damage number
}
#endregion
#region Update Method
private void Update()
{
if (isDissolving)
{
fade -= Time.deltaTime;
if (fade < 0f)
{
fade = 0f;
isDissolving = false;
}
material.SetFloat("_Fade", fade);
if (fade == 0f)
{
this.enabled = false;
}
}
}
#endregion
}
Comment
Best Answer
Answer by jkpenner · Apr 24, 2020 at 04:43 AM
Should be able to determine the amount of damage to apply and store it into a variable.
// Determine amount of damage to apply
var damageAfterArmor = damage - armor.armorAmount;
// Apply damage to the character
currentHealth -= damageAfterArmor;
// Display applied damage
ShowFloatingDamage(damageAfterArmor.ToString());
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Cannot edit text file. 0 Answers
Read objects like in Penumbra 1 Answer