首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Unity3d中通过UNET同步的问题

在Unity3d中通过UNET同步的问题
EN

Stack Overflow用户
提问于 2016-03-07 12:15:43
回答 1查看 1.5K关注 0票数 0

我在尝试通过网络同步球员的位置和轮换。我已经把事情部分地解决了。

我有两个球员,一个主机和远程。当查看主机的屏幕时,我看到了本地和网络播放器的正确位置。当在遥控器上时,我看到本地播放器的正确位置,但看不到联网播放器的正确位置。

这是我的同步脚本:

代码语言:javascript
运行
复制
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在任何对象上抛出这个脚本,它应该通过网络自动同步,并以本地播放器为源。

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

EN

回答 1

Stack Overflow用户

发布于 2016-03-20 23:03:32

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

代码语言:javascript
运行
复制
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;
    }
}
}

旋转:

代码语言:javascript
运行
复制
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class Player_SyncRotation : NetworkBehaviour {

private Transform myTransform;

// Object rotation vars
[SyncVar] private Quaternion syncObjectRotation;
public float rotationLerpRate = 15;

private Quaternion lastRot;
private float threshold = 5f;


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

private void FixedUpdate () {
    LerpRotation ();
    TransmitRotation (); 
}

private void LerpRotation() {

    if (!isLocalPlayer) {
        myTransform.rotation = Quaternion.Lerp (myTransform.rotation, syncObjectRotation, Time.deltaTime * rotationLerpRate);
    }

}

[Command]
private void Cmd_ProvideRotationToServer (Quaternion objectRotation) {
    syncObjectRotation = objectRotation;
}

[ClientCallback]
private void TransmitRotation() { // Send rotation to server
    if (isLocalPlayer && Quaternion.Angle(myTransform.rotation, lastRot) > threshold) {
        Cmd_ProvideRotationToServer (myTransform.rotation);
        lastRot = myTransform.rotation;
    }
}

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35836074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档