using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BNG {
///
/// This collider will Damage a Damageable object on impact
///
public class DamageCollider : MonoBehaviour {
///
/// How much damage to apply to the Damageable object
///
public float Damage = 25f;
///
/// Used to determine velocity of this collider
///
public Rigidbody ColliderRigidbody;
///
/// Minimum Amount of force necessary to do damage. Expressed as relativeVelocity.magnitude
///
public float MinForce = 0.1f;
///
/// Our previous frame's last relative velocity value
///
public float LastRelativeVelocity = 0;
// How much impulse force was applied last onCollision enter
public float LastDamageForce = 0;
///
/// Should this take damage if this collider collides with something? For example, pushing a box off of a ledge and it hits the ground
///
public bool TakeCollisionDamage = false;
///
/// How much damage to apply if colliding with something at speed
///
public float CollisionDamage = 5;
Damageable thisDamageable;
private void Start() {
if (ColliderRigidbody == null) {
ColliderRigidbody = GetComponent();
}
thisDamageable = GetComponent();
}
private void OnCollisionEnter(Collision collision) {
if(!this.isActiveAndEnabled) {
return;
}
OnCollisionEvent(collision);
}
public virtual void OnCollisionEvent(Collision collision) {
LastDamageForce = collision.impulse.magnitude;
LastRelativeVelocity = collision.relativeVelocity.magnitude;
if (LastDamageForce >= MinForce) {
// Can we damage what we hit?
Damageable d = collision.gameObject.GetComponent();
if (d) {
d.DealDamage(Damage, collision.GetContact(0).point, collision.GetContact(0).normal, true, gameObject, collision.gameObject);
}
// Otherwise, can we take damage ourselves from this collision?
else if (TakeCollisionDamage && thisDamageable != null) {
thisDamageable.DealDamage(CollisionDamage, collision.GetContact(0).point, collision.GetContact(0).normal, true, gameObject, collision.gameObject);
}
}
}
}
}