How am I able to sync particles from my gun with other clients?
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
How am I able to sync particles from my gun with other clients?
LeNerd
2017-01-04 17:04:42
First of all sorry for my bad english.
I programmed a multiplayer game with photon in which you can shoot with a gun. With the help of tutorials I was able to sync the tranformation and rotation with the other clients. I also can shoot them, so they can die. My only problem is, that when I shoot with my gun only I can see the partical effects and the impact prefab. Now to my question. Can someone explain what I have to do, to sync the partical effects of my gun with the other clients.
Here is the shooting script of my weapon
//
//
//PLS READ MY COMMENTS
using UnityEngine;
using System.Collections;
public class Shoothing : MonoBehaviour
{
public ParticleSystem muzzleFlash;
public GameObject impactPrefab;
Animator anim;
GameObject[] impacts;
int currentImpact = 0;
int maxImpacts = 5;
bool shooting = false;
float damage = 25;
void Start()
{
impacts = new GameObject[maxImpacts];
for (int i = 0; i < maxImpacts; i++)
impacts[i] = (GameObject)Instantiate(impactPrefab);
anim = GetComponentInChildren<Animator>();
}
void Update()
{
AudioSource audio = GetComponent<AudioSource>();
if (Input.GetButtonDown("Fire1")&& !anim.GetCurrentAnimatorStateInfo(0).IsName("autopistol_walk"))
{
//thats the partical effect
muzzleFlash.Play();
anim.SetTrigger("Fire");
shooting = true;
audio.Play();
}
}
void FixedUpdate()
{
if (shooting)
{
shooting = false;
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 200f))
{
//As you can see I already synct the damage with the server and the other clients
if (hit.transform.tag == "Player")
{
hit.transform.GetComponent<PhotonView>().RPC("GetShot", PhotonTargets.All, damage, PhotonNetwork.player.name);
}
impacts[currentImpact].transform.position = hit.point;
// thats the impact prefab
impacts[currentImpact].GetComponent<ParticleSystem>().Play();
if (++currentImpact >= maxImpacts)
currentImpact = 0;
}
}
}
}
Comments
[Deleted User]
2017-01-05 09:19:35
Hi @LeNerd,
muzzleFlash.Play();
is only called on your local client. You need to synchronize this as well to tell others to play the effect, too. In addition you should add if (PhotonNetwork.isMine)
condition to the Update functions to avoid running code on objects that are owned and controlled by other clients.
The PUN Basics Tutorial has some words on shooting in games, too. Maybe you want to check this as well.
I hope that's clear: Call muzzleFlash.Play() when you get shot (for the remote character/weapon which is shooting). Don't send another RPC to do only this...
@Christian_Simon Would you also do Photon.IsMine for health update PunRPC?
Back to top