簡體   English   中英

網絡播放器錯誤 UNITY

[英]Networking player bug UNITY

您好,當我啟動 google 服務 RealTime 時,我遇到了一個生成問題,兩個玩家都在滯后,一個玩家被竊聽到地面對撞機。 這是動畫:

動畫片

這是我從客戶端發送的腳本:

void Update()
{

    if (player == PlayerOne) {       
    string PlayerOneData = "PlayerOne" + ":" + player.transform.position.x + ":" + player.transform.position.y;
    byte[] PlayerOneDataArr = System.Text.ASCIIEncoding.Default.GetBytes(PlayerOneData);
    PlayGamesPlatform.Instance.RealTime.SendMessageToAll(false, PlayerOneDataArr);
    // Camera.main.GetComponent<CameraFollow>().setTarget(PlayerOne.transform);
    textik.text = "PlayerOne upload " + PlayerOneData;
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
        {
            Vector2 touchPosition = Input.GetTouch(0).position;
            double halfScreen = Screen.width / 2.0;
            if (touchPosition.x < halfScreen)
            {
                player.transform.Translate(Vector3.left * 10 * Time.deltaTime);
            }
            else if (touchPosition.x > halfScreen)
            {
                player.transform.Translate(Vector3.right * 10 * Time.deltaTime);
            }

        }
    }
    else if (player == PlayerTwo)
    {
        string PlayerTwoData = "PlayerTwo" + ":" + player.transform.position.x + ":" + player.transform.position.y;
        byte[] PlayerTwoDataArr = System.Text.ASCIIEncoding.Default.GetBytes(PlayerTwoData);
        PlayGamesPlatform.Instance.RealTime.SendMessageToAll(false, PlayerTwoDataArr);
        //Camera.main.GetComponent<CameraFollow>().setTarget(PlayerTwo.transform);
        textik.text = "PlayerTwo upload" + PlayerTwoData;
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
        {
            Vector2 touchPosition = Input.GetTouch(0).position;
            double halfScreen = Screen.width / 2.0;

            //Check if it is left or right?
            if (touchPosition.x < halfScreen)
            {
                player.transform.Translate(Vector3.left * 10 * Time.deltaTime);
            }
            else if (touchPosition.x > halfScreen)
            {
                player.transform.Translate(Vector3.right * 10 * Time.deltaTime);
            }

        }
    }
}

這是我的接收功能:

public void OnRealTimeMessageReceived(bool IsReliable, string SenderId, byte[] data)
{

    string position = System.Text.Encoding.Default.GetString(data);
    string[] raw = position.Split(new string[] { ":" }, System.StringSplitOptions.RemoveEmptyEntries);
    if (!IsReliable)
    {
        if (raw[0] == "PlayerOne")
        {
            PlayerOne.transform.position = new Vector2(System.Convert.ToSingle(raw[1]), System.Convert.ToSingle(raw[2]));
            textik2.text = "PlayerOne PRIJAL " + position;
        }
        if (raw[0] == "PlayerTwo")
        {
            PlayerTwo.transform.position=new Vector2(System.Convert.ToSingle(raw[1]), System.Convert.ToSingle(raw[2]));
            textik2.text = "PlayerTWO prijal " + position;
        }
    }
}

我的預制件被設置為:

預制屏風

感謝您的任何想法或任何幫助

你多久發送一次數據? 確保您發送的頻率不高。 確保在傳輸的位置之間進行插值。

我可以推薦 Unitys 基本網絡示例:

https://unity3d.com/de/learn/tutorials/topics/multiplayer-networking/network-manager?playlist=29690

您可以在 10 分鍾內使其工作。 不需要服務器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM