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 Moaid_T4 · Feb 16, 2016 at 06:02 AM · networkingmultiplayerfirst person shooter

spawning a gun in a multiplayer game

hello, im trying to make some sort of a first person shooter game, i can spawn the player correctly with the isLocalPlayer bool set to true and all, but im not really sure how to spawn the gun, im using this code but its not working when a client connects

 void Start()
     {
         hc = transform.parent.parent.GetComponent<HunterController>();
         if (localPlayerAuthority)
         {
             print("its a me local player");
             if (!shotPos.GetComponent<AudioSource>())
                 aud = shotPos.gameObject.AddComponent<AudioSource>();
             else
                 aud = shotPos.GetComponent<AudioSource>();
         }
         else
         {
             print("client");
         }
     }

whats the correct way of spawning a gun and assigning it to the local player, i want to spawn the gun because the gun prefab its self has its own shoot logic and i want that logic to be able to make damage to players, thanks.

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
1
Best Answer

Answer by Salmjak · Feb 16, 2016 at 09:19 AM

When a player want to spawn something you should use a [Command] function with NetworkServer.Spawn() inside of it. [Command] let players send functions to be run at the server. You then have to use [ClientRpc] to send information from the server to all clients, such as scale, rotation, transform.parent, new components, etc. Make sure to add the prefab to your NetworkManager-component, under "Registered Spawnable Prefabs" and add a NetworkIdentity-component to your gun-prefab. It would look something like this:

 [Command]
     void CmdSpawnGun(){
     GameObject obj = Instantiate(gunPrefab, transform.position, Quaternion.identity) as GameObject; //sometimes you might have to exchange gunPrefab with Resources.Load(gunPrefab.name, typeof(gameobject))
     obj.transform.parent = transform.parent //make the player calling this function the parent
     NetworkServer.Spawn(obj);
     RpcSetParent(obj, gameObject) //you need to set the parent on all other clients after the object have been spawned
     } [ClientRpc]

void RpcSetParent(GameObject obj, GameObject parent){ obj.transform.parent = parent.transform; }

Please also update your code-sample, it doesn't include any spawning or instantiating at all, so that's why it's not working. Unless your gun is an AudioSource-component?

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

76 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

Related Questions

Only Host Can Damage Players In A Network Match 0 Answers

Multiplayer First Person shooter similar to Battlefield 1 Answer

only game host can damage clients 1 Answer

Using syncVar in networkManager 1 Answer

Unity Network Spawning Prefabs 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