'Unity Networking: Flipping sprites in Multiplayer doesn't flip host
I'm working on a small multiplayer 2d platformer game. I've managed to get it to replicate animations, movement and flipping sprites across network, but I have a bug I can't get my head around. The code for flipping sprites works perfectly for the host - he can see other players switch sides when turning left or right, but everyone else cannot see the host flip. Also the third player cannot see the second player turn etc. Something is not right and it would be great if someone could take a look at my code.
Code in main Player script:
if (input.x > 0 && !facingRight)
{
FlipSprite();
GetComponent<Player_SyncPosition>().FlipSprite();
}
else if (input.x < 0 && facingRight)
{
FlipSprite();
GetComponent<Player_SyncPosition>().FlipSprite();
}
Code in an additional Player_SyncPosition script on the player:
[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;
}
}
Edit:
Player
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;
}
Solution 1:[1]
The upper code work like a charm, but I think it can be more concise
[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;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | thanthuong |