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 humra · Dec 06, 2017 at 09:17 PM · networkingmultiplayerfpsweaponchanging

When a host player changes his weapon all players get switched to it

I'm making a multiplayer FPS and I just implemented switching weapons. One player acts as a host for the game. When a client switches his weapon it shows properly on all the other clients (and host player), but when the host player switches his weapon, the weapons of all the players switch to target weapon.

Here is my class that performs the weapon switching:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 
 public class WeaponManager : NetworkBehaviour {
 
     [SerializeField]
     private string weaponLayerName = "Weapon";
     [SerializeField]
     private AssaultRifleScript primaryWeapon;
     [SerializeField]
     private Transform weaponHolder;
     [SerializeField]
     private float weaponSwitchTime = 0.5f;
 
     private Weapons currentWeapon;
     private WeaponGraphics currentGraphics;
     private int currentWeaponIndex = 1;
     public bool isReloading = false;
 
     private void Start()
     {
         EquipWeapon(new AssaultRifleScript());
     }
 
     public void EquipWeapon(Weapons newWeapon)
     {
         currentWeapon = newWeapon;
 
         GameObject weaponInst = (GameObject)Instantiate(newWeapon.graphics, weaponHolder.position, weaponHolder.rotation);
         weaponInst.transform.SetParent(weaponHolder);
 
         currentGraphics = weaponInst.GetComponent<WeaponGraphics>();
         if(currentGraphics == null)
         {
             Debug.LogError("No weapon graphics component on the weapon object: " + weaponInst.name);
         }
 
         if(isLocalPlayer)
         {
             Util.SetLayerRecursive(weaponInst, LayerMask.NameToLayer(weaponLayerName));
         }
     }
 
     private void ClearOldWeapon()
     {
         foreach(Transform child in weaponHolder)
         {
             Destroy(child.gameObject);
         }
     }
 
     [Command]
     public void CmdSwitchWeapon(int selectedWeaponIndex)
     {
         if (selectedWeaponIndex != currentWeaponIndex)
         {
             RpcSwitchWeapon(selectedWeaponIndex);
         }
     }
 
     [ClientRpc]
     public void RpcSwitchWeapon(int selectedWeaponIndex)
     {
         ClearOldWeapon();
 
         switch(selectedWeaponIndex)
         {
             case 1:
                 EquipWeapon(new AssaultRifleScript());
                 break;
             case 2:
                 EquipWeapon(new PistolScript());
                 break;
             case 3:
                 EquipWeapon(new SniperScript());
                 break;
             case 4:
                 EquipWeapon(new HeavyScript());
                 break;
             default:
                 break;
         }
 
         currentWeaponIndex = selectedWeaponIndex;
     }
 }
 

Could anyone tell me what I'm doing wrong?

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 humra · Dec 06, 2017 at 09:47 PM

It turns out to be a silly mistake. All I had to do is add a isLocalPlayer check in another script that actually calls the CmdSwitchWeapons command.

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

104 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

Related Questions

Unity networking tutorial? 6 Answers

Multiplayer projectiles 0 Answers

can firebase real-time database be used on a pc standalone game? 0 Answers

Shoot Projectile Where Camera Faces [PLEASE HELP!] 2 Answers

Photon Networking Instantiating Problem 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