detached vehicle parts should be synced

This commit is contained in:
in0finite 2020-07-01 23:36:01 +02:00
parent fbd23c0cce
commit 41e5223fc7
9 changed files with 187 additions and 9 deletions

View file

@ -64,6 +64,7 @@ MonoBehaviour:
- {fileID: 1273918895060814, guid: 9d45d42a7408c1e4ab325d6840364b84, type: 3}
- {fileID: 5841438317718401360, guid: abddce9c98c1ba5a992f109cfd8c5a5c, type: 3}
- {fileID: 4054766663391349888, guid: df1f8ef3d99c152cfb414f719d5e2bbf, type: 3}
- {fileID: 5998353205578393877, guid: 919ee6fe912ab4940abdedd633508610, type: 3}
--- !u!114 &4862460140142645991
MonoBehaviour:
m_ObjectHideFlags: 0
@ -131,7 +132,6 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
m_serverPort: 18418
simulateResponding: 0
gameServerPortNumber: 7777
--- !u!114 &2615086005589439192
MonoBehaviour:

View file

@ -0,0 +1,77 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &5998353205578393877
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 5468094862909607614}
- component: {fileID: 6750907620125006804}
- component: {fileID: 3359912204732796237}
- component: {fileID: 7107055912798612425}
m_Layer: 13
m_Name: VehicleExplosionLeftoverPart
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &5468094862909607614
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5998353205578393877}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &6750907620125006804
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5998353205578393877}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9b91ecbcc199f4492b9a91e820070131, type: 3}
m_Name:
m_EditorClassIdentifier:
serverOnly: 0
localPlayerAuthority: 0
m_AssetId:
m_SceneId: 120031090
--- !u!114 &3359912204732796237
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5998353205578393877}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 045da3be85ff76c40a5e8362ec1693ab, type: 3}
m_Name:
m_EditorClassIdentifier:
syncInterval: 0.1
--- !u!114 &7107055912798612425
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5998353205578393877}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 18b3d5884b734d84894d176734f1de9d, type: 3}
m_Name:
m_EditorClassIdentifier:
syncInterval: 0.1

View file

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 919ee6fe912ab4940abdedd633508610
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -95,8 +95,7 @@ MonoBehaviour:
controlInputOnLocalPlayer: 1
controlWheelsOnLocalPlayer: 1
vehicleSyncRate: 20
explosionForceMultiplier: 700
explosionChassisForceMultiplier: 11000
massToHealthExponent: 1
explosionDamageRadius: 7
explosionDamageOverDistanceCurve:
serializedVersion: 2
@ -123,6 +122,8 @@ MonoBehaviour:
m_PostInfinity: 2
m_RotationOrder: 4
explosionMassToDamageExponent: 1
explosionLeftoverPartPrefab: {fileID: 5998353205578393877, guid: 919ee6fe912ab4940abdedd633508610,
type: 3}
explosionLeftoverPartsLifetime: 30
explosionLeftoverPartsMaxDepenetrationVelocity: 15
explosionLeftoverPartsMass: 100

View file

@ -166,6 +166,8 @@ namespace SanAndreasUnity.Behaviours.Vehicles
bool m_isServer => Net.NetStatus.IsServer;
public bool IsControlledByLocalPlayer => Ped.Instance != null && Ped.Instance.CurrentVehicle == this && Ped.Instance.CurrentVehicleSeat.IsDriver;
public Mirror.NetworkIdentity NetIdentity { get; private set; }
public Mirror.NetworkTransform NetTransform { get; private set; }
List<Ped> m_lastPreparedPeds = new List<Ped>();
@ -185,6 +187,7 @@ namespace SanAndreasUnity.Behaviours.Vehicles
private void Awake()
{
this.NetIdentity = this.GetComponentOrThrow<Mirror.NetworkIdentity>();
this.NetTransform = this.GetComponent<Mirror.NetworkTransform>();
_props = new MaterialPropertyBlock();
this.Awake_Damage();

View file

@ -27,6 +27,7 @@ namespace SanAndreasUnity.Behaviours.Vehicles
public AnimationCurve explosionDamageOverDistanceCurve = AnimationCurve.Linear(0f, 1f, 1f, 0f);
[Range(0.1f, 3f)] public float explosionMassToDamageExponent = 1f;
public GameObject explosionLeftoverPartPrefab;
public float explosionLeftoverPartsLifetime = 20f;
public float explosionLeftoverPartsMaxDepenetrationVelocity = 15f;
public float explosionLeftoverPartsMass = 100f;

View file

@ -165,7 +165,7 @@ namespace SanAndreasUnity.Behaviours.Vehicles
if (!startingNames.Any(n => frame.gameObject.name.StartsWith(n)))
continue;
DetachFrameDuringExplosion(frame, VehicleManager.Instance.explosionLeftoverPartsMass);
DetachFrameDuringExplosion(frame, VehicleManager.Instance.explosionLeftoverPartsMass, null);
}
// chassis need to be handled after all other objects are detached, because chassis can sometimes
@ -179,7 +179,7 @@ namespace SanAndreasUnity.Behaviours.Vehicles
}
else
{
DetachFrameDuringExplosion(chassisFrame, this.HandlingData.Mass * 0.8f);
DetachFrameDuringExplosion(chassisFrame, this.HandlingData.Mass * 0.8f, null);
}
// inflict damage to nearby objects
@ -206,7 +206,16 @@ namespace SanAndreasUnity.Behaviours.Vehicles
}
void DetachFrameDuringExplosion(Frame frame, float mass)
public void DetachFrameDuringExplosion(string frameName, float mass, GameObject parentGo)
{
Frame frame = this.Frames.FirstOrDefault(f => f.gameObject.name == frameName);
if (null == frame)
Debug.LogError($"Failed to find frame by name: {frameName}");
else
DetachFrameDuringExplosion(frame, mass, parentGo);
}
void DetachFrameDuringExplosion(Frame frame, float mass, GameObject parentGo)
{
var meshFilter = frame.GetComponentInChildren<MeshFilter>();
if (null == meshFilter)
@ -215,8 +224,12 @@ namespace SanAndreasUnity.Behaviours.Vehicles
if (!meshFilter.gameObject.activeInHierarchy)
return;
meshFilter.transform.SetParent(null, true);
meshFilter.gameObject.name = "vehicle_part_" + meshFilter.gameObject.name;
if (null == parentGo)
parentGo = Object.Instantiate(VehicleManager.Instance.explosionLeftoverPartPrefab, meshFilter.transform.position, meshFilter.transform.rotation);
parentGo.name = "vehicle_part_" + meshFilter.gameObject.name;
meshFilter.transform.SetParent(parentGo.transform, true);
meshFilter.gameObject.layer = UnityEngine.LayerMask.NameToLayer("Default");
var meshCollider = meshFilter.gameObject.GetOrAddComponent<MeshCollider>();
meshCollider.convex = true;
@ -226,7 +239,15 @@ namespace SanAndreasUnity.Behaviours.Vehicles
rigidBody.drag = 0.05f;
rigidBody.maxDepenetrationVelocity = VehicleManager.Instance.explosionLeftoverPartsMaxDepenetrationVelocity;
Object.Destroy(meshFilter.gameObject, VehicleManager.Instance.explosionLeftoverPartsLifetime * Random.Range(0.8f, 1.2f));
if (NetStatus.IsServer)
{
Object.Destroy(parentGo, VehicleManager.Instance.explosionLeftoverPartsLifetime * Random.Range(0.8f, 1.2f));
var netScript = parentGo.GetComponentOrThrow<NetworkedVehicleDetachedPart>();
netScript.InitializeOnServer(this.NetIdentity.netId, frame.gameObject.name, mass, rigidBody);
NetManager.Spawn(parentGo);
}
}
void AssignExplosionSound(GameObject explosionGo)

View file

@ -0,0 +1,57 @@
using Mirror;
using SanAndreasUnity.Behaviours.Vehicles;
using SanAndreasUnity.Utilities;
using UnityEngine;
namespace SanAndreasUnity.Net
{
public class NetworkedVehicleDetachedPart : NetworkBehaviour
{
public NetworkRigidBody NetworkRigidBody { get; private set; }
[SyncVar] uint m_net_vehicleId;
[SyncVar] string m_net_frameName;
[SyncVar] float m_net_mass;
void Awake()
{
this.NetworkRigidBody = this.GetComponentOrThrow<NetworkRigidBody>();
}
public void InitializeOnServer(uint vehicleId, string frameName, float mass, Rigidbody rigidbody)
{
m_net_vehicleId = vehicleId;
m_net_frameName = frameName;
m_net_mass = mass;
this.NetworkRigidBody.Rigidbody = rigidbody;
}
public override void OnStartClient()
{
if (NetStatus.IsServer)
return;
F.RunExceptionSafe(() => this.OnStartClientInternal());
}
void OnStartClientInternal()
{
GameObject vehicleGo = NetManager.GetNetworkObjectById(m_net_vehicleId);
if (null == vehicleGo)
{
// this should not happen because vehicle must have been spawned before detached part
Debug.LogError($"Can not find vehicle object with id {m_net_vehicleId} for detached part with id {this.netId}");
}
else
{
Vehicle vehicle = vehicleGo.GetComponentOrThrow<Vehicle>();
vehicle.DetachFrameDuringExplosion(m_net_frameName, m_net_mass, this.gameObject);
this.NetworkRigidBody.Rigidbody = this.GetComponentInChildren<Rigidbody>();
}
}
}
}

View file

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