Search Unity

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

Resolved 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. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    244
    @cesar121321432
    If you are you using NetworkAnimator, then changing the animation of the player (i.e. shooting) should automatically synchronize between clients/host (depending upon whether server or owner authoritative).
    A "no additional messaging" approach:
    Add the following (simple) script to your project:

    Code (CSharp):
    1. using UnityEngine;
    2. public class PFXAnimationEvent : MonoBehaviour
    3. {
    4.     public ParticleSystem PFX;
    5.  
    6.     public void OnStartPFX()
    7.     {
    8.         if ( PFX != null )
    9.         {
    10.             PFX.Play();
    11.         }
    12.     }
    13.  
    14.     public void OnEndPFX()
    15.     {
    16.         if (PFX != null)
    17.         {
    18.             PFX.Stop();
    19.         }
    20.     }
    21. }
    Select the prefab that contains the Animator and NetworkAnimator. Add the above script to the same GameObject that the Animator and NetworkAnimator is attached to and assign the PFX property to the particle system that has the firing particle system (assuming it is part of the same prefab).
    Next, open the Animator window by double clicking the AnimatorController set in the Animtor->Controller property. Select the layer that contains your "Firing" animation state and then select the "Firing" state (not sure what you have labeled these so I am gong to refer to it as "Firing" depending upon context). Then double click the associated animation assigned to that state to get the Animation window opened.

    In your "Firing" animation timeline, select a good frame that you would like your PFX to start playing and add an event to that frame time.
    upload_2023-8-31_14-53-21.png
    The downward pointing tag with the + at the bottom right creates an event. When you do this, you will see the Animation Event properties for that event in the inspector view, for the first event (above I assigned it to 0:10) set the method to be invoked to the OnStartPFX method (you might have to scroll down a bit to find it). Then do the same for when you want the firing FX to stop (create a new event at a later time) and then in the inspector view for that newly created event assign the OnEndPFX method to be invoked.

    upload_2023-8-31_15-0-34.png
    upload_2023-8-31_15-0-18.png

    You can get much fancier with this approach... I am just guiding you through the "basics"... but in the end when the "Firing" animation state is playing you should see the PFX start at the specified time in the firing animation and stop at the specified time in the firing animation.

    When the authority of the NetworkAnimator (server or owner) starts the "Firing" animation then when the other non-authority instances start playing that animation they should automatically start and stop the FX at the right time "relative" to when each non-authority instance begins playing the "Firing" animation...

    This approach makes it much easier to synchronize your particle systems with your animations so that they always play at the right "animation time" during the animation without having to send any additional messages and worry about synchronizing the PFX system with the animation...as a matter of fact... with NetworkAnimator you only need to start the animation on the authority side for that to be automatically synchronized on the non-authority instances... so really you don't need to send any messages using this approach.

    Let me know if this approach helps to resolve your issue?
     

    Attached Files:

    Last edited: Sep 1, 2023
  3. JethRow

    JethRow

    Joined:
    Sep 22, 2021
    Posts:
    10
    You need to tell the server which player did that shot, and then you need to get his particle effect from the server and play it, you can do it by getting his client id or network id, im new to networking too but i did it something like this
    RpccallmethodClientRpc
    (ulong playerID)
    NetworkObject player=GetNetworkObject(playerID)
    And through there u can then get to his script function that calls the shot. I hope this helped, the code is not copy pastable i just wrote this on my phone