0

这与http://forum.unity3d.com/threads/re...ms-to-be-broken-in-5-2-1.359149/#post-2785856中的问题相同

我的问题如下:

1) 为对象分配客户端权限:AssignClientAuthority

2)移动对象,有效

3)应用 RemoveClientAuthority 并且对象在客户端恢复到原始位置。“TEST”调用只是为了查看它是否适用于最后一个对象。

这是一个错误还是我做错了什么?

这是我做的一个测试的代码示例:

    foreach (string tagName in MP_Singleton.Instance.master_Object_List) {

        temp_GameObject = GameObject.FindWithTag(tagName);

        Cmd_LocalAuthority (true, temp_GameObject);

        temp_GameObject.GetComponent<Renderer>().sortingOrder = z;

        randomX = UnityEngine.Random.Range (-0.055f, 0.055f);
        randomY = UnityEngine.Random.Range (-0.055f, 0.055f);
        randomX = randomX + deckStartPosX;
        randomY = randomY + deckStartPosY;

        Rpc_Position (temp_GameObject, randomX, randomY, z, twistAngle);

        // Add to depth
        z++;

    }

    Cmd_LocalAuthority (false, temp_GameObject); //<< TEST

RPC:

[ClientRpc]
void Rpc_Position(GameObject myGO, float ranX, float ranY, int zDepth, float twist) {

    myGO.transform.position = new Vector3 (ranX, ranY, zDepth);
    myGO.transform.localEulerAngles = new Vector3(0f, 0f, twist);

}

Cmd_LocalAuthority:

[Command]
void Cmd_LocalAuthority(bool getAuthority, GameObject obj) {

    objNetId = obj.GetComponent<NetworkIdentity> ();        // get the object's network ID

    if (getAuthority) {
        objNetId.AssignClientAuthority (connectionToClient);    // assign authority to the player
    } else {
        objNetId.RemoveClientAuthority (connectionToClient);    // remove the authority from the player
    }
}
4

1 回答 1

0

Unity 的优秀人员帮助我解决了这个问题,这是我自己的错误。

我花了至少 2 - 3 个月的时间试图修复它们,它们立即修复。

我错过的是我在导致问题的 pos-sync-script 中检查了权限。它位于“Cmd_ProvidePositionToServer”中。

这是代码:

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

public class Object_SyncPosition : NetworkBehaviour {

private Transform myTransform;
[SerializeField] float lerpRate = 5;
[SyncVar] private Vector3 syncPos;
private NetworkIdentity theNetID;

private Vector3 lastPos;
private float threshold = 0.5f;


void Start () {
    myTransform = GetComponent<Transform> ();
    syncPos = GetComponent<Transform>().position;
}


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

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

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

[ClientCallback]
void TransmitPosition () {
    if (hasAuthority  && Vector3.Distance(myTransform.position, lastPos) > threshold) {
        Cmd_ProvidePositionToServer (myTransform.position);
        lastPos = myTransform.position;
    }
}
}
于 2016-09-17T11:51:03.490 回答