Skip to content

Instantly share code, notes, and snippets.

@SteveSwink
Created August 13, 2013 16:51
Show Gist options
  • Save SteveSwink/6223160 to your computer and use it in GitHub Desktop.
Save SteveSwink/6223160 to your computer and use it in GitHub Desktop.
Heart.cs - handles damage from any type of collision
using UnityEngine;
using System.Collections;
public class Heart : MonoBehaviour {
IDamageEventReciever damageReciever;
public int maxHealth;
int health;
Vector3 impactSumInternal;
Vector3 contactPointInternal;
Vector3 normalInternal;
ContactPoint[] contactsInternal;
void Awake(){
foreach(Component c in gameObject.GetComponents<Component>()){
Debug.Log(" type is " + c.GetType() + " == IDamageEventReceiver? " + (c is IDamageEventReciever));
if(c is IDamageEventReciever){
Debug.Log("^^^^^^^^^ IDamageEventReciever is: " + damageReciever);
damageReciever = (IDamageEventReciever)c;
}
}
}
public int GetHealth(){
return health;
}
public int SetHealth(int newHealth){
health = newHealth;
return health;
}
public int AddHealth(int toAdd){
health += toAdd;
return health;
}
// pass damage
public void DoDamage(int damage){
health -= damage;
damageReciever.TookDamage(damage);
}
// pass the Collider
public void DoDamage(int damage, Collider col){
health -= damage;
damageReciever.TookDamage(damage, col);
Debug.Log("Collider on "+ gameObject.name + " took" + damage + "damage from " + col.gameObject.name + " at " + Time.time);
}
// pass the Collision
public void DoDamage(int damage, Collision col){
health -= damage;
damageReciever.TookDamage(damage, col);
Debug.Log("Trigger on " + gameObject.name + " took" + damage + "damage from " + col.gameObject.name + " at " + Time.time);
}
// pass the CC hit
public void DoDamage(int damage, ControllerColliderHit ccHit){
health -= damage;damageReciever.TookDamage(damage);
damageReciever.TookDamage(damage, ccHit);
Debug.Log("CharacterController on " + gameObject.name + " took" + damage + "damage from " + ccHit.gameObject.name + " at " + Time.time);
}
void GetContactNormalRotation(Quaternion rot){
// Quaternion.FromToRotation(Vector3.up, contact.normal);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment