1

我正在尝试了解 Unet,但我无法掌握它,尽管遵循了一些教程。

我正在制作一个 2d 游戏,而我目前所困的是更新我的角色精灵转动的方式(向左或向右)。

此处显示的问题

我的播放器预制件有: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;

    }
}

我知道这很可能是我做错了超级简单的事情,这只会增加询问这个问题的痛苦,但非常感谢任何帮助!谢谢

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

4

1 回答 1

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));
        }
    }

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

于 2017-02-19T19:25:52.773 回答