diff --git a/Assets/Prefabs/Explosion.prefab b/Assets/Prefabs/Explosion.prefab index 0ae5c0e2..a02c3944 100644 --- a/Assets/Prefabs/Explosion.prefab +++ b/Assets/Prefabs/Explosion.prefab @@ -23332,12 +23332,13 @@ MonoBehaviour: m_GameObject: {fileID: 100010} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 0ac1691131a8c844dafe8b6ace6a172a, type: 3} + m_Script: {fileID: 11500000, guid: 788838a8a852c8843ac90fb32e785378, type: 3} m_Name: m_EditorClassIdentifier: explosionForce: 6 upwardsModifier: 2.5 radius: 10 + explosionMultiplier: 1 --- !u!114 &11400000 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Behaviours/Vehicles/Vehicle_Damage.cs b/Assets/Scripts/Behaviours/Vehicles/Vehicle_Damage.cs index c59f82e8..35c84375 100644 --- a/Assets/Scripts/Behaviours/Vehicles/Vehicle_Damage.cs +++ b/Assets/Scripts/Behaviours/Vehicles/Vehicle_Damage.cs @@ -1,7 +1,6 @@ using System.Linq; using SanAndreasUnity.Utilities; using UnityEngine; -using UnityStandardAssets.Effects; namespace SanAndreasUnity.Behaviours.Vehicles { @@ -196,7 +195,7 @@ namespace SanAndreasUnity.Behaviours.Vehicles // modify strength of explosion based on vehicle mass float forceFactor = Mathf.Sqrt(this.HandlingData.Mass) / Mathf.Sqrt(1500f); - var physicsForce = explosionGo.GetComponentOrThrow(); + var physicsForce = explosionGo.GetComponentOrThrow(); physicsForce.explosionForce *= forceFactor; physicsForce.upwardsModifier *= forceFactor; diff --git a/Assets/Scripts/Utilities/ExplosionForce.cs b/Assets/Scripts/Utilities/ExplosionForce.cs new file mode 100644 index 00000000..94916a86 --- /dev/null +++ b/Assets/Scripts/Utilities/ExplosionForce.cs @@ -0,0 +1,68 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace SanAndreasUnity.Utilities +{ + + public class ExplosionForce : MonoBehaviour + { + public float explosionForce = 4; + public float upwardsModifier = 1f; + public float radius = 10f; + public float explosionMultiplier = 1f; + + + private IEnumerator Start() + { + // wait one frame because some explosions instantiate debris which should then + // be pushed by physics force + yield return null; + + float multiplier = this.explosionMultiplier; + + float r = radius * multiplier; + var cols = Physics.OverlapSphere(this.transform.position, r); + + var rigidbodies = new Dictionary>(); + foreach (var col in cols) + { + if (col.attachedRigidbody != null) + { + if (rigidbodies.ContainsKey(col.attachedRigidbody)) + { + rigidbodies[col.attachedRigidbody].Add(col); + } + else + { + rigidbodies.Add(col.attachedRigidbody, new List() { col }); + } + } + } + + foreach (var pair in rigidbodies) + { + Rigidbody rb = pair.Key; + var colliders = pair.Value; + + // apply higher force on objects with higher mass + float massFactor = Mathf.Pow(rb.mass, 0.95f); + + foreach (var collider in colliders) + { + Vector3 closestPointOnCollider = collider.ClosestPoint(this.transform.position); + + Vector3 diff = closestPointOnCollider - this.transform.position; + float distance = diff.magnitude; + float distanceFactor = Mathf.Sqrt(1.0f - Mathf.Clamp01(distance / r)); + + rb.AddForceAtPosition((diff.normalized * explosionForce + Vector3.up * upwardsModifier) * multiplier * distanceFactor * massFactor / colliders.Count, closestPointOnCollider, ForceMode.Impulse); + + } + + } + + } + } + +} diff --git a/Assets/Scripts/Utilities/ExplosionForce.cs.meta b/Assets/Scripts/Utilities/ExplosionForce.cs.meta new file mode 100644 index 00000000..7171a1ec --- /dev/null +++ b/Assets/Scripts/Utilities/ExplosionForce.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 788838a8a852c8843ac90fb32e785378 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UnityStandardAssets b/Assets/UnityStandardAssets index 7d389408..c0da37a7 160000 --- a/Assets/UnityStandardAssets +++ b/Assets/UnityStandardAssets @@ -1 +1 @@ -Subproject commit 7d389408a769011a2e91804845ad4d59ee7b8125 +Subproject commit c0da37a7b71ef0f6bb492388893dca6db94014ad