I tried to use a ServerRpc and a ClientRpc to control a particle system, but it only plays on the client itself, and does not show on other clients.
The particle system is in the player network object
Here is my code:
[SerializeField] private ParticleSystem shootParticles;
public override void Shoot(Vector3 targetPosition)
{
ShowEffectServerRpc();
}
[ServerRpc(RequireOwnership = false)]
private void ShowEffectServerRpc()
{
ShowEffectClientRpc();
}
[ClientRpc]
private void ShowEffectClientRpc()
{
shootParticles.Play();
}
I would suggest splitting things up into multiple classes. Suppose we have a
AvatarStateNetworkBehaviour, aServerAvatarNetworkBehaviour (handling serverside stuff for the player avatar), and aAvatarEffectsMonoBehaviour.I would also refactor your code to put the "Player" Network Object in a completely different object, similar to the "PersistentPlayer" within the Netcode "Boss Room" Example Project (see the official documentation, paying close attention to the point about Core gameplay structure) - with the "Avatar" being a distinct network object. It can still be owned by the player whom owns it, but this way, you can decouple the logical representation of the player from the avatar they're playing with (giving you more freedom to manipulate their avatar without completely breaking things)
I'm also assuming (from the way you have presented your code) that the 'shoot' particle system is some sort of 'emits a few particles and then stops emitting' system.
Now, all you need to do is call the
SendShootInputServerRpcmethod of yourAvatarStatewith whatever position you want to target, and then all of the clients should hopefully see the same few particles emitted at roughly the same time as each other (lag permitting).