1
votes

I have a Player that shoots Bullets, but the Bullets are only being Spawned to all clients when the Player that shoots is the Host.

Using Unity 5.6

What I have so far...

  • I already have the Prefab registered in the NetworkManager.
  • The Prefab has a NetworkIdentity with ServerOnly in false and LocalPlayerAuthority in false.
  • The Prefab has a NetworkTransform with a NetworkSendRate = 0 and a TransformSyncMode to Sync RigidBody2D.
  • The Player shoots the Bullet with a method [Command] and using NetworkServer.Spawn (bulletInstance)
    • Before the NetworkServer.Spawn is called I assign the velocity to the RigidBody2D of the bulletInstance.
    • The Bullet Script Class is a NetworkBehaviour
    • Player has NetworkIdentity with LocalPlayerAuthority in true
    • Player Script Class is a NetworkBehaviour

enter image description here

1
Have you add networkidentiy in bulletPrefab ?? Second way you can use a method with [RpcClient] RpcSpawnABullet(vector) into [Command]CmdFire.Cổ Chí Tâm
how did you call cmdFireMuhammad Faizan Khan

1 Answers

3
votes

In Unet,

1. Only the SERVER can spawn any object - end of story

In Unet, servers spawn objects and that's it. End of story.

2. You HAVE TO HAVE "player" objects. Every client absolutely MUST have a "player" object.

It's totally normal that the "player" object is not actually a tank or biped - but is simply an empty game object. (Sometimes called an "abstract player object").

Every client MUST have a "player object" - often just an empty game object

3. Regarding spawning objects which have client authority - see point 1.

It's very easy to spawn objects with client authority. Just go to the doco page Manual/UNetSpawning.html and scroll down to Code (CSharp):

NetworkServer.SpawnWithClientAuthority(treeGo, conn);

It is explained there (really that's all there is to it).

4. Say some specific player, X, will have the authority over the object. Of course, you can't spawn the object until X has joined!

In the line of code just above, people often ask how the hell do you know which "conn" NetworkConnection to use?

Of course, it is meaningless until X has actually joined the game! See point 2!

So you might do things like this...

Example 1 - only one specific client is your "mothership". Of course, the server must, obviously, know the NetworkConnection to that client. You'll have it saved in a variable networkConnectionToMothership. Thus, in this example, you have the "conn" to use.

or

Example 2 - a client (say, client JohnSmith) wants to spawn an object which JohnSmith will have authority over. Quite simply, the client (JohnSmith) asks the server to spawn the object. "Asks the server" simply means send a Command.

Inside any "Command" (ie, running on the server) you literally have the property connectionToClient which gives you the "conn" needed!

So that's the deal.

For this specific question, in your code you are calling NetworkServer.spawn.

You should be calling

NetworkServer.SpawnWithClientAuthority(treeGo, conn);

(Search for that on this doco page: Manual/UNetSpawning.html )

and I have explained above in example (2) how you would know what "conn" to use.