From 9678faa18a89d57a087319f62b4a01737d48f6ce Mon Sep 17 00:00:00 2001 From: in0finite Date: Sun, 21 Jun 2020 18:21:15 +0200 Subject: [PATCH] inflict damage to nearby objects --- .../Behaviours/Vehicles/Vehicle_Damage.cs | 4 +++ Assets/Scripts/Utilities/Damageable.cs | 32 +++++++++++++++++++ Assets/UnityStandardAssets | 2 +- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Assets/Scripts/Behaviours/Vehicles/Vehicle_Damage.cs b/Assets/Scripts/Behaviours/Vehicles/Vehicle_Damage.cs index f1cd0916..088e140e 100644 --- a/Assets/Scripts/Behaviours/Vehicles/Vehicle_Damage.cs +++ b/Assets/Scripts/Behaviours/Vehicles/Vehicle_Damage.cs @@ -182,6 +182,10 @@ namespace SanAndreasUnity.Behaviours.Vehicles this.HandlingData.Mass * 0.8f); } + // inflict damage to nearby objects + + Damageable.InflictDamageToObjectsInArea(explosionCenter, 6f, this.HandlingData.Mass); + // create explosion effect GameObject explosionGo = Object.Instantiate(VehicleManager.Instance.explosionPrefab, this.transform.position, this.transform.rotation); diff --git a/Assets/Scripts/Utilities/Damageable.cs b/Assets/Scripts/Utilities/Damageable.cs index 9dadb292..0662bea9 100644 --- a/Assets/Scripts/Utilities/Damageable.cs +++ b/Assets/Scripts/Utilities/Damageable.cs @@ -44,6 +44,38 @@ namespace SanAndreasUnity.Utilities } } + public static void InflictDamageToObjectsInArea(Vector3 center, float radius, float damageAmount) + { + Collider[] colliders = Physics.OverlapSphere(center, radius); + + var damagables = new HashSet(); + + foreach (var collider in colliders) + { + var damagable = collider.GetComponentInParent(); + if (damagable != null && !damagables.Contains(damagable)) + { + damagables.Add(damagable); + } + } + + foreach (var damageable in damagables) + { + //Collider collider = pair.Value; + + //Vector3 closestPointOnCollider = collider.ClosestPoint(center); + //float distanceToPointOnCollider = Vector3.Distance(center, closestPointOnCollider); + //float distanceToCollider = Vector3.Distance(center, collider.transform.position); + + float distance = Vector3.Distance(center, damageable.transform.position); + float distanceFactor = 1.0f - Mathf.Clamp01(distance / radius); + float damageAmountBasedOnDistance = damageAmount * distanceFactor; + + F.RunExceptionSafe(() => damageable.Damage(new DamageInfo() { amount = damageAmountBasedOnDistance })); + } + + } + } } diff --git a/Assets/UnityStandardAssets b/Assets/UnityStandardAssets index e2abbfcf..95287f6a 160000 --- a/Assets/UnityStandardAssets +++ b/Assets/UnityStandardAssets @@ -1 +1 @@ -Subproject commit e2abbfcf853f5c7b3a5fe891df0b08ffa396afd4 +Subproject commit 95287f6ab37fe3261d7a517551633ee4b329ab2e