sync projectile

This commit is contained in:
in0finite 2020-12-26 18:45:42 +01:00
parent 0c751b0208
commit 7cba029b92
5 changed files with 89 additions and 10 deletions

View file

@ -67,6 +67,7 @@ MonoBehaviour:
- {fileID: 5998353205578393877, guid: 919ee6fe912ab4940abdedd633508610, type: 3}
- {fileID: 100010, guid: 2894e2899fe860745afbc810459e2cc2, type: 3}
- {fileID: 100010, guid: 4f8c6e508a3f69243ab7b8813398b7ed, type: 3}
- {fileID: 8038540346792760480, guid: 858b1226127ef9f4e8d16c9610e691b6, type: 3}
--- !u!114 &4862460140142645991
MonoBehaviour:
m_ObjectHideFlags: 0

View file

@ -4594,6 +4594,8 @@ GameObject:
- component: {fileID: 223436154079187918}
- component: {fileID: 5633959673307472571}
- component: {fileID: 1740834482409443228}
- component: {fileID: 4459588035354191565}
- component: {fileID: 4032372881333096458}
m_Layer: 14
m_Name: Projectile
m_TagString: Untagged
@ -4762,3 +4764,32 @@ AudioSource:
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
--- !u!114 &4459588035354191565
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8038540346792760480}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9b91ecbcc199f4492b9a91e820070131, type: 3}
m_Name:
m_EditorClassIdentifier:
serverOnly: 0
localPlayerAuthority: 0
m_AssetId: 858b1226127ef9f4e8d16c9610e691b6
m_SceneId: 0
--- !u!114 &4032372881333096458
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8038540346792760480}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 665fa8c1c261a9a4dbd28a2cb558e853, type: 3}
m_Name:
m_EditorClassIdentifier:
syncInterval: 0.1

View file

@ -21,8 +21,8 @@ namespace SanAndreasUnity.Behaviours
[SerializeField] private Transform m_modelAttachTransform = null;
private bool m_alreadyExploded = false;
private Rigidbody m_rigidBody;
private AudioSource m_audioSource;
public Rigidbody RigidBody { get; private set; }
public AudioSource AudioSource { get; private set; }
public static Projectile Create(
@ -33,14 +33,16 @@ namespace SanAndreasUnity.Behaviours
Geometry.GeometryParts model,
Ped shooterPed)
{
NetStatus.ThrowIfNotOnServer();
var go = Instantiate(prefab, position, rotation);
var projectile = go.GetComponentOrThrow<Projectile>();
if (audioClip != null)
{
projectile.m_audioSource.clip = audioClip;
projectile.m_audioSource.Play();
projectile.AudioSource.clip = audioClip;
projectile.AudioSource.Play();
}
if (shooterPed != null)
@ -63,14 +65,16 @@ namespace SanAndreasUnity.Behaviours
void Awake()
{
m_rigidBody = this.GetComponentOrThrow<Rigidbody>();
m_audioSource = this.GetComponentOrThrow<AudioSource>();
this.RigidBody = this.GetComponentOrThrow<Rigidbody>();
this.AudioSource = this.GetComponentOrThrow<AudioSource>();
}
private void Start()
{
Destroy(this.gameObject, this.lifeTime);
m_rigidBody.velocity = this.transform.forward * this.speed;
if (NetStatus.IsServer)
Destroy(this.gameObject, this.lifeTime);
this.RigidBody.velocity = this.transform.forward * this.speed;
}
private void Update()
@ -81,6 +85,9 @@ namespace SanAndreasUnity.Behaviours
private void OnCollisionEnter(Collision other)
{
if (!NetStatus.IsServer)
return;
if (m_alreadyExploded)
return;
@ -108,8 +115,7 @@ namespace SanAndreasUnity.Behaviours
var psm = explosionGo.GetComponentOrThrow<ParticleSystemMultiplier>();
psm.multiplier = this.particleSystemMultiplier;
if (NetStatus.IsServer)
NetManager.Spawn(explosionGo);
NetManager.Spawn(explosionGo);
// assign explosion sound
F.RunExceptionSafe(() => Vehicle.AssignExplosionSound(explosionGo));

View file

@ -0,0 +1,30 @@
using Mirror;
using SanAndreasUnity.Behaviours;
using SanAndreasUnity.Behaviours.Vehicles;
using SanAndreasUnity.Utilities;
using UnityEngine;
namespace SanAndreasUnity.Net
{
public class NetworkedProjectile : NetworkBehaviour
{
private Projectile m_projectile;
private void Awake()
{
m_projectile = this.GetComponentOrThrow<Projectile>();
if (!NetStatus.IsServer)
{
F.RunExceptionSafe(() => Vehicle.AssignExplosionSound(this.gameObject));
// disable all colliders
var colliders = this.gameObject.GetComponentsInChildren<Collider>();
foreach (var c in colliders)
{
c.enabled = false;
}
}
}
}
}

View file

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