public static void TryInflictDamage(RaycastHit hit, GameObject owner, float damage, float inflictForce) { Inflictable hitObject = CommonUtil.GetComponentFromSelfOrParent <Inflictable>(hit.collider); if (hitObject == null) { return; } Transform damgeProviderTransform = owner.transform; // Inflict damage when hit a damageable. if (hitObject is Damageable) { Health hp = CommonUtil.GetComponentFromSelfOrParent <Health>(hitObject); hp.Damage(damage, owner); // Signal inflict ((Damageable)hitObject).OnDamaged(owner); } else { // Hit an inflictable with no Hp bar, apply force if a rigidbody is attached on it. Rigidbody rigidbody = hit.rigidbody; if (rigidbody != null) { rigidbody.AddForce(damgeProviderTransform.forward * inflictForce, ForceMode.Force); } // Signal inflict hitObject.OnInflicted(owner); } }
private void OnControllerColliderHit(ControllerColliderHit hit) { Inflictable inf = hit.collider.gameObject.GetComponent <Inflictable>(); if (inf != null) { inf.OnInflicted(gameObject); } }