简体   繁体   English

在Unity Networking中切换播放器对象

[英]Switching between player objects in Unity Networking

I would like to know how I can switch back and forth between controllable objects in Unity's networking. 我想知道如何在Unity网络中的可控对象之间来回切换。

For example, say there are two objects, Player1 and Player2, and you're controlling Player1. 例如,假设有两个对象,Player1和Player2,并且您正在控制Player1。 By pressing the spacebar, you're now controlling Player2. 按空格键,您现在正在控制Player2。 Press again, goes back to controlling Player1. 再次按,返回控制Player1。

What I have done: I followed the Unity Manual up to "Test Multiplayer Movement", and by referring to "Switching Players" in Player Objects 我做了什么:我遵循Unity手册直到“测试多人游戏运动”,并参考玩家对象中的 “切换玩家”
I changed the Player's script to as follows: 我将播放器的脚本更改为如下:

using UnityEngine;
using UnityEngine.Networking;

public class PlayerMove : NetworkBehaviour {

    string player1;
    string player2;

    void Start(){
        player1 = "Player1(Clone)";
        player2 = "Player2";
    }

    // Update is called once per frame
    void Update () {
        if(!isLocalPlayer){
            return;
        }


        float x = Input.GetAxis("Horizontal")*0.1f;
        float z = Input.GetAxis("Vertical")*0.1f;

        transform.Translate(x, 0, z);

        if(Input.GetKeyDown(KeyCode.Space)){
            Debug.Log("switched");
            PlayerChange(this.gameObject);
        }
    }

  public void PlayerChange(GameObject oldPlayer)
  {
      var conn = oldPlayer.GetComponent<NetworkIdentity>().connectionToClient;

      GameObject newPlayer;
      if(this.gameObject.name == player1){
          newPlayer = GameObject.Find(player2);
          NetworkServer.ReplacePlayerForConnection(conn, newPlayer, 0);
      }
      else{
          newPlayer = GameObject.Find(player1); 
          NetworkServer.ReplacePlayerForConnection(conn, newPlayer, 0);
      }  
  }

}

Also, Player2 object is created by dragging Player1 (Prefab) to Hierarchy. 此外,通过将Player1(Prefab)拖动到层次结构来创建Player2对象。

Error: I tried playing it, and while switching to Player2 from Player1 works, switching back to Player1 from Player2 does not. 错误:我尝试过播放它,当从Player1切换到Player2时,从Player2切换回Player1则不然。 The error message goes: 错误消息:

SetClientOwner m_ClientAuthorityOwner already set!
UnityEngine.Networking.NetworkServer:ReplacePlayerForConnection(NetworkConnection, GameObject, Int16)
PlayerMove:PlayerChange(GameObject) (at Assets/PlayerMove.cs:43)
PlayerMove:Update() (at Assets/PlayerMove.cs:28)

For using a network object you should need to assign the authority first in order to use it. 要使用网络对象,您需要先分配权限才能使用它。

Suppose if you are at player1 and want to switch to player2 then, 假如你在player1并且想要切换到player2那么,

Assign authority to player2 and remove authority from player1(and vice-versa for another switch). 将权限分配给player2并从player1中删除权限(反之亦然,用于另一个交换机)。

For assigning authority you should use this AssignClientAuthority . 要分配权限,您应该使用此AssignClientAuthority For practicle implemntation of the code i'll recommend you this unity forum discussion (worth to understand). 为了实现代码的实现,我会向你推荐这个统一论坛的讨论 (值得理解)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM