When using Unet’s default Network manager, Rotation of my sprites isn’t synced.
I’m using the following manager setup:
This is my Player object setup:
How I’m rotating the sprite:
void RLflip(int rflipcode){
if (rflipcode != flipcode) {
if(flipcode == 0){
transform.Rotate(new Vector3(0,180,0));
flipcode = 1;
}else{
transform.Rotate(new Vector3(0,-180,0));
flipcode = 0;
}
}
}
Any Ideas?
thanks in advance.
Signed, Vandie.
Looking at the script you linked I see the problems:
- You’re setting the Vector3 SpriteRot
to the transform.localRotation every
update on the Server.
- You’re also
using your RLflip only on the client,
which won’t transmit the details to
the server to distribute.
To correct this you need to do the following:
Setup a Command to send a request to the Server to flip the Sprite when the player hits Left or Right
- Remove the if(isServer) from your Control function
- (Optionally) Remove the if(!isServer) from the UpdateRot function
Here’s a quick script I built to Sync Rotation based on client input:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class RotationSync : NetworkBehaviour
{
[SyncVar(hook = "UpdateRot")] public Vector3 SpriteRot;
void Update ()
{
if (isLocalPlayer)
{
Control();
}
}
void Control()
{
if(Input.GetKey(KeyCode.RightArrow))
{
CmdFlip(false);
}
else if(Input.GetKey(KeyCode.LeftArrow))
{
CmdFlip(true);
}
}
[Command]
void CmdFlip(bool Flag)
{
if(Flag)
{
SpriteRot = new Vector3(-180,0,0);
}
else
{
SpriteRot = new Vector3(180,0,0);
}
}
void UpdateRot(Vector3 NewPos)
{
transform.Rotate (NewPos);
}
}
As a side note, the above works on its own, you wouldn’t want to use the Network Transform component.
Credit to @KaldrisRelm as without them I would never have figured this out.
The script I ended up using was halfway fixed when they posted there final answer but it was based of their second comment.
At the top of my C# script I made sure that the following two variables where there:
[SyncVar] private int flipcode;
[SyncVar] private Quaternion SpriteRot;
I used the following code to flip all of the players on the server rather than the individual clients:
[Command]void Cmd_RLflip(int rflipcode){
if (rflipcode != flipcode) {
if(flipcode == 0){
transform.Rotate(new Vector3(0,180,0));
flipcode = 1;
}else{
transform.Rotate(new Vector3(0,-180,0));
flipcode = 0;
}
SpriteRot = transform.localRotation;
}
}
And then used one final method to sync it to the clients (which is called each Update) only when there has been a change in direction:
void SyncRot(){
if(!isServer && transform.localRotation != SpriteRot){
transform.localRotation = SpriteRot;
}
}
Again this is a heavily fixed version of Kaldris’s second comment so I thank them for the help.