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?
Answer by Russellinho · Sep 13, 2017 at 02:10 PM
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.
Answer by GunLengend · Sep 13, 2017 at 03:57 AM
[Command] always run on server and never execute on Client anymore ! To make sure it happen on Client, there is two way :
Using NetworkServer.Spawn() after Instantiate on Server to make Client also spawn it
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:
Client Register bullet prefab.
Client send Command function
Server Instantiate bullet
Sever call NetworkServer.Spawn to make it spawn in all client
Client receive Spawn handler and spawn same like server.
Your answer
![](https://koobas.hobune.stream/wayback/20220612134732im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problema multiplayer al agregar vehiculo 0 Answers
Server in Visual Studio and Client in Unity ?,server Visual Studio and Client unity3D ? 0 Answers
Testing my multiplayer with someone across the country 1 Answer
Unet: [Command] not running 1 Answer
Networking - Host can spawn objects but client cant 0 Answers