[英]Unity: Spawn object from client to all clients
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使用Unity 5.6
What I have so far...到目前为止我所拥有的......
In Unet,在乌内特,
In Unet, servers spawn objects and that's it.在 Unet 中,服务器生成对象,仅此而已。 End of story.
故事结束。
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每个客户端都必须有一个“玩家对象”——通常只是一个空的游戏对象
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):
只需转到 doco 页面 Manual/UNetSpawning.html 并向下滚动到 Code (CSharp):
NetworkServer.SpawnWithClientAuthority(treeGo, conn);
It is explained there (really that's all there is to it).那里有解释(真的就是这样)。
In the line of code just above, people often ask how the hell do you know which "conn" NetworkConnection
to use ?在上面的代码行中,人们经常问你到底是怎么知道使用哪个“conn”
NetworkConnection
?
Of course, it is meaningless until X has actually joined the game!当然,直到X真正加入游戏才有意义! See point 2!
见第2点!
So you might do things like this...所以你可能会做这样的事情......
Example 1 - only one specific client is your "mothership".
示例 1 - 只有一个特定的客户是您的“母舰”。 Of course, the server must, obviously, know the
NetworkConnection
to that client.当然,很明显,服务器必须知道该客户端的
NetworkConnection
。 You'll have it saved in a variablenetworkConnectionToMothership
.您将把它保存在一个变量
networkConnectionToMothership
。 Thus, in this example, you have the "conn" to use.因此,在本例中,您可以使用“conn”。
or或
Example 2 - a client (say, client JohnSmith) wants to spawn an object which JohnSmith will have authority over.
示例 2 - 客户端(例如,客户端 JohnSmith)想要生成一个对象,JohnSmith 将拥有该对象的权限。 Quite simply, the client (JohnSmith) asks the server to spawn the object.
很简单,客户端(JohnSmith)要求服务器生成对象。 "Asks the server" simply means send a
Command
.“询问服务器”只是意味着发送一个
Command
。
Inside any "Command" (ie, running on the server) you literally have the property connectionToClient
which gives you the "conn" needed!在任何“命令”(即在服务器上运行)中,您确实拥有属性
connectionToClient
,它为您提供所需的“conn”!
So that's the deal.所以这就是交易。
For this specific question, in your code you are calling NetworkServer.spawn
.对于这个特定问题,在您的代码中,您正在调用
NetworkServer.spawn
。
You should be calling你应该打电话
NetworkServer.SpawnWithClientAuthority(treeGo, conn);
(Search for that on this doco page: Manual/UNetSpawning.html ) (在此 doco 页面上搜索: Manual/UNetSpawning.html )
and I have explained above in example (2) how you would know what "conn" to use.我在上面的示例 (2) 中已经解释了如何知道要使用什么“conn”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.