Networking Player Movement
网络玩家运动
To make the player movement network aware and to make sure that only the local player can control the local player GameObject, we need to update the PlayerController script.
为了使玩家运动网络意识到并且确保只有本地玩家可以控制本地玩家的游戏对象,我们需要更新PlayerController脚本。
The two biggest changes to the PlayerController script will be to use the namespace UnityEngine.
PlayerController脚本的两个最大变化是使用名称空间UnityEngine。
Networking, and to have the PlayerController script derive from NetworkBehaviour rather than MonoBehaviour.
建立网络,并让PlayerController脚本从网络行为而不是单行为中派生。
Open the PlayerController script for editing.
打开PlayerController脚本进行编辑。
Add the namespace UnityEngine.Networking.
UnityEngine.Networking添加名称空间。
using UnityEngine.Networking;
使用UnityEngine.Networking;
Change MonoBehaviour to NetworkBehaviour.
改变MonoBehaviour NetworkBehaviour。
public class PlayerController : NetworkBehaviour
Add a check for isLocalPlayer in the Update function, so that only the local player processes input.
在更新函数中为isLocalPlayer添加一个检查,这样只需要本地播放器处理输入。
if (!isLocalPlayer)
{
return;
}
The final script should look like this:
最后的脚本应该是这样的:
PlayerController
C#
void Update()
{
if (!isLocalPlayer)
{
return;
}
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;
transform.Rotate(0, x, 0);
transform.Translate(0, 0, z);
}
}
The namespace UnityEngine.
名称空间UnityEngine。
Networking holds the networking code we will need to write network-aware scripts.
网络中包含了我们需要编写网络感知脚本的网络代码。
For more information on namespaces, please see the page on namespaces in the Manual
有关名称空间的更多信息,请参见手册中的名称空间页面。
The class NetworkBehaviour is a specialized class based on Monobehaviour.
类网络行为是一种基于单行为的专门化类。
Scripts attached to GameObjects that need to use networking features should inherit from NetworkBehaviour.
附加到需要使用网络特性的游戏对象的脚本应该从网络行为继承。
For more information please see the pages on NetworkBehaviour in either the Scripting Reference or the Manual.
有关更多信息,请参阅脚本引用或手册中关于网络行为的页面。
Note the check using !isLocalPlayer.
注意检查使用!isLocalPlayer。
LocalPlayer is part of NetworkBehaviour and all scripts that derive from NetworkBehaviour will understand the concept of a LocalPlayer.
LocalPlayer是网络行为的一部分,所有源自网络行为的脚本都将理解LocalPlayer的概念。
What a LocalPlayer is and how it works requires an understanding the architecture of a project using the Multiplayer Networking High Level API (or HLAPI).
LocalPlayer是什么,它是如何工作的,需要了解一个项目的架构,使用多人网络高级API(或HLAPI)。
For more information on the HLAPI please see the page on the HLAPI.
想要了解更多关于HLAPI的信息,请参阅HLAPI上的页面。
In a networked project, the Server and all of the Clients are executing the same code from the same scripts on the same GameObjects at the same time.
在一个网络项目中,服务器和所有客户端同时在相同的游戏对象上执行相同的代码。
This means that in a situation with a Server and two Clients, there will be six potential player GameObjects being processed.
这意味着在一个服务器和两个客户机的情况下,将会处理6个潜在的玩家游戏对象。
This is because there will be two players and therefore two player GameObjects on the Server.
这是因为在服务器上有两个玩家和两个玩家的游戏对象。
There will also be two players on each of the two Clients.
在这两个客户中也会有两个玩家。
This makes a total of six player GameObjects.
这使得总共有6个玩家的游戏对象。
All of these player GameObjects were spawned from the same player prefab asset and all share a copy of the same PlayerController script.
所有这些玩家的游戏对象都是从同一个玩家的预制资产中派生出来的,并且所有的玩家都共享同一个PlayerController脚本的副本。
With a script that derives from NetworkBehaviour, which player “owns” what GameObject is understood automatically and is managed as part of the spawning process.
有了一个源自网络行为的脚本,玩家“拥有”了什么游戏对象被自动理解,并作为生成过程的一部分进行管理。
The LocalPlayer is the player GameObject “owned” by the local Client.
LocalPlayer是本地客户端“拥有”的玩家游戏对象。
This ownership is set by the NetworkManager when a Client connects to the Server and new player GameObjects are created from the player prefab.
当客户端连接到服务器时,这个所有权由NetworkManager设置,新玩家的游戏对象是由玩家预置的。
When a Client connects to the Server, the instance created on the local client is marked as the LocalPlayer.
当客户端连接到服务器时,在本地客户端上创建的实例被标记为LocalPlayer。
All of the other player GameObjects that represent this player - whether this is on the Server or another Client - are not marked as a LocalPlayer.
表示该播放器的所有其他玩家的游戏对象——无论是在服务器上还是在其他客户端——都没有被标记为LocalPlayer。
By using the boolean check isLocalPlayer, a script can acknowledge or ignore code depending upon whether or not it is owned by the LocalPlayer.
通过使用布尔检查isLocalPlayer,脚本可以确认或忽略代码,这取决于它是否由LocalPlayer拥有。
In our case, in Update, we have a check that says:
在我们的案例中,在更新中,我们有一张支票,上面写着:
if (!isLocalPlayer)
{
return;
}
With this check, only the LocalPlayer can execute the movement code.
通过这个检查,只有LocalPlayer可以执行移动代码。
In the situation with a Server and two Clients, like our example project, the player GameObject on the Server and the remote Client will not be a LocalPlayer.
在服务器和两个客户机的情况下,例如我们的示例项目,服务器上的player GameObject和远程客户端将不会是LocalPlayer。
In this case, the code path will return and not execute the movement code.
在这种情况下,代码路径将返回而不是执行移动代码。
This allows identical scripts on all of the GameObjects that work correctly regardless of whether they are being processed on the Server or any of the game Clients.
这允许在所有的游戏对象上都有相同的脚本,不管它们是在服务器上处理还是在任何游戏客户端处理。
If we were to run the example project at this point, the player GameObjects would not stay in sync across all of the instances of the running project.
如果我们在这一点上运行示例项目,那么player GameObjects将不会在运行项目的所有实例中保持同步。
Movement Input would only be processed on the local player GameObject and the position and rotation of that player GameObject would not be updated across the network.
移动输入只会在本地玩家的游戏对象上进行处理,并且玩家的游戏对象的位置和旋转不会在整个网络中被更新。
To keep the player GameObjects in sync, we need to add a NetworkTransform component to the player prefab.
为了使玩家的游戏对象保持同步,我们需要向玩家预置添加一个NetworkTransform组件。
Save the script.
保存脚本。
Return to Unity.
回到Unity。
Select the player prefab asset in the Project Window.
在项目窗口中选择播放器预置资产。
With the player prefab asset selected,
选择了玩家预置的资产,
...
…
find and add the component: Network > NetworkTransform.
查找并添加组件:网络>网络转换。
Save the Project.
保存项目。
This NetworkTransform synchronizes the GameObject’s transform across the network.
这个NetworkTransform同步了整个网络中的GameObject的转换。
The local player moves the locally owned player GameObject and only the locally owned player GameObject because of the isLocalPlayer check.
本地玩家移动本地拥有的玩家游戏对象,只有本地玩家的游戏对象,因为isLocalPlayer检查。
The player GameObject's NetworkTransform then synchronizes the position, rotation and scale of the GameObject's transform across the Server and all of the Clients.
玩家GameObject的NetworkTransform然后在服务器和所有客户端同步游戏对象转换的位置、旋转和缩放比例。
For more information on the NetworkTransform component, please see the page on the NetworkTransform.
有关NetworkTransform组件的更多信息,请参阅NetworkTransform的页面。