• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Russellinho · Sep 12, 2017 at 10:12 PM · fpsservershootingmultiplayer-networkingclient

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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 :

  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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

140 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how can i set the direction of my gun to the Raycast collision point? 1 Answer

Having issues with Photon 0 Answers

UDPClient can't receive and send ? 0 Answers

LAN Auto find server/host 0 Answers

What backend technology should I use for Unity mobile game? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges