Unity中的网络播放器方向

时间:2017-01-25 18:55:35

标签: unity3d multiplayer unity-networking

我目前正在制作我的第一个多人游戏,并且在网络上改变/传输玩家方向时会遇到一些问题。

我当前的播放器控件类是:

using UnityEngine;
using System.Collections;

public class playerControls : MonoBehaviour {

    #region

    //Vars
    //Movements Vars
    public float runSpeed;
    Rigidbody mybody;
    Animator myAnimator;
    bool playerDirectionE;

    #endregion

    // Use this for initialization
    void Start () {
        mybody = GetComponent<Rigidbody>();
        myAnimator = GetComponent<Animator>();
        playerDirectionE = true;
        mybody.transform.eulerAngles = new Vector3(0, 90, 0);


    }

    // Update is called once per frame
    void Update () {

    }

    void FixedUpdate()
    {
        float move = Input.GetAxis("Horizontal");
        myAnimator.SetFloat("speed", Mathf.Abs(move)); //To intiate the charecter transition(move)

        mybody.velocity = new Vector3(move * runSpeed, mybody.velocity.y, 0); //move charecter along the x axis, and keep y on gravity, not touching the z axis

        if(move>0 && !playerDirectionE)
        {
            Flip();
        }
        else if(move<0 && playerDirectionE)
        {
            Flip();
        }
    }

    void Flip()
    {
        playerDirectionE = !playerDirectionE;
        Vector3 theScale = transform.localScale;
        theScale.z *= -1;
        transform.localScale = theScale;
    }
}

我使用以下客户端信息通过网络发送命令:

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

public class PlayerSyncRotation : NetworkBehaviour {

    [SyncVar(hook ="OnPlayerRotSynced")]
    private float syncPlayerRotation;

    [SerializeField]
    private Transform playerTransform;
    private float lerpRate = 15;

    private float lastPlayerRot;
    private float threshold = 1;

    private List<float> syncPlayerRotList = new List<float>();
    private float closeEneough = 0.3f;
    [SerializeField]
    private bool userHistoricalInterpolation;

    [Client]
    void OnPlayerRotSynced(float latestPlayerRotation)
    {
        syncPlayerRotation = latestPlayerRotation;
        syncPlayerRotList.Add(syncPlayerRotation);
    }

    [Command]
    void CmdProvideRotationsToServer(float playerRot)
    {
        syncPlayerRotation = playerRot;
    }

    [Client]
    void transmitRotations()
    {
        if (isLocalPlayer)
        {
            if(CheckIfBeyondThreshold(playerTransform.localScale.z, lastPlayerRot)){
                lastPlayerRot = playerTransform.localScale.z;
                CmdProvideRotationsToServer(lastPlayerRot);

            }
        }
    }

    bool CheckIfBeyondThreshold(float rot1, float rot2)
    {
        if (Mathf.Abs(rot1 - rot2) > threshold)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        lerpRotation();
    }

    void FixedUpdate()
    {
        transmitRotations();
    }

    void lerpRotation()
    {
        if (!isLocalPlayer)
        {
                HistoricalInterpolation();
        }
    }

    void HistoricalInterpolation(){
        if (syncPlayerRotList.Count > 0)
        {
            LerpPlayerRotation(syncPlayerRotList[0]);

            if(Mathf.Abs(playerTransform.localEulerAngles.z - syncPlayerRotList[0]) < closeEneough)
            {
                syncPlayerRotList.RemoveAt(0);
            }
            Debug.Log(syncPlayerRotList.Count.ToString() + "syncPlayerRotList Count");
        }
    }

    void LerpPlayerRotation(float rotAngle)
    {
        Vector3 playerNewRot = new Vector3(0, 0, rotAngle);
        playerTransform.rotation = Quaternion.Lerp(playerTransform.rotation, Quaternion.Euler(playerNewRot),lerpRate*Time.deltaTime);
    }
}

我的轮换在客户端看起来很好,但是在第二个客户端的网络上,轮换被打破并且看起来非常错误。

我附加了一个指向Webm的链接,您可以在其中看到我的问题HERE的简短视频片段。

这里有人会对我可能做错了什么或如何解决这个问题有任何意见吗?任何建议,将不胜感激。

1 个答案:

答案 0 :(得分:0)

相反,使用您的自定义代码,我想说你使用Unet NetworkTransform。它是Unet高级API,具有不同的自定义选项。

  

用于同步联网的位置和轮换的组件   对象(more)。

Unet HLAPI是开源的,因此您也可以在bitbucket获取networkTransform的编码。