vehicle explosion logic done - vehicle parts are detached, have rigid bodies, and explosion force is applied to them

This commit is contained in:
in0finite 2020-06-20 16:16:11 +02:00
parent 5ba7751ef6
commit 41856f3def
3 changed files with 61 additions and 1 deletions

View file

@ -95,3 +95,7 @@ MonoBehaviour:
controlInputOnLocalPlayer: 1
controlWheelsOnLocalPlayer: 1
vehicleSyncRate: 20
explosionForceMultiplier: 700
explosionLeftoverPartsLifetime: 30
explosionLeftoverPartsMaxDepenetrationVelocity: 15
explosionLeftoverPartsMass: 100

View file

@ -21,6 +21,12 @@ namespace SanAndreasUnity.Behaviours.Vehicles
public float vehicleSyncRate = 20;
public float explosionForceMultiplier = 0.15f;
public float explosionLeftoverPartsLifetime = 20f;
public float explosionLeftoverPartsMaxDepenetrationVelocity = 15f;
public float explosionLeftoverPartsMass = 10f;
void Awake()
{

View file

@ -1,4 +1,5 @@
using SanAndreasUnity.Utilities;
using System.Linq;
using SanAndreasUnity.Utilities;
using UnityEngine;
namespace SanAndreasUnity.Behaviours.Vehicles
@ -87,9 +88,58 @@ namespace SanAndreasUnity.Behaviours.Vehicles
m_alreadyExploded = true;
// destroy this game object
Object.Destroy(this.gameObject);
// detach the following parts:
// - doors
// - wheels
// - bonnet
// - boot
// - windscreen
// - exhaust
string[] startingNames = new string[] { "door_", "wheel_", "bonnet_", "boot_", "windscreen_", "exhaust_" };
Vector3 explosionCenter = this.transform.position;
float explosionForce = Mathf.Sqrt(this.HandlingData.Mass) * VehicleManager.Instance.explosionForceMultiplier;
float explosionRadius = 10f;
foreach (var frame in _frames)
{
if (!frame.gameObject.activeInHierarchy)
continue;
if (!startingNames.Any(n => frame.gameObject.name.StartsWith(n)))
continue;
var meshFilter = frame.GetComponentInChildren<MeshFilter>();
if (null == meshFilter)
continue;
if (!meshFilter.gameObject.activeInHierarchy)
continue;
meshFilter.transform.SetParent(null, true);
meshFilter.gameObject.name = "vehicle_part_" + meshFilter.gameObject.name;
meshFilter.gameObject.layer = UnityEngine.LayerMask.NameToLayer("Default");
var meshCollider = meshFilter.gameObject.GetOrAddComponent<MeshCollider>();
meshCollider.convex = true;
meshCollider.sharedMesh = meshFilter.sharedMesh;
var rigidBody = meshFilter.gameObject.GetOrAddComponent<Rigidbody>();
rigidBody.mass = VehicleManager.Instance.explosionLeftoverPartsMass;
rigidBody.drag = 0.05f;
rigidBody.maxDepenetrationVelocity = VehicleManager.Instance.explosionLeftoverPartsMaxDepenetrationVelocity;
rigidBody.AddExplosionForce(explosionForce, explosionCenter, explosionRadius);
Object.Destroy(meshFilter.gameObject, VehicleManager.Instance.explosionLeftoverPartsLifetime);
}
// add rigid body to them and apply force
// create explosion effect
}
}