Unet: Sync Rotation Top down 2D does not properly work

I’ve created a simple multiplayer LAN game but the sync rotatation

The rotation axis is Y on the rotation sync mode

I’m using the sync rotation for the Top Down game, but it seems to work only one way on the client, here is the code

using UnityEngine.Networking;
using UnityEngine;

public class Player : NetworkBehaviour {

    public float speed;

    private void Update()
    {
        if (!isLocalPlayer)
        {
            return;
        }

        /*Vector3 dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
        float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);*/

        float x = Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime;
        float y = Input.GetAxisRaw("Vertical") * speed * Time.deltaTime;

        Vector2 movement = new Vector2(x, y);

        if (x != 0 || y != 0)
        {
            transform.right = Vector3.Normalize(new Vector3(x, y, 0));
        }
        



        transform.Translate(x, y, 0, Space.World);
    }

    public override void OnStartLocalPlayer()
    {
        base.OnStartLocalPlayer();

        GetComponent<SpriteRenderer>().color = Color.blue;
    }
}

This is on the client

Then the host and client together, where the client has pressed the left key

Could someone explain a fix and why this happens please, would be very happy if anyone answers :slight_smile:

First of all, check at NetworkIdentity if Local Player Authority is marked. If not, mark it.

Then, the actual problem is how you’re defining the rotation for the player. For some reason, by using this line

transform.right = Vector3.Normalize(new Vector3(x, y, 0));

It sometimes sets it to -180 in the Y axis, instead of 180 in the Z axis. tl;dr you’re probably passing only Z rotation but the calculation is giving you a Y rotation too, which you’re not passing.

  • Either change this line:

    transform.right = Vector3.Normalize(new Vector3(x, y, 0));

to something like

float angle = Vector2.SignedAngle(Vector2.right, movement);
transform.rotation = Quaternion.Euler(0,0,angle);

In this way we are calculating only the Z rotation from a vector pointing right to your movement vector.

  • The other way around it is to set the NetworkTransform Rotation Axis property to either YZ or XYZ (full 3D).

Either should do the trick.

did you solve this problem? i have the same