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 X_Magma · Nov 10, 2017 at 12:34 AM · networkinginstantiateclient

Objects instantiated by the host show on all clients but client objects don't show on host.

Hello, I have this issue where if the host of the room shoots a bullet, it shows on all clients, but when a client shoots a bullet it only shows for them.

Here is my code:

 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Mouse0))
     {
         InvokeRepeating("ShootBullet", 0, 1 / rateOfFire);
     }
     if(Input.GetKeyUp(KeyCode.Mouse0))
     {
         CancelInvoke("ShootBullet");
     }
 }
 [Client]
 void ShootBullet()
 {
     if(!isLocalPlayer)
     {
         return;
     }
     Vector2 mousePosition = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
     Vector3 difference = new Vector3(mousePosition.x, mousePosition.y, 0) - transform.position;
     difference.Normalize();
     float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;

     GameObject _bullet = Instantiate(bullet, transform.position, Quaternion.Euler(0, 0, rotZ));

     //_bullet.GetComponent<Rigidbody2D>().AddForce(_bullet.transform.right * 10);
     _bullet.transform.position += _bullet.transform.right * 10;

     NetworkServer.Spawn(_bullet);

     FindObjectOfType<AudioManager>().Play("Shoot");
 }
 //[Command]
 //void CmdShootBullet()
 //{
 //    InvokeRepeating("RpcShootBullet", 0, 1/rateOfFire);
 //}

Any help please? I feel like it's something wrong with the paramaters ([ClientRpc], [Command], etc)

EDIT: I think I have found the issue, I have updated my code:

public GameObject bullet; public float rateOfFire = 0.10f;

 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Mouse0))
     {
         InvokeRepeating("CmdShootBullet", 0, 1 / rateOfFire);
     }
     if(Input.GetKeyUp(KeyCode.Mouse0))
     {
         CancelInvoke("CmdShootBullet");
     }
 }
 [Command]
 void CmdShootBullet()
 {
     if(!isLocalPlayer)
     {
         return;
     }

     if(!NetworkServer.active)
     {
         Debug.LogError("PlayerShoot: No Network Server Active");
         return;
     }
     Vector2 mousePosition = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
     Vector3 difference = new Vector3(mousePosition.x, mousePosition.y, 0) - transform.position;
     difference.Normalize();
     float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;

     GameObject _bullet = Instantiate(bullet, transform.position, Quaternion.Euler(0, 0, rotZ));

     NetworkServer.Spawn(_bullet);

     _bullet.transform.position += _bullet.transform.right * 5.5f;
     _bullet.GetComponent<Rigidbody2D>().velocity = _bullet.transform.right * _bullet.GetComponent<Bullet>().speed;

     FindObjectOfType<AudioManager>().Play("Shoot");
 }

I was getting an error on the non-host client saying something along the lines of "Can't spawn objects without the NetworkServer active, however I wouldn't get it on the host. If anyone can please help it would be much appreciated.

Thanks.

Comment
Add comment · Show 1
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 Leonstar0 · Nov 10, 2017 at 06:18 AM 0
Share

Sooo... I dunno a lot about this, so I'm not much more help then a rubber duck... buuuuuut.

Here are some questions I have: !isLocalPlayer Is this a stub...? Why is this here? Doesn't it make is so that the function only works for non-local players? What is the definition of a client then, if there can be a local client? And why does that matter? Wait, is the method only meant to be handling bullets shot by other players?

GameObject _bullet = Instantiate(...blah) Does this create the local bullet (from the prefab), for the client? That would explain why clients can see their own bullets.

NetworkServer.Spawn(_bullet); When a client shoots, they can only see their bullet... when the host shoots, all clients can see their bullet... but is the host not a type of client, just connecting to the local server? What is special about the host that enables other clients to see their actions?

Again, I'm just a rubber duck - I haven't really ever fiddled around with this, I'm highly likely to be asking dumb questions ¯_(ツ)_/¯ .

0 Replies

· Add your reply
  • Sort: 

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

72 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

Related Questions

How can I spawn a GameObject on all players in my multiplayer game? 0 Answers

Level Instantiation running on Server and client 0 Answers

Instantiated object from client player not moving when spawned. How can I fix this? 0 Answers

Network.Instantiate issues - GameObject ownership and network views? 0 Answers

Having problem with making non networked objects only be intractable with local player 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