I’m trying to make a shield system. I have two values (100, 100) and a damage value (109). Since this is a shield system, i need to subtract 109 by 100 (9) and somehow display the shield hp, and subtract the remaining damage value to health, so it would look like (109 - 100 = 9, (0 Shield HP) 100 - 9 = 91 (91 HP)) I’ve tried doing this, but it doesn’t work.
curShield -= damage;
damage -= curShield;
if (curShield - damage <= 0)
{
curShield = 0;
}
else
{
Mathf.Abs(curShield);
}
if (damage > 0 && curShield <= 0)
{
damage -= curHealth;
curHealth -= damage;
Mathf.Abs(curHealth);
}
@memeinaction
Here is a simple example of shield system. Hopefully you can expand from this:
public float maxHealth = 100;
public float maxShieldDurability = 100;
float health;
float shieldDurability;
private void Start()
{
health = maxHealth;
shieldDurability = maxShieldDurability;
TakeDamage(109);
Debug.Log("health: " + health.ToString() + " shield: " + shieldDurability.ToString());
}
public void TakeDamage(float damage)
{
if (!Shielded())
{
if (damage <= health)
{
health -= damage;
}
else
{
health = 0;
}
}
else
{
if (damage <= shieldDurability)
{
shieldDurability -= damage;
}
else
{
damage = damage - shieldDurability;
shieldDurability = 0;
health -= damage;
}
}
}
bool Shielded()
{
if (shieldDurability > 0)
{
return true;
}
else
{
return false;
}
}
@kitten, since it doesnt allow me to comment on your profile, for whatever reason, I works but when i try adding damage to 101, it goes negative, and doesnt subtract from health. I dont know what im missing.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HealthSystem : MonoBehaviour {
public float maxHealth = 100;
public float maxShieldDurability = 100;
float health;
float shieldDurability;
private void Start()
{
health = maxHealth;
shieldDurability = maxShieldDurability;
TakeDamage(109);
Debug.Log("health: " + health.ToString() + " shield: " + shieldDurability.ToString());
}
public void TakeDamage(float damage)
{
if (!Shielded())
{
health -= damage;
}
else
{
shieldDurability -= damage;
}
}
bool Shielded()
{
if (shieldDurability > 0)
{
return true;
}
else
{
return false;
}
}
IEnumerator DetectLowHealth()
{
yield return new WaitForSeconds(0.5f);
yield return new WaitForSeconds(0.5f);
StartCoroutine(DetectLowHealth());
}
}