I made this simple armor/shield/health system which works almost perfectly except for when you get hit by a projectile and then lose your shield and armor. It seems as if when that happens, my health all of a sudden starts going down to zero even when the damage isn't that high. Here's the script:
public class Stats : MonoBehaviour {
//
//
public GUISkin skin1;
public GUISkin skin2;
public GUISkin skin3;
public GUISkin skin4;
//
//
public string ShieldAnimation;
public string ArmorAnimation;
public string DamageAnimation;
//
public float ArmorRed = 1;
public float ShieldRed = 1;
public float Health = 100;
public float MaxHealth = 100;
public float Armor = 100;
public float MaxArmor = 100;
public float Shield = 100;
public float MaxShield = 100;
public float Defence = 1;
//
public float Mana = 100;
public float MaxMana = 100;
public float ManaEff = 1;
//
public float PhysResistance = 1;
public float MagResistance = 1;
public float FireRes = 1;
public float EarthRes = 1;
public float AirRes = 1;
public float WaterRes = 1;
public float IceRes = 1;
public float MechRes = 1;
public float LightRes = 1;
public float DarkRes = 1;
public float PSNRes = 1;
//
public float HealthBarLength;
public float ArmorBarLength;
public float ShieldBarLength;
public float ManaBarLength;
void Start () {
HealthBarLength = Screen.width /3;
ArmorBarLength = Screen.width /3;
ShieldBarLength = Screen.width /3;
ManaBarLength = Screen.height /3;
}
void Update () {
if ( Health <= 0 )
{
OnDying ();
}
//
//
AddjustHealth ( 0 );
AddjustArmor ( 0 );
AddjustShield ( 0 );
AddjustMana ( 0 );
}
void OnGUI () {
if ( Shield >= 1 )
{
GUI.skin = skin1;
GUI.Box ( new Rect ( 110, 5, ShieldBarLength, 20 ), Shield + " ");
}
if ( Armor >= 1 )
{
GUI.skin = skin2;
GUI.Box ( new Rect ( 110, 25, ArmorBarLength, 20 ), Armor + " ");
}
GUI.skin = skin3;
GUI.Box ( new Rect ( 110, 45, HealthBarLength , 20 ), Health + " ");
if ( Mana >= 1 )
{
GUI.skin = skin4;
GUI.Box ( new Rect ( 50, 70, 20, ManaBarLength ), Mana + " ");
}
}
//
//
public void AddjustHealth ( int adj ) {
Health += adj ;
if ( Health <= 1 )
Health = 0;
if ( Health > MaxHealth )
Health = MaxHealth;
if ( MaxHealth < 1 )
MaxHealth = 1;
HealthBarLength = ( Screen.width /3 ) * ( Health / ( float ) MaxHealth );
}
public void AddjustArmor ( int adj ) {
Armor += adj ;
if ( Armor < 1 )
Armor = 0;
if ( Armor > MaxArmor )
Armor = MaxArmor;
if ( MaxArmor < 1 )
MaxArmor = 0;
ArmorBarLength = ( Screen.width /3 ) * ( Armor / ( float ) MaxArmor );
}
public void AddjustShield ( int adj ) {
Shield += adj ;
if ( Shield < 1 )
Shield = 0;
if ( Shield > MaxShield )
Shield = MaxShield;
if ( MaxShield < 1 )
MaxShield = 0;
ShieldBarLength = ( Screen.width /3 ) * ( Shield / ( float ) MaxShield );
}
public void AddjustMana ( int adj ) {
if ( Mana < 1 )
Mana = 0;
if ( Mana > MaxMana )
Mana = MaxMana;
if ( MaxMana < 1 )
MaxMana = 0;
ManaBarLength = ( Screen.height /3 ) * ( Mana / ( float ) MaxMana );
}
//
//
void OnCollisionStay ( Collision other ) {
if ( other.gameObject.tag == ( "FireHazards" ))
{
if ( Shield >= 1 )
Shield -= 1 / ShieldRed / FireRes;
if ( Shield <= 0 )
{
if ( Armor >= 1 )
Armor -= 1 / ArmorRed / FireRes;
}
}
if ( Shield <= 0 )
if ( Armor <= 0 )
Health -= 1 / FireRes;
if ( other.gameObject.tag == ( "PhysicalHazards" ))
{
if ( Shield >= 1 )
Shield -= 1 / ShieldRed / PhysResistance;
if ( Shield <= 1 )
{
if ( Armor >= 0 )
Armor -= 1 / ArmorRed / PhysResistance;
}
}
if ( Shield <= 0 )
if ( Armor <= 0 )
Health -= 1 / Defence / PhysResistance;
}
//
//Checkpoints and Death
//
public Vector3 Pos = new Vector3 ( -74, 24, -35 );
void OnTriggerEnter ( Collider other ) {
if ( other.gameObject.tag == ( "Checkpoint" ))
{
Pos = other.gameObject.transform.position;
}
}
//
//
void OnTriggerStay ( Collider other ) {
if ( other.gameObject.tag == ( "Checkpoint" ))
{
Shield = MaxShield;
Armor = MaxArmor ;
Health = MaxHealth;
Mana = MaxMana ;
}
}
public string DeathAnimation;
public void OnDying () {
animation.Play ( DeathAnimation );
GameObject.Destroy ( gameObject );
StopCoroutine ( "Follow" );
StopCoroutine ( "Movement" );
StopCoroutine ( "Follow" );
Respawn ();
}
public void Respawn () {
GameObject.Instantiate ( GameObject.Find ( "trevor" ));
transform.position = Pos;
StartCoroutine ( "Movement" );
StartCoroutine ( "Follow" );
Reset ();
}
public void Reset () {
Mana = MaxMana;
Health = MaxHealth;
Shield = MaxShield;
Armor = MaxArmor;
}
void OnCollisionEnter ( Collision other ) {
float NetAirRes = 1;
float NetDarkRes = 1;
float NetEarthRes = 1;
float NetFireRes = 1;
float NetIceRes = 1;
float NetLightRes = 1;
float NetMagRes = 1;
float NetMechRes = 1;
float NetPhysRes = 1;
float NetPSNRes = 1;
float NetWaterRes = 1;
float NetRes = 1;
float NetDamage = 0;
NetAirRes = other.gameObject.GetComponent ().AirPierce / gameObject.GetComponent ().AirRes;
NetDarkRes = other.gameObject.GetComponent ().DarkPierce / gameObject.GetComponent ().DarkRes;
NetEarthRes = other.gameObject.GetComponent ().EarthPierce / gameObject.GetComponent ().EarthRes;
NetFireRes = other.gameObject.GetComponent ().FirePierce / gameObject.GetComponent ().FireRes;
NetIceRes = other.gameObject.GetComponent ().IcePierce / gameObject.GetComponent ().IceRes;
NetLightRes = other.gameObject.GetComponent ().LightPierce / gameObject.GetComponent ().LightRes;
NetMagRes = other.gameObject.GetComponent ().MagPierce / gameObject.GetComponent ().MagResistance;
NetMechRes = other.gameObject.GetComponent ().MechPierce / gameObject.GetComponent ().MechRes;
NetPhysRes = other.gameObject.GetComponent ().PhysPierce / gameObject.GetComponent ().PhysResistance;
NetPSNRes = other.gameObject.GetComponent ().PSNPierce / gameObject.GetComponent ().PSNRes;
NetWaterRes = other.gameObject.GetComponent ().WaterPierce / gameObject.GetComponent ().WaterRes;
NetRes = ( NetAirRes + NetDarkRes + NetEarthRes + NetFireRes + NetFireRes + NetIceRes + NetLightRes + NetMagRes + NetMechRes + NetPhysRes + NetPSNRes + NetWaterRes ) / 11;
NetDamage = NetRes * other.gameObject.GetComponent ().Damage;
if ( Shield > 0 )
Shield -= ( NetDamage / gameObject.GetComponent ().ShieldRed );
if ( Shield <= 0 )
Armor -= ( NetDamage / gameObject.GetComponent ().ArmorRed );
if ( Armor <= 0 )
Health -= ( NetDamage / gameObject.GetComponent ().Defence );
}
}
The problem seems to be at the OnCollsionEnter area at the bottom.
↧