【发布时间】:2021-11-12 00:21:44
【问题描述】:
对不起,如果这是一个重复的问题。
我发现这个 Reddit 帖子正是我当前的问题。 https://www.reddit.com/r/Unity2D/comments/lw96s5/multiplayer_cameras/
但是,只有一条评论不是描述性的,并且没有任何指向“教程”的链接。换句话说,我在 Unity 中有一个 MLAPI 游戏,带有 LocalPlayer 预制件(有摄像头、FPS 移动代码和 PlayerInput)和 ServerPlayer(没有摄像头或 PlayerInput 组件)预制件。它们代表我游戏中不同的复制玩家。
当前的问题是决定是生成本地玩家还是远程玩家的代码。
决定生成什么预制件:
private void ApprovalCheck(byte[] connectionData, ulong clientId, MLAPI.NetworkManager.ConnectionApprovedDelegate callback)
{
//Your logic here
bool approve = connectionData == System.Text.Encoding.ASCII.GetBytes("xd");
bool createPlayerObject = true;
if (NetworkManager.Singleton.ConnectedClients[clientId].PlayerObject.IsLocalPlayer)
{
localPlayerPrefab.GetComponent<NetworkObject>()
.SpawnAsPlayerObject(clientId);
}
else
{
serverPlayerPrefab.GetComponent<NetworkObject>()
.SpawnAsPlayerObject(clientId);
}
//If approve is true, the connection gets added. If it's false. The client gets
disconnected
callback(createPlayerObject, null, approve, positionToSpawnAt, Quaternion.identity);
}
如果您需要任何额外的详细信息,请告诉我。
【问题讨论】:
-
使用相同的预制件,而不是让预制件本身检查它是否属于本地播放器。如果是,请保持相机处于活动状态,否则停用/销毁它...
-
@derHugo 这不起作用。
playerCamera.transform.parent.gameObject.GetComponent<NetworkObject>().IsLocalPlayer -
我应该使用
NetworkBehaviour的IsLocalPlayer吗?
标签: c# unity3d p2p multiplayer unity3d-unet