- Home /
Flipping sprites in Multiplayer doesn't flip host
Hello.
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 NetworkBehaviour 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;
}
}
Answer by AbsinthePie · Jan 05, 2016 at 08:15 AM
I experienced a whole variety of different problems (moving backwards etc.) and this is working:
Player: Player_SyncPosition syncPos;
void Awake()
{
syncPos = GetComponent<Player_SyncPosition>();
}
void Update()
{
if ((input.x > 0 && !facingRight) || (input.x < 0 && facingRight))
{
facingRight = !facingRight;
syncPos.CmdFlipSprite(facingRight);
}
}
Player_SyncPos
[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;
}
}
It works nice for me! Thanks! :) In my case, i used those functions over my player movement script. Just to be curious, are you using another class "Player_SyncPosition" to do this? or is that a part of another class?
Answer by NinjaISV · Dec 22, 2015 at 05:29 PM
I can see the problem already. In the FlipSprite()
function you check to make sure that this is the local player by using an if statement that has isLocalPlayer
in it. Then in the CmdDlip()
function, you check to make sure it is not the local player by using an if statement that says !isLocalPlayer
this will prevent the function from ever being called.
Your answer
Follow this Question
Related Questions
Flipping Sprite on Client/Over Network 0 Answers
Unity vs client-side prediction 0 Answers
SyncVar Hook not Updating after Client Joins 0 Answers
How can I animate a child sprite of a parent object? 0 Answers
My sprite animation gets messed up after a certain event occurs! How do I fix it? 0 Answers