particle collision events networking
Hello, I'm trying make a multiplayer game with particle collisions. It's an FPS game Using a gun that emits particles and when they collide it emits other particles at the collision location. I followed the Unity particle scripting tutorials to create the core mechanics of the game and my code it pretty much identical. See the link below for the tutorial. I'm using Unet for networking and at the moment I can emit particles for each client but collision is not working. I check the components and when I remove networking code collision works. I can't pass Client RPC or comment to methods that have Particle Collision Events. Particles are not colliding even locally. You can see the code down below.
All I want to do is to collide particle trough network. Everyone will be able to see the particles when they get collided. Also damage other players with particle collisions but I think I can solve that once I understand how I can pass particles through network.
Thanks in advance for the help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class ParticleLuncher : NetworkBehaviour {
ParticleSystem system;
public ParticleSystem splatterParticles;
public Gradient particleColor;
public ParticleDecalPool decalPool;
List<ParticleCollisionEvent> collisionEvents;
// Use this for initialization
void Start () {
if (!isLocalPlayer)
this.enabled = false;
system = GetComponentInChildren<ParticleSystem>();
collisionEvents = new List<ParticleCollisionEvent>();
splatterParticles = FindObjectOfType<SplatOnCollision>().GetComponent<ParticleSystem>();
decalPool = FindObjectOfType<ParticleDecalPool>();
}
void OnParticleCollision(GameObject other)
{
ParticlePhysicsExtensions.GetCollisionEvents(system, other, collisionEvents);
for (int i = 0; i < collisionEvents.Count; i++)
{
decalPool.ParticleHit(collisionEvents[i], particleColor);
EmitAtLocation(collisionEvents[i]);
}
if (!isServer)
return;
//Find Player Health do the damage.
}
void EmitAtLocation(ParticleCollisionEvent PCE)
{
splatterParticles.transform.position = PCE.intersection;
splatterParticles.transform.rotation = Quaternion.LookRotation(PCE.normal);
ParticleSystem.MainModule psMain = splatterParticles.main;
psMain.startColor = particleColor.Evaluate(Random.Range(0f, 1f));
splatterParticles.Emit(1);
}
// Update is called once per frame
void Update () {
if (!isLocalPlayer)
return;
if (Input.GetButton("Fire1"))
{
ParticleSystem.MainModule psMain = system.main;
psMain.startColor = particleColor.Evaluate(Random.Range(0f, 1f));
system.Emit(1);
}
}
}
Particle Pool Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class ParticleDecalPool : NetworkBehaviour {
private int particleDataIndex;
public int maxDecal = 1000;
private ParticleDecalData[] particleData;
public float DecalSizeMin = 0.5f;
public float DecalSizeMax = 1.5f;
public ParticleSystem.Particle[] particles;
private ParticleSystem decalParticleSystem;
// Use this for initialization
void Start () {
particleData = new ParticleDecalData[maxDecal];
particles = new ParticleSystem.Particle[maxDecal];
decalParticleSystem = GetComponent<ParticleSystem>();
for (int i = 0; i < maxDecal; i++)
{
particleData[i] = new ParticleDecalData();
}
}
public void ParticleHit(ParticleCollisionEvent PCE, Gradient colorGradient)
{
SetParticleData(PCE, colorGradient);
DisplayParticle();
}
void SetParticleData(ParticleCollisionEvent PCE, Gradient colorGradient)
{
if (particleDataIndex >= maxDecal)
{
particleDataIndex = 0;
}
//Record Collision pos, rot, size, color
particleData[particleDataIndex].pos = PCE.intersection;
Vector3 particleRotEuler = Quaternion.LookRotation(PCE.normal).eulerAngles;
particleRotEuler.z = Random.Range(0, 360);
particleData[particleDataIndex].rot = particleRotEuler;
particleData[particleDataIndex].size = Random.Range(DecalSizeMin, DecalSizeMax);
particleData[particleDataIndex].color = colorGradient.Evaluate(Random.Range(0f, 1f));
particleDataIndex++;
}
void DisplayParticle()
{
for (int i = 0; i < particleData.Length; i++)
{
particles[i].position = particleData[i].pos;
particles[i].rotation3D = particleData[i].rot;
particles[i].startSize = particleData[i].size;
particles[i].startColor = particleData[i].color;
}
decalParticleSystem.SetParticles(particles, particles.Length);
}
}
Script that emits splashes.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class SplatOnCollision : NetworkBehaviour {
public ParticleSystem particleLauncher;
public Gradient particleColorGradient;
public ParticleDecalPool dropletDecalPool;
List<ParticleCollisionEvent> collisionEvents;
void Start ()
{
collisionEvents = new List<ParticleCollisionEvent> ();
}
void OnParticleCollision(GameObject other)
{
int numCollisionEvents = ParticlePhysicsExtensions.GetCollisionEvents (particleLauncher, other, collisionEvents);
int i = 0;
while (i < numCollisionEvents)
{
dropletDecalPool.ParticleHit(collisionEvents[i], particleColorGradient);
i++;
}
}
}