Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Particle system in netcode for gameobjects

Discussion in 'Netcode for GameObjects' started by cesar121321432, Jul 24, 2023.

  1. cesar121321432

    cesar121321432

    Joined:
    Jul 11, 2020
    Posts:
    20
    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:


    public void shoot_muzzleflash()
    {
    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
     
  2. Mj-Kkaya

    Mj-Kkaya

    Joined:
    Oct 10, 2017
    Posts:
    175
    Hi @cesar121321432 ,
    Did you read this documentation page?
    "Creating a method that ends with the ServerRpc suffix within a NetworkBehaviour derived class."
     
  3. cesar121321432

    cesar121321432

    Joined:
    Jul 11, 2020
    Posts:
    20
    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.
     
  4. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    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.
    public void CallToCmdStartParticlesServerRPC()
    {
    CmdStartParticlesServerRPC();
    }
     
  5. cesar121321432

    cesar121321432

    Joined:
    Jul 11, 2020
    Posts:
    20
    that makes selectable, but when i play the game neither the owner or other players can see the muzzle flash
     
  6. cesar121321432

    cesar121321432

    Joined:
    Jul 11, 2020
    Posts:
    20
    Can someone just give me an example, ive been stuck with this for 2 weeks now
     
  7. Mj-Kkaya

    Mj-Kkaya

    Joined:
    Oct 10, 2017
    Posts:
    175
    Hi there,
    First all of, you have to share really code of your project.
    We don't figure out what happens in your code.
     
  8. cesar121321432

    cesar121321432

    Joined:
    Jul 11, 2020
    Posts:
    20

    'you have to share really code of your project'

    what does that mean???
     
  9. cesar121321432

    cesar121321432

    Joined:
    Jul 11, 2020
    Posts:
    20
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using TMPro;
    4. using Unity.Mathematics;
    5. using Unity.Netcode;
    6. using UnityEngine;
    7.  
    8. public class Makarov : NetworkBehaviour
    9. {
    10.     public Animator anim;
    11.     public AudioSource src;
    12.     public AudioClip shoot_clip;
    13.     public ParticleSystem muzzleFlash;
    14.     public ParticleSystem smoke;
    15.     public Transform gun;
    16.     public Vector3 gunaimPos;
    17.     public quaternion pivot;
    18.     public Transform player;
    19.     public float AimSpeed = 5;
    20.    
    21.     public Transform shootpoint;
    22.     public GameObject hitEffect;
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.         gunaimPos = new Vector3(0.117f, 0.018f, 0.083f);
    27.         gameObject.GetComponent<NetworkObject>().Spawn();
    28.    
    29.     }
    30.  
    31.  
    32.     // Update is called once per frame
    33.     void Update()
    34.     {
    35.  
    36.      
    37.         player.localRotation = Quaternion.Lerp(player.localRotation, pivot, AimSpeed * Time.deltaTime);
    38.         gun.localPosition = Vector3.Slerp(transform.localPosition, gunaimPos, AimSpeed * Time.deltaTime);
    39.         if (Input.GetMouseButtonDown(0) && IsLocalPlayer)
    40.         {
    41.          
    42.                 anim.Play("Makarov_Shoot");
    43.            
    44.            
    45.          
    46.         }
    47.         if (Input.GetMouseButtonDown(1))
    48.  
    49.         {
    50.             if (IsOwner)
    51.             {
    52.                 gunaimPos = new Vector3(0.00400000019f, 0.209999993f, 0.166615874f);
    53.                 // gun.localPosition = new Vector3(0.002f, 0.134f, 0.083f);
    54.             }
    55.         }
    56.         if (Input.GetMouseButtonUp(1))
    57.         {
    58.             if (IsOwner)
    59.             {
    60.                 gunaimPos = new Vector3(0.117f, 0.018f, 0.083f);
    61.                 //gun.localPosition = new Vector3(0.117f, 0.018f, 0.083f);
    62.             }
    63.         }
    64.         /*
    65.         if (Input.GetKeyDown("q"))
    66.         {
    67.             pivot = new quaternion(player.localRotation.x, player.localRotation.y, -10, 0);
    68.  
    69.        
    70.         if (Input.GetKeyUp("q"))
    71.         {
    72.             pivot = new quaternion(player.localRotation.x, player.localRotation.y, 0, 0);
    73.  
    74.         }
    75.         if (Input.GetKeyDown("e"))
    76.         {
    77.             pivot = new quaternion(player.localRotation.x, player.localRotation.y, 10, 0);
    78.  
    79.         }
    80.         if (Input.GetKeyUp("e"))
    81.         {
    82.             pivot = new quaternion(player.localRotation.x, player.localRotation.y, 0, 0);
    83.  
    84.         }
    85.         */
    86.     }
    87.  
    88.    
    89.     public void raycast_shoot()
    90.     {
    91.         RaycastHit hit;
    92.         if (Physics.Raycast(shootpoint.transform.position, shootpoint.transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity))
    93.         {
    94.             Debug.DrawRay(shootpoint.transform.position, shootpoint.transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
    95.             Debug.Log("Did Hit");
    96.             Instantiate(hitEffect, hit.point, Quaternion.LookRotation(hit.normal));
    97.         }
    98.         else
    99.         {
    100.             Debug.DrawRay(shootpoint.transform.position, shootpoint.transform.TransformDirection(Vector3.forward) * 1000, Color.white);
    101.             Debug.Log("Did not Hit");
    102.         }
    103.       //  gameObject.GetComponent<NetworkObject>().Spawn();
    104.     }
    105.  
    106.     public void shoot_sound()
    107.     {
    108.    
    109.  
    110.             src.PlayOneShot(shoot_clip);
    111.             //gameObject.GetComponent<NetworkObject>().Spawn();
    112.        
    113.  
    114.     }
    115.     public void shoot_Smoke()
    116.     {
    117.         smoke.Play();
    118.        // smoke.gameObject.GetComponent<NetworkObject>().Spawn();
    119.     }
    120.  
    121.  
    122.  
    123.     [ServerRpc]
    124.     public void CmdStartParticlesServerRpc()
    125.  
    126.     {
    127.  
    128.         RpcStart_shoot_muzzleflashClientRpc();
    129.  
    130.         shoot_muzzleflash();
    131.  
    132.     }
    133.  
    134.  
    135.  
    136.     [ClientRpc]
    137.     public void RpcStart_shoot_muzzleflashClientRpc()
    138.  
    139.     {
    140.  
    141.         shoot_muzzleflash();
    142.  
    143.     }
    144.  
    145.  
    146.  
    147.     public void shoot_muzzleflash()
    148.  
    149.     {
    150.  
    151.         muzzleFlash.Play();
    152.  
    153.  
    154.  
    155.  
    156.  
    157.     }
    158. }
    159.  
    You mean this?
     
  10. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    Does the function
    CmdStartParticlesServerRpc
    get called? If you put a debug.log in there will it log something?
     
    Mj-Kkaya likes this.
  11. cesar121321432

    cesar121321432

    Joined:
    Jul 11, 2020
    Posts:
    20
    figuered it out!
    Code (CSharp):
    1.  
    2.     [ServerRpc]
    3.     public void MuzzleFlashGameobjectServerRpc()
    4.     {
    5.         GameObject go = Instantiate(muzzleflashGobj, shootpoint.transform.position, shootpoint.transform.rotation);
    6.         go.GetComponent<NetworkObject>().Spawn();
    7.  
    8.     }
    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?
     
  12. Mj-Kkaya

    Mj-Kkaya

    Joined:
    Oct 10, 2017
    Posts:
    175
    Hi there,
    Where/When do you call this method? Could you share script?
     
  13. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    change [ServerRPC] to [ServerRpc(RequireOwnership = false)] so the clients can also call it
     
  14. cesar121321432

    cesar121321432

    Joined:
    Jul 11, 2020
    Posts:
    20
    tried that but didnt do anything
     
  15. cesar121321432

    cesar121321432

    Joined:
    Jul 11, 2020
    Posts:
    20
    i call it as an event in an animation for the gun
     
  16. Mj-Kkaya

    Mj-Kkaya

    Joined:
    Oct 10, 2017
    Posts:
    175
    Could you share that script, because the script can be the problem. Maybe there is a restriction about "isowner check" etc.