- Home /
(PUN) 2D how to send info to show that sprites were flipped horizontally?
I setup movement and animations but flipping sprites i dont know how to do.
For flipping locally i have:
//flip the sprites
if (Input.GetAxis ("Horizontal") > 0.1f)
transform.localScale = new Vector3 (-1, 1, 1);
if (Input.GetAxis ("Horizontal") < -0.1f)
transform.localScale = new Vector3 (1, 1, 1);
Here is my send and receiving of transform and animation triggers: (remember this is 2D so no rotation)
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
if (stream.isWriting) {
// This is OUR player. We need to send our actual position to the network
stream.SendNext(transform.position);
stream.SendNext(anim.GetFloat("speed"));
stream.SendNext(anim.GetFloat("speedY"));
stream.SendNext(anim.GetBool("grounded"));
stream.SendNext(anim.GetBool("sitting"));
} else {
// This is someone else's player. We need to recieve their position
realPosition = (Vector3)stream.ReceiveNext();
anim.SetFloat("speed",(float)stream.ReceiveNext());
anim.SetFloat("speedY",(float)stream.ReceiveNext());
anim.SetBool("grounded",(bool)stream.ReceiveNext());
anim.SetBool("sitting",(bool)stream.ReceiveNext());
}
Answer by 334499p · Aug 15, 2015 at 07:25 AM
I'd suggest just adding directly on to your OnPhotonSerializeView function. At the end of the stream.isWriting if statement just add on stream.SendNext(flipped); //flipped is a local boolean variable at the top of your script
At the end of the else statement add on
realFlipped = (bool)stream.ReceiveNext();// realFlipped is a network boolean variable at the top of your script
In the Update() function for that script add the following:
if(GetComponent<PhotonView>().isMine){ //If this is my player then use the local variable "flipped"
if(flipped){
transform.localScale = new Vector3 (1, 1, 1);
}else{
transform.localScale = new Vector3 (-1, 1, 1);
}
}else{ //If this is another player accessing my variable
if(realFlipped){
transform.localScale = new Vector3 (1, 1, 1);
}else{
transform.localScale = new Vector3 (-1, 1, 1);
}
}
-Change the flipped variable according to your Input.Axis if statement
The flipped variable is basically YOUR copy of that variable and the realFlipped variable is another player's copy of the variable.
Your answer