public void OnMeleeDamage() { Collider[] objectsInRange = Physics.OverlapSphere(transform.position, meleeRadius); List <Destructible> damagedObjects = new List <Destructible>(); // Keep track of what objects have been damaged so we don't do damage multiple times per collider. bool hasPlayedHitEffect = false; foreach (Collider col in objectsInRange) { // Ignore terrain colliders if (col is TerrainCollider) { continue; } // Ignore trigger colliders if (col.isTrigger) { continue; } // Ignore the player's character controller (ie, don't allow hitting yourself) if (col is CharacterController && col.tag == "Player") { continue; } if (!hasPlayedHitEffect) // Only play the hit effect once per melee attack. { // Play hit effects HitEffects hitEffects = col.gameObject.GetComponentInParent <HitEffects>(); if (hitEffects != null && hitEffects.effects.Count > 0) { hitEffects.PlayEffect(HitBy.Axe, transform.position, transform.forward * -1); } hasPlayedHitEffect = true; } // Apply impact force to rigidbody hit Rigidbody rbody = col.attachedRigidbody; if (rbody != null) { rbody.AddForceAtPosition(transform.forward * 3f, transform.position, ForceMode.Impulse); } // Apply damage if object hit was Destructible Destructible destObj = col.gameObject.GetComponentInParent <Destructible>(); if (destObj != null && !damagedObjects.Contains(destObj)) { damagedObjects.Add(destObj); ImpactDamage meleeImpact = new ImpactDamage() { DamageAmount = damageAmount, AdditionalForce = additionalForceAmount, AdditionalForcePosition = transform.position, AdditionalForceRadius = additionalForceRadius }; destObj.ApplyDamage(meleeImpact); } } }
public void ProcessBulletHit(RaycastHit hitInfo, Vector3 bulletDirection) { HitEffects hitEffects = hitInfo.collider.gameObject.GetComponentInParent <HitEffects>(); if (hitEffects != null && hitEffects.effects.Count > 0) { hitEffects.PlayEffect(HitBy.Bullet, hitInfo.point, hitInfo.normal); } // Apply damage if object hit was Destructible Destructible destObj = hitInfo.collider.gameObject.GetComponentInParent <Destructible>(); if (destObj != null) { ImpactDamage bulletImpact = new ImpactDamage() { DamageAmount = Instance.bulletDamage, AdditionalForce = Instance.bulletForcePerSecond, AdditionalForcePosition = hitInfo.point, AdditionalForceRadius = .5f }; destObj.ApplyDamage(bulletImpact); } Vector3 force = bulletDirection * (Instance.bulletForcePerSecond / Instance.bulletForceFrequency); // Apply impact force to rigidbody hit Rigidbody rbody = hitInfo.collider.attachedRigidbody; if (rbody != null) { rbody.AddForceAtPosition(force, hitInfo.point, ForceMode.Impulse); } // Check for Chip-Away Debris ChipAwayDebris chipAwayDebris = hitInfo.collider.gameObject.GetComponent <ChipAwayDebris>(); if (chipAwayDebris != null) { chipAwayDebris.BreakOff(force, hitInfo.point); } }