Unity Networking:在多人游戏中翻转精灵不会翻转主机

Unity Networking: Flipping sprites in Multiplayer doesn't flip host

我正在开发一款小型多人 2d 平台游戏。我已经设法让它在网络上复制动画、移动和翻转精灵,但我有一个我无法理解的错误。翻转精灵的代码非常适合主持人——他可以看到其他玩家在左转或右转时换边,但其他人看不到主持人翻转。第三个玩家也看不到第二个玩家的转弯等。有些地方不对,如果有人可以看一下我的代码,那就太好了。

主要Player脚本中的代码:

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

上面的代码很有魅力,但我认为它可以更简洁

[SyncVar(hook = "FacingCallback")]
public bool m_FacingRight = true;
[Command]
public void CmdFlipSprite(bool facing)
{
    m_FacingRight = !m_FacingRight;
}

void FacingCallback(bool _Old, bool _New)
{
    Vector3 SpriteScale = transform.localScale;
    SpriteScale.x = m_FacingRight ? 1 : -1;
    transform.localScale = SpriteScale;
}