Created
August 13, 2013 16:53
-
-
Save SteveSwink/6223198 to your computer and use it in GitHub Desktop.
This goes on an object maybe?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class DamageOnCollision : MonoBehaviour { | |
public int damagePerHit = 1; | |
public bool destroyOnCollision = true; | |
public float destroyDelay = 0.1f; | |
void OnCollisionEnter(Collision col){ | |
var heart = col.collider.GetComponent<Heart>(); | |
if(heart != null){ | |
heart.DoDamage(damagePerHit, col); | |
} | |
if(destroyOnCollision) | |
Destroy(gameObject, destroyDelay); | |
} | |
// if this thing is a trigger | |
void OnTriggerEnter(Collider col){ | |
var heart = col.collider.GetComponent<Heart>(); | |
if(heart != null){ | |
heart.DoDamage(damagePerHit, col); | |
} | |
if(destroyOnCollision) | |
Destroy(gameObject, destroyDelay); | |
} | |
// if this thing is a charactercontroller | |
void OnControllerColliderHit(ControllerColliderHit hit){ | |
var heart = hit.collider.GetComponent<Heart>(); | |
if(heart != null){ | |
heart.DoDamage(damagePerHit, hit); | |
} | |
if(destroyOnCollision) | |
Destroy(gameObject, destroyDelay); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment