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 /
avatar image
0
Question by JoeD86 · May 22, 2018 at 11:39 PM · networkingmultiplayercommand

Commands being called with the host, but not a client.

I'm putting together a Multiplayer Shooter and for some reason, when I call the function to shoot a bullet, it works with the host player, but not the client player. Here is the function in question

     [Command]
     void CmdFire()
     {
         
         ammoInClip--;
         updateAmmoCounter();
         Vector3 pos = cameraObject.transform.position;
         Vector3 rot = transform.eulerAngles;
         float drift = 0.002f * (100f - weaponRef.weaponList[0].getAccuracy());
         Vector3 aimDrift = new Vector3(Random.Range(-drift, drift), Random.Range(-drift, drift), 0f);
 
         GameObject newBullet = Instantiate(
             bulletObject,
             pos,
             Quaternion.Euler(rot));
         NetworkServer.Spawn(newBullet);
         //newBullet.transform.rotation(Quaternion.Euler(aimDrift));
     }

And here is the rest of the script (Pardon the mess)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 using UnityEngine.Networking;
 
 public class Player_GunController : NetworkBehaviour, IPointerDownHandler
 {
 
     public Animation augCon;
     public GameObject shootPoint;
     public ParticleSystem shootParticles;
     private Weapons weaponRef;
     public Animation gunAnim;
     public WeaponAnimClips animClips;
     public GameObject cameraObject;
     public GameObject hitTest;
     public GameObject bulletObject;
     public Text ammoCounter;
     private int ammoInClip;
 
     bool fire,startReload;
     float fireRate;
 
     public Image fireButton;
 
 
     void Start()
     {
         
         if (!isLocalPlayer && !GetComponent<Player_Movement>().isSinglePlayer)
         {
             Destroy(this);
         }
 
         
         weaponRef = Weapons.Instance;
       //  shootParticles = shootPoint.GetComponentInChildren<ParticleSystem>();
         gunAnim = GetComponentInChildren<Animation>();
         animClips = GetComponentInChildren<WeaponAnimClips>();
 
        // gunAnim.Play(animClips.clips[0].name);
         ammoInClip = weaponRef.weaponList[0].getClipSize();
         updateAmmoCounter();
     }
 
     void Update()
     {
         if (isLocalPlayer || GetComponent<Player_Movement>().isSinglePlayer)
         {
 
             if (Input.GetAxis("Fire1") > 0 && !startReload)
             {
                 
                 if (fireRate <= 0)
                 {
                   //  gunAnim.Rewind();
                    // gunAnim.Play(animClips.clips[Random.Range(3, 5)].name);
                     if (ammoInClip <= 0 && !startReload)
                     {
                         fire = false;
                         startReload = true;
                         reloadGun();
                     }
                     else if (!startReload)
                     {
 
                         CmdFire();
                     }
                     fireRate = weaponRef.weaponList[0].getFireRate();
 
                 }
                 else
                 {
                     fireRate -= Time.deltaTime;
                 }
 
 
 
             }
             if (gunAnim != null && !gunAnim.IsPlaying(animClips.clips[2].name) && startReload)
             {
                 ammoInClip = weaponRef.weaponList[0].getClipSize();
                 updateAmmoCounter();
                 startReload = false;
             }
         }
 
         
     }
 
 
 
     public void reloadGun()
     {
        // gunAnim.Play(animClips.clips[2].name);
         startReload = true;
 
     }
 
     [Command]
     void CmdFire()
     {
         
         ammoInClip--;
         updateAmmoCounter();
         Vector3 pos = cameraObject.transform.position;
         Vector3 rot = transform.eulerAngles;
         float drift = 0.002f * (100f - weaponRef.weaponList[0].getAccuracy());
         Vector3 aimDrift = new Vector3(Random.Range(-drift, drift), Random.Range(-drift, drift), 0f);
 
         GameObject newBullet = Instantiate(
             bulletObject,
             pos,
             Quaternion.Euler(rot));
         NetworkServer.Spawn(newBullet);
         //newBullet.transform.rotation(Quaternion.Euler(aimDrift));
     }
 
     /*
     public void CmdshootHitScanner()
     {
         ammoInClip--;
         updateAmmoCounter();
         Vector3 pos = cameraObject.transform.position;
         Vector3 rot = transform.eulerAngles;
         Ray ray;
         RaycastHit hit;
         float drift = 0.002f * (100f - weaponRef.weaponList[0].getAccuracy());
         Vector3 aimDrift = new Vector3(Random.Range(-drift, drift), Random.Range(-drift, drift), 0f);
 
         GameObject newBullet = Instantiate(
             bulletObject,
             pos,
             Quaternion.Euler(cameraObject.transform.TransformDirection(Vector3.forward) + aimDrift));
 
 
         if(Physics.Raycast(pos, cameraObject.transform.TransformDirection(Vector3.forward) + aimDrift,out hit, Mathf.Infinity))
         {
             Instantiate(hitTest,hit.point, Quaternion.identity);
             if (hit.collider.gameObject.tag == "Player")
             {
                // CmdShootPlayer(-5, hit.transform.gameObject);
             }
         }
     }
     */
 
     public void updateAmmoCounter()
     {
        // ammoCounter.text = ammoInClip.ToString() + "/" + weaponRef.weaponList[0].getClipSize().ToString();
     }
 
     public void OnPointerDown(PointerEventData data)
     {
         Debug.Log("PointerDown");
     }
 
 
 }
 
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

1 Reply

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

Answer by Bunny83 · May 23, 2018 at 01:16 AM

Uhm you destroy your script when you're not on the owning players side. That means the script will be destroyed on the server. So the client can't send the command to the server since the script with the command method does not exist anymore on the server.


You should remove that Destroy line. The script must exist on all peers in order to exchange information / call RPCs.

Comment
Add comment · Show 1 · 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 JoeD86 · May 23, 2018 at 01:37 AM 0
Share

Well, I just feel super silly now. Thanks a ton!

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

168 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 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

Unity networking tutorial? 6 Answers

Command Function updates local SyncVar variable late on client 1 Answer

How to do a grab and throw system in a multiplayer game? 1 Answer

Multiplayer push object error. 0 Answers

UNET : SyncVar value not updating, hooks firing 1 Answer


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