在Unity3d中通过UNET同步问题

时间:2016-03-07 04:15:43

标签: unity3d unity3d-unet

我试图通过网络同步玩家的位置和轮换。我的事情已经部分运作了。

我有两个玩家主机和遥控器。查看主机屏幕时,我看到本地和网络播放器的正确位置。在遥控器上时,我看到了本地但不是联网播放器的正确位置。

这是我的同步脚本:

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

[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(NetworkIdentity))]
public class SyncRigidbody : NetworkBehaviour {

    public float positionLerpSpeed = 10f;
    public float positionThreshold = 0.0025f;
    public float rotationLerpSpeed = 10f;
    public float rotationThreshold = 2f;

    private Rigidbody _rigidbody;
    private Vector3 _requestedPos;
    private Vector3 _lastPos;
    private Quaternion _requstedRot;
    private Quaternion _lastRot;

    void Start () {
        _rigidbody = gameObject.GetComponent<Rigidbody> ();
        if (!isLocalPlayer) {
            _rigidbody.isKinematic = true;
        }
    }

    void Update () {
        TransmitPosition ();
        TransmitRotation ();
        LerpPosition ();
        LerpQuaternion ();
    }

    void LerpPosition () {
        if (!isLocalPlayer) {
            _rigidbody.MovePosition (_requestedPos);
        }
    }

    void LerpQuaternion () {
        if (!isLocalPlayer && _requstedRot.w != 0) {
            _rigidbody.MoveRotation (_requstedRot);
        }
    }

    [Command]
    void CmdUpdateTransformPosition (Vector3 pos) {
        Debug.Log ("CmdUpdateTransformPosition: For " + gameObject.name + " to " + pos);
        _requestedPos = pos;
    }

    [Command]
    void CmdUpdateTransformRotation (Quaternion rot) {
        Debug.Log ("CmdUpdateTransformRotation: For " + gameObject.name + " to " + rot);
        _requstedRot = rot;
    }

    [Client]
    void TransmitPosition () {
        if (isLocalPlayer && Vector3.Distance (_rigidbody.position, _lastPos) > positionThreshold) {
            Debug.Log ("TransmitPosition: For " + gameObject.name + " to " + _rigidbody.position);
            CmdUpdateTransformPosition (_rigidbody.position);
            _lastPos = _rigidbody.position;
        }
    }

    [Client]
    void TransmitRotation () {
        if (isLocalPlayer && Quaternion.Angle (_rigidbody.rotation, _lastRot) > rotationThreshold) {
            Debug.Log ("TransmitRotation: For " + gameObject.name + " to " + _rigidbody.rotation);
            CmdUpdateTransformRotation (_rigidbody.rotation);
            _lastRot = _rigidbody.rotation;
        }
    }
}

我的想法是,我应该能够在Rigidbody的任何对象上抛出此脚本,它应该通过网络自动同步,本地播放器是源。

为什么它在远程连接上我没有在主机上看到同步对象我看到它们都正确了?

1 个答案:

答案 0 :(得分:0)

我不知道这是否会对你有所帮助,但这里有两个我正在使用的位置和旋转同步脚本对我有用。也许不是最优化的但它们有效:

rm -rf node_modules bower_components dist tmp
npm cache clean
bower cache clean
npm install
bower install

旋转:

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

public class Player_SyncPosition : NetworkBehaviour {

private Transform myTransform;
[SerializeField] float lerpRate = 15;
[SyncVar] private Vector3 syncPos;

private Vector3 lastPos;
private float threshold = 0.5f;

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


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

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

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

[ClientCallback]
void TransmitPosition () {
    if (isLocalPlayer && Vector3.Distance(myTransform.position, lastPos) > threshold) {
        Cmd_ProvidePositionToServer (myTransform.position);
        lastPos = myTransform.position;
    }
}
}