我正在测试 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);
}
}
}