Unity2D,无法在所有客户端(多人游戏)中同步我的精灵

时间:2017-02-19 11:54:07

标签: c# unity3d multiplayer unity3d-2dtools unity3d-unet

我正在尝试了解Unet,但是我很难掌握它,尽管有一些教程。

我正在制作一款2D游戏,而我目前所处的位置正在更新我的角色精灵转向(左或右)的方式。

Problem shown here

我的播放器预制件包括:Sprite渲染器,rigidbody2D,网络标识(设置为本地播放器权限)和网络转换。

这是附加到每个玩家的代码:

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

public class CharController : NetworkBehaviour {

    public float maxSpeed = 1f;
    [SyncVar]
    bool facingRight = true;
    Animator anim;


    // Use this for initialization
    void Start () {
        anim = GetComponent<Animator>();
    }

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

    }
    void Moving()
    {


            float move = Input.GetAxis("Horizontal");

            GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);

            if(move > 0 && !facingRight)
            {
                RpcFlip();
            }
            else if(move < 0 && facingRight)
            {
                RpcFlip();
            }
            anim.SetFloat("MovingSpeed", Mathf.Abs(move));
    }

    private void FixedUpdate()
    {
        if (!isLocalPlayer)
        {
            return;
        }
        // handle input here...
        Moving();







    }
    [ClientRpc]
    void RpcFlip()
    {
        if (isLocalPlayer)
        {
            //facingRight = !facingRight;
            //currentSprite.flipX = !currentSprite.flipX;


        }

        facingRight = !facingRight;
            Vector3 theScale = transform.localScale;
            theScale.x *= -1;
            transform.localScale = theScale;

    }
}

我知道这很可能是一件非常简单的事我做错了,这只会增加更多的痛苦来问这个,但任何帮助都非常感谢!感谢

编辑:我的游戏设置方式,其中一个玩家既是主机又是客户端,我不知道这是否会让它变得更难,但这就是团结在本地玩的方式。< / p>

1 个答案:

答案 0 :(得分:0)

我在阅读谷歌的前5页后想出来了,这是我的最终代码,以防其他有同样问题的人偶然发现:

public float maxSpeed = 1f;
    Animator anim;

    [SyncVar(hook = "FacingCallback")]  //Everytime the bool netFacingRight is called, the method FacingCallback is called aswell. 
                                        //Since the bool only changes when CmdFlipSprite is called, it makes sure that CmdFlipSprite calls
                                        //the change on our server, and FacingCallback calls it locally.
    public bool netFacingRight = true;

    // Use this for initialization
    void Start () {
        anim = GetComponent<Animator>();
    }


    [Command]
    public void CmdFlipSprite(bool facing)
    {
        netFacingRight = facing;
        if (netFacingRight)
        {
            Vector3 SpriteScale = transform.localScale;
            SpriteScale.x = 1;
            transform.localScale = SpriteScale;
        }
        else
        {
            Vector3 SpriteScale = transform.localScale;
            SpriteScale.x = -1;
            transform.localScale = SpriteScale;
        }
    }

    void FacingCallback(bool facing)
    {
        netFacingRight = facing;
        if (netFacingRight)
        {
            Vector3 SpriteScale = transform.localScale;
            SpriteScale.x = 1;
            transform.localScale = SpriteScale;
        }
        else
        {
            Vector3 SpriteScale = transform.localScale;
            SpriteScale.x = -1;
            transform.localScale = SpriteScale;
        }
    }

    ////////




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

        if (!isLocalPlayer)
        {
            return;
        }
        float move = Input.GetAxis("Horizontal");

        GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);

        if ((move > 0 && !netFacingRight) || (move < 0 && netFacingRight))
        {
            netFacingRight = !netFacingRight;
            CmdFlipSprite(netFacingRight);
            anim.SetFloat("MovingSpeed", Mathf.Abs(move));
        }
    }

您通常必须在本地和服务器上调用该函数!