我正在测试UNET(Unity 5.2),但遇到的问题可能非常简单。
我有一个橙色水果(GameObject),我可以拖动并在代码中附加网络变换。当释放鼠标(Mouse Up)时,我想释放Orange的所有权,所以没有人拥有它,后来想要附加到另一个玩家。我已经使用ReplacePlayerForConnection
和其他一些测试进行了测试,但完全搞砸了代码。
我现在已经重置了所有内容,并且必须要求提供一些帮助。
我附加到Orange GameObject的脚本是:
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;
}
}
这是附加到Orange GameObject的脚本#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);
}
}
}
答案 0 :(得分: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);
}
答案 1 :(得分:0)
使用AssignClientAuthority
/ RemoveClientAuthority
永远不要在运行时触摸这些参数:
GetComponent<NetworkIdentity> ().localPlayerAuthority = false;
GetComponent<NetworkIdentity> ().serverOnly = false;