- Home /
How To Sync Scale in UNET
Im New to networking and Unity as well, so if this is a simple mistake pls bear with me. Im working on a 2d multiplayer project. I have this NetworkBehaviour
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class LocalPlayerSetup : NetworkBehaviour {
void FixedUpdate(){
flipClients(netFacingRight);
}
void flipClients(bool facing){
if (!isLocalPlayer) {
netFacingRight = facing;
if (netFacingRight)
{
Vector3 flipSprite = this.transform.localScale;
flipSprite.x = 1;
transform.localScale = flipSprite;
}
else
{
Vector3 flipSprite = this.transform.localScale;
flipSprite.x = -1;
transform.localScale = flipSprite;
}
}
}
[SyncVar(hook = "CmdFlipSprite")]
public bool netFacingRight = true;
[Command]
public void CmdFlipSprite(bool facing)
{
netFacingRight = facing;
if (netFacingRight)
{
Vector3 flipSprite = this.transform.localScale;
flipSprite.x = 1;
this.transform.localScale = flipSprite;
}
else
{
Vector3 flipSprite = this.transform.localScale;
flipSprite.x = -1;
this.transform.localScale = flipSprite;
}
}
void Start () {
if (isLocalPlayer) {
GetComponent<PlayerManager>().enabled = true;
}
}
}
My problem is that, in the Client app, the host's sprite is not flipping. In the Host i can see everything is working well.
Here is the part of PlayerManager where i call CmdFlipSprite() facingright is Initially true.
void Update (){
MovePlayer (speedX);
if (facingright && speedX < 0 || !facingright && speedX > 0) {
facingright = !facingright;
flip();
local.CmdFlipSprite(facingright);
}
}
Hope that someone can help me. Thanks! :)
Hello, do you $$anonymous$$d accepting my answer since it is correct. It really makes me stay motivated to answer these questions.
Answer by SadSwede · Dec 03, 2016 at 05:13 PM
I had the same issue with sprite changing awhile ago but i fixed it by removing my syncvars and using these 2 functions in a script instead.
[Command]
public void CmdSpriteChange(int sprite)
{
RpcSpriteChange(sprite);
}
[ClientRpc]
void RpcSpriteChange(int sprite)
{
if (sprite == 0)
{
graphic.sprite = sideways;
}
else if (sprite == 1)
{
graphic.sprite = forward;
}
}
so let's say i want to set a new sprite for EVERY client on the server. To do that i call this function locally:
CmdSpriteChange();
Like this:
void Start()
{
if(isLocalPlayer)
{
CmdSpriteChange(1);
graphic.sprite = forward;
}
}
I call the "Cmd" function to make it change on everyone elses screens, and to make it change on yours you just say " graphic.sprite = forward;"
I'am not good at explaining so if it's hard to understand just tell me and ill try one more time ^^
I LITERALLY just created an account to thank you.. it has been on my head for TWO WHOLE days!! THAN$$anonymous$$ YOU you kind sir!!!
Answer by AurimasBlazulionis · Sep 07, 2016 at 05:07 PM
First of all, this:
[SyncVar(hook = "CmdFlipSprite")]
Remove the hook. Basically now, the local player will tell the server to run that command once the value changes, other players will not sync the value since they are not allowed to call commands on objects without authority, even if they could, the commands run only on the server.
If you want to flip it, on local player call CmdFlipSprite
.
Keep in mind, if you change these things often and need fast reaction, on local player you will have to predict the outcome, this is up to you to implement. The good start would be to create a hook, in which the clients check isLocalPlayer
and if it is not true, then blindly accept the change and if it is true, first check if your predictions are correct and if they are not, then change the value.
Sorry misclicked, I was just checking this one out. BTW when you say
If you want to flip it, on local player call CmdFlipSprite.
is it the LocalPlayerSetup.cs or on my Player$$anonymous$$anager?
Oh right, you do that already. So, if you do not care about predictions, just remove the hook line.
O$$anonymous$$ ill try this once I get home from school. Thanks for the answer! :)
I removed the hook line but its still the same. Still cant see the host's sprite flip on the client. Should I try to sync their local scales ins$$anonymous$$d?