2

我正在测试 UNET (Unity 5.2),但遇到了一些可能应该非常简单的问题。

我有一个橙色水果(GameObject),我可以拖动它并在代码中附加了网络转换。释放鼠标(Mouse Up)时,我想释放 Orange 的所有权,所以没有人拥有它,以后想附加到另一个玩家。我已经测试ReplacePlayerForConnection了其他一些东西,但完全搞砸了代码。

我现在已经重置了所有东西,并且必须寻求一些帮助如何做到这一点。

我拥有的附加到 Orange GameObject 的脚本是:

1

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;

public class orange : NetworkBehaviour {

float distance = 10;


void Start() {

    if (isLocalPlayer) {
    
        //GameObject.Find("Main Camera").SetActive(false);
    
    }

}

void OnMouseDrag() {

    Vector3 mousePosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, distance);
    Vector3 objPosition = Camera.main.ScreenToWorldPoint (mousePosition);
    
    transform.position = objPosition;

}

void OnMouseUp() {
    print (">>> MOUSE UP <<<");

    if (isLocalPlayer) {
        GetComponent<NetworkIdentity> ().localPlayerAuthority = false;
        GetComponent<NetworkIdentity> ().serverOnly = false;
    }

}

这是附加到橙色游戏对象的脚本 #2:

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;

public class Orange_SyncPosition : NetworkBehaviour {

[SyncVar] // Server will automatically transmit this value to all players when it changes
private Vector3 syncPos;

[SerializeField] Transform myTransform;
[SerializeField] float lerpRate = 15;

void FixedUpdate () {
    TransmitPosition ();
    LerpPosition ();
}

void LerpPosition() {

    if (!isLocalPlayer) {
        myTransform.position = Vector3.Lerp(myTransform.position, syncPos, Time.deltaTime *lerpRate);
    }

}

[Command]
void CmdProvidePositionToServer (Vector3 pos) {
    syncPos = pos;
}

[ClientCallback]
void TransmitPosition () {

    if (isLocalPlayer) {
        CmdProvidePositionToServer (myTransform.position);
    }

}
}
4

2 回答 2

0

使用AssignClientAuthority/RemoveClientAuthority

并且永远不要在运行时接触这些参数:

GetComponent<NetworkIdentity> ().localPlayerAuthority = false;
GetComponent<NetworkIdentity> ().serverOnly = false;
于 2018-05-29T07:12:09.650 回答
0

使用NetworkServer.ReplacePlayerForConnection()

// Player is a NetworkBehaviour on the existing player object
void ReplacePlayer (Player existingPlayer) {

    var conn = existingPlayer.connectionToClient;
    var newPlayer = Instantiate<GameObject>(playerPrefab);

    Destroy(existingPlayer.gameObject);
    NetworkServer.ReplacePlayerForConnection(conn, newPlayer, 0);

}
于 2015-10-31T19:41:16.687 回答