Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges