I’m trying to create a 2D multiplayer game with new networking system. I attached Network Transform to player prefab and it detected rigidbody2d and top down Y rotation to sync. Rigidbody syncing but rotation is not sync. What should I do to sync rotation?
Rotations dont seem to sync with NetworkTransform, so instead make your facingRight a syncvar, then as the local client, send the Command to server to change the syncvar. And let all clients calculate the rotation.
havent tested this but it should work. So if your move is greater than 0 then youre facing right, or else facing left. And so this value is sent to server so it could be changed on all clients, however it wont change for client who sent the value. Also the part where the transform is flipped is ran on all clients.
[SyncVar(hook="OnFacingRight")]
public bool facingRight; //this bool will sync on all clients
//this function will execute because of the syncvar hook
void OnFacingRight(bool newValue)
{
//the new value is to all clients except the local client
if(!isLocalPlayer)
{
facingRight = newValue;
}
}
[Command]
void CmdFlipTo(bool isRight)
{
facingRight = isRight;
}
bool lastFacingRight;
void Update()
{
if(isLocalPlayer)
{
//facing right equals to true if moving right, or else its moving left
facingRight = move > 0;
//used to save bandwidth, so it sends only when its changed
if(lastFacingRight != facingRight)
{
lastFacingRight = facingRight;
CmdFlipTo(facingRight); //value set to all clients
}
}
//this will run for all clients
if(facingRight)
{
transform.localEulerAngles = Vector3.zero;
}
else
{
transform.localEulerAngles = new Vector3(0,180,0);
}
}