我正在研究一款小型多人2D平台游戏。我已经设法在网络上复制动画,移动和翻转精灵,但是我有一个错误,我无法理解。翻转精灵的代码非常适合主持人-他可以看到其他玩家在向左或向右转弯时切换方向,但是其他人看不到主持人的翻转。第三位玩家也看不到第二位玩家的回合,等等。有些事情不对劲,如果有人可以看看我的代码,那就太好了。

主播放器脚本中的代码:

if (input.x > 0 && !facingRight)
     {
         FlipSprite();
         GetComponent<Player_SyncPosition>().FlipSprite();
     }
     else if (input.x < 0 && facingRight)
     {
         FlipSprite();
         GetComponent<Player_SyncPosition>().FlipSprite();
     }


播放器上其他Player_SyncPosition脚本中的代码:

 [ClientCallback]
 public void FlipSprite()
 {
     if (isLocalPlayer)
     {
         CmdFlip();
     }
 }
 [Command]
 void CmdFlip()
 {
     if (!isLocalPlayer)
     {
         print("Switching sides");
         facingRight = !facingRight;
         Vector3 SpriteScale = GetComponent<Transform>().localScale;
         SpriteScale.x *= -1;
         GetComponent<Transform>().localScale = SpriteScale;
     }
 }


编辑:

播放器

Player_SyncPosition pSync;
void Awake()
{
    pSync = GetComponent<Player_SyncPosition>();
}

void Update ()
{
if ((input.x > 0 && !pSync.facingRight) || (input.x < 0 && pSync.facingRight))
    {
        pSync.FlipSprite();
    }
}


Player_SyncPosition

[SyncVar(hook = "FaceDirCallback")]
public bool facingRight = true;

[ClientCallback]
public void FlipSprite()
{
    if (isLocalPlayer)
    {
        CmdFlip();
    }
}
[Command]
void CmdFlip()
{
    print("Switching sides");
    facingRight = !facingRight;
    Vector3 SpriteScale = transform.localScale;
    SpriteScale.x *= -1;
    transform.localScale = SpriteScale;
}

void FaceDirCallback(bool newValue)
{
    print(facingRight);
    print(newValue);
    facingRight = newValue;
}

最佳答案

我遇到了各种各样的不同问题(向后移动等),并且这种方法有效:

玩家:

void Awake()
     {
         syncPos = GetComponent<Player_SyncPosition>();
     }

void Update()
 {
  if ((input.x > 0 && !facingRight) || (input.x < 0 && facingRight))
         {
             facingRight = !facingRight;
             syncPos.CmdFlipSprite(facingRight);
         }
 }


Player_SyncPosition:

 [SyncVar(hook = "FacingCallback")]
     public bool netFacingRight = true;

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

关于c# - Unity网络:在多人游戏中翻转 Sprite 不会翻转主机,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34355894/

10-12 12:42
查看更多