replace ExplosionPhysicsForce script from Standard Assets with custom script

This commit is contained in:
in0finite 2020-06-28 20:32:30 +02:00
parent f865dd0051
commit 47c44355ba
5 changed files with 83 additions and 4 deletions

View file

@ -23332,12 +23332,13 @@ MonoBehaviour:
m_GameObject: {fileID: 100010} m_GameObject: {fileID: 100010}
m_Enabled: 1 m_Enabled: 1
m_EditorHideFlags: 0 m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 0ac1691131a8c844dafe8b6ace6a172a, type: 3} m_Script: {fileID: 11500000, guid: 788838a8a852c8843ac90fb32e785378, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
explosionForce: 6 explosionForce: 6
upwardsModifier: 2.5 upwardsModifier: 2.5
radius: 10 radius: 10
explosionMultiplier: 1
--- !u!114 &11400000 --- !u!114 &11400000
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View file

@ -1,7 +1,6 @@
using System.Linq; using System.Linq;
using SanAndreasUnity.Utilities; using SanAndreasUnity.Utilities;
using UnityEngine; using UnityEngine;
using UnityStandardAssets.Effects;
namespace SanAndreasUnity.Behaviours.Vehicles namespace SanAndreasUnity.Behaviours.Vehicles
{ {
@ -196,7 +195,7 @@ namespace SanAndreasUnity.Behaviours.Vehicles
// modify strength of explosion based on vehicle mass // modify strength of explosion based on vehicle mass
float forceFactor = Mathf.Sqrt(this.HandlingData.Mass) / Mathf.Sqrt(1500f); float forceFactor = Mathf.Sqrt(this.HandlingData.Mass) / Mathf.Sqrt(1500f);
var physicsForce = explosionGo.GetComponentOrThrow<ExplosionPhysicsForce>(); var physicsForce = explosionGo.GetComponentOrThrow<ExplosionForce>();
physicsForce.explosionForce *= forceFactor; physicsForce.explosionForce *= forceFactor;
physicsForce.upwardsModifier *= forceFactor; physicsForce.upwardsModifier *= forceFactor;

View file

@ -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<Rigidbody, List<Collider>>();
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<Collider>() { 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);
}
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 788838a8a852c8843ac90fb32e785378
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

@ -1 +1 @@
Subproject commit 7d389408a769011a2e91804845ad4d59ee7b8125 Subproject commit c0da37a7b71ef0f6bb492388893dca6db94014ad