Networking - Function runs on server but not on client?

I am trying to sync shooting across server and client. The shooting from the client shows up on the server but not on the client. Here is my function for shooting:

	[Command]
	public void CmdFire() {
		if (fireTimer < fireRate || currentBullets < 0 || isReloading) {
			return;
		}

		RaycastHit hit;
		if (Physics.Raycast (shootPoint.position, shootPoint.transform.forward, out hit, range)) {
			Debug.Log (hit.transform.name + "found");
			GameObject bloodSpill = null;
			if (hit.transform.tag.Equals ("Human")) {
				bloodSpill = Instantiate (bloodEffect, hit.point, Quaternion.FromToRotation (Vector3.forward, hit.normal));
				bloodSpill.transform.Rotate (180f, 0f, 0f);
				hit.transform.gameObject.GetComponent<BetaEnemyScript> ().health -= (int)damage;
			} else {
				GameObject hitParticleEffect = Instantiate (hitParticles, hit.point, Quaternion.FromToRotation (Vector3.up, hit.normal));
				GameObject bulletHoleEffect = Instantiate (bulletImpact, hit.point, Quaternion.FromToRotation (Vector3.forward, hit.normal));
				bulletHoleEffect.transform.SetParent (hit.transform);
				Destroy (hitParticleEffect, 1f);
				Destroy (bulletHoleEffect, 3f);
			}
			if (bloodSpill != null)
				Destroy (bloodSpill, 1.5f);
		}

		gunAnimator.CrossFadeInFixedTime ("Firing", 0.01f);

		muzzleFlash.Play ();
		PlayShootSound ();
		currentBullets--;
		// Reset fire timer
		fireTimer = 0.0f;
	}

What is the issue here? I am aware that command sends the code to be run on the server, but does it not also run it on the client? If so, I want it to run on the client too so I can at least see the muzzle flash and have the animation play. How can I accomplish this?

[Command] always run on server and never execute on Client anymore !
To make sure it happen on Client, there is two way :

  1. Using NetworkServer.Spawn() after Instantiate on Server to make Client also spawn it
  2. Using RpcClient or TargetRpc to send a callback to client, make it spawn same like server.

In your code, it’s missing both two way. And if you looking for solution 1, it’s simplicity like this flow:

  1. Client Register bullet prefab.
  2. Client send Command function
  3. Server Instantiate bullet
  4. Sever call NetworkServer.Spawn to make it spawn in all client
  5. Client receive Spawn handler and spawn same like server.

Problem solved.

All I had to do was duplicate the CmdFire to Fire like this:

     public void Fire() {
         if (fireTimer < fireRate || currentBullets < 0 || isReloading) {
             return;
         }
 
         RaycastHit hit;
         if (Physics.Raycast (shootPoint.position, shootPoint.transform.forward, out hit, range)) {
             Debug.Log (hit.transform.name + "found");
             GameObject bloodSpill = null;
             if (hit.transform.tag.Equals ("Human")) {
                 bloodSpill = Instantiate (bloodEffect, hit.point, Quaternion.FromToRotation (Vector3.forward, hit.normal));
                 bloodSpill.transform.Rotate (180f, 0f, 0f);
                // hit.transform.gameObject.GetComponent<BetaEnemyScript> ().health -= (int)damage;
             } else {
                 GameObject hitParticleEffect = Instantiate (hitParticles, hit.point, Quaternion.FromToRotation (Vector3.up, hit.normal));
                 GameObject bulletHoleEffect = Instantiate (bulletImpact, hit.point, Quaternion.FromToRotation (Vector3.forward, hit.normal));
                 bulletHoleEffect.transform.SetParent (hit.transform);
                 Destroy (hitParticleEffect, 1f);
                 Destroy (bulletHoleEffect, 3f);
             }
             if (bloodSpill != null)
                 Destroy (bloodSpill, 1.5f);
         }
 
         gunAnimator.CrossFadeInFixedTime ("Firing", 0.01f);
 
         muzzleFlash.Play ();
         PlayShootSound ();
         currentBullets--;
         // Reset fire timer
         fireTimer = 0.0f;
     }

I removed the damage subtraction line since it’s already synced from the server anyways. After that, you place this Fire() underneath the CmdFire() call and make sure that CmdFire() is only called if it’s not a server. Therefore, the animations will occur on the server and client.