Particle system in netcode for gameobjects

Hello, im developing a realistic fps with multiplayer using netcode for gameobjects.
Im stuck at trying to get the muzzle flash effect to show for the other player, my particle system is attached to the gun and played with this function:

{
muzzleFlash.Play();

}```

and im activating this function in an animation with an event.
I tried this:

[ServerRpc]
public void CmdStartParticles()
{
RpcStart_shoot_muzzleflash();
shoot_muzzleflash();
}

[ClientRpc]
public void RpcStart_shoot_muzzleflash()
{
shoot_muzzleflash();
}

public void shoot_muzzleflash()
{
muzzleFlash.Play();

}```

but then i cant choose the CmdStartParticles() as an event in the animation

im experienced with unity but not with netcode, so sorry if this is a noob question

Hi @cesar121321432 ,
Did you read this documentation page?
“Creating a method that ends with the ServerRpc suffix within a NetworkBehaviour derived class.”

No, because the second code block is prob completely wrong, that why im asking the question
there is nothing on the docs about particle systems as far as i can see.

Try to add a function that calls the ServerRPC function and then you may be able to choose it as an event for your animation.

{
CmdStartParticlesServerRPC();
}```

that makes selectable, but when i play the game neither the owner or other players can see the muzzle flash

Can someone just give me an example, ive been stuck with this for 2 weeks now

Hi there,
First all of, you have to share really code of your project.
We don’t figure out what happens in your code.

‘you have to share really code of your project’

what does that mean???

using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.Mathematics;
using Unity.Netcode;
using UnityEngine;

public class Makarov : NetworkBehaviour
{
    public Animator anim;
    public AudioSource src;
    public AudioClip shoot_clip;
    public ParticleSystem muzzleFlash;
    public ParticleSystem smoke;
    public Transform gun;
    public Vector3 gunaimPos;
    public quaternion pivot;
    public Transform player;
    public float AimSpeed = 5;
   
    public Transform shootpoint;
    public GameObject hitEffect;
    // Start is called before the first frame update
    void Start()
    {
        gunaimPos = new Vector3(0.117f, 0.018f, 0.083f);
        gameObject.GetComponent<NetworkObject>().Spawn();
   
    }

 
    // Update is called once per frame
    void Update()
    {

      
        player.localRotation = Quaternion.Lerp(player.localRotation, pivot, AimSpeed * Time.deltaTime);
        gun.localPosition = Vector3.Slerp(transform.localPosition, gunaimPos, AimSpeed * Time.deltaTime);
        if (Input.GetMouseButtonDown(0) && IsLocalPlayer)
        {
          
                anim.Play("Makarov_Shoot");
           
           
         
        }
        if (Input.GetMouseButtonDown(1))

        {
            if (IsOwner)
            {
                gunaimPos = new Vector3(0.00400000019f, 0.209999993f, 0.166615874f);
                // gun.localPosition = new Vector3(0.002f, 0.134f, 0.083f);
            }
        }
        if (Input.GetMouseButtonUp(1))
        {
            if (IsOwner)
            {
                gunaimPos = new Vector3(0.117f, 0.018f, 0.083f);
                //gun.localPosition = new Vector3(0.117f, 0.018f, 0.083f);
            }
        }
        /*
        if (Input.GetKeyDown("q"))
        {
            pivot = new quaternion(player.localRotation.x, player.localRotation.y, -10, 0);

       
        if (Input.GetKeyUp("q"))
        {
            pivot = new quaternion(player.localRotation.x, player.localRotation.y, 0, 0);

        }
        if (Input.GetKeyDown("e"))
        {
            pivot = new quaternion(player.localRotation.x, player.localRotation.y, 10, 0);

        }
        if (Input.GetKeyUp("e"))
        {
            pivot = new quaternion(player.localRotation.x, player.localRotation.y, 0, 0);

        }
        */
    }

   
    public void raycast_shoot()
    {
        RaycastHit hit;
        if (Physics.Raycast(shootpoint.transform.position, shootpoint.transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity))
        {
            Debug.DrawRay(shootpoint.transform.position, shootpoint.transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
            Debug.Log("Did Hit");
            Instantiate(hitEffect, hit.point, Quaternion.LookRotation(hit.normal));
        }
        else
        {
            Debug.DrawRay(shootpoint.transform.position, shootpoint.transform.TransformDirection(Vector3.forward) * 1000, Color.white);
            Debug.Log("Did not Hit");
        }
      //  gameObject.GetComponent<NetworkObject>().Spawn();
    }
 
    public void shoot_sound()
    {
    

            src.PlayOneShot(shoot_clip);
            //gameObject.GetComponent<NetworkObject>().Spawn();
       

    }
    public void shoot_Smoke()
    {
        smoke.Play();
       // smoke.gameObject.GetComponent<NetworkObject>().Spawn();
    }



    [ServerRpc]
    public void CmdStartParticlesServerRpc()

    {

        RpcStart_shoot_muzzleflashClientRpc();

        shoot_muzzleflash();

    }



    [ClientRpc]
    public void RpcStart_shoot_muzzleflashClientRpc()

    {

        shoot_muzzleflash();

    }



    public void shoot_muzzleflash()

    {

        muzzleFlash.Play();





    }
}

You mean this?

Does the function CmdStartParticlesServerRpc get called? If you put a debug.log in there will it log something?

1 Like

figuered it out!

    [ServerRpc]
    public void MuzzleFlashGameobjectServerRpc()
    {
        GameObject go = Instantiate(muzzleflashGobj, shootpoint.transform.position, shootpoint.transform.rotation);
        go.GetComponent<NetworkObject>().Spawn();

    }

changed the particlesystem to be instantiated at the muzzle point instead of being activated, that worked

but only when the host shoots do the other clients see, but then the client shoots you cant see it on neither screens. anyone know what this could be?

Hi there,
Where/When do you call this method? Could you share script?

change [ServerRPC] to [ServerRpc(RequireOwnership = false)] so the clients can also call it

tried that but didnt do anything

i call it as an event in an animation for the gun

Could you share that script, because the script can be the problem. Maybe there is a restriction about “isowner check” etc.