Skip to content

Instantly share code, notes, and snippets.

@SteveSwink
Created August 13, 2013 16:53
Show Gist options
  • Save SteveSwink/6223198 to your computer and use it in GitHub Desktop.
Save SteveSwink/6223198 to your computer and use it in GitHub Desktop.
This goes on an object maybe?
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