[Solved ]Network Transform wont update Rotation 2D

Ive found lots of other resolved questions, (Like 10) about 3D and some others (3) about 2D, but everytime I try to adapt the code to my project it wont work, Id give a sample of what Ive tried, but its that I dont really know how to use [SyncVar] and get an error “Could not build Network Behavior” aside with some others, This must be real simple, but whatever I do just doesnt work xd.

This is my closest attempt:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.Networking;

public class Movement : NetworkBehaviour {
     [SyncVar] Quaternion rot;
   
     void Update() {
     if (!isLocalPlayer) {transform.rotation = rot; }
     if (!isLocalPlayer) { return;}
     rot = transform.rotation;
     }
}

I dont really know what happens here, Apparently, only one Client gets each other’s rotation? I used 2 windowed games and the editor to test this in an online host, and the guy who created the game was the only one that percieved the other’s rotation, but when I use a windowed version and the editor, the client is the one that percieves the rotations, not the host.

I am really confused, never coded multiplayer before, I dont understand, please help xd

Ok thank so much to whoever those 20 views are from, for at least taking a view to my problem, I now know what it was:

The problem wasnt it not syncing rotation, I, have my 2D game and you collide with lots of things, when coding singleplayer I hated the Rigidbody2D’s angularVelocity cause my move code’s rotation section does not counters it, so It stayed there annoying, but as its my first game, I didnt know it was it, then I constrained z rotation, so only the move code can make it rotate, and right now I have seen that the network transform was syncing the Rigidbody, I didnt know that, and the Rigidbody has no rotation, it has angular Velocity, and by constrainig z Rotation, it cancelled angular Velocity and it wouldnt rotate. So now, I dont have any constraints in the Rigidbody and the final code is:

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class Movement : NetworkBehaviour {
    public float moveSpeed;

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

    void Move()
    {
        GetComponent<Rigidbody2D>().angularVelocity = 0;
        if (Input.GetKey(KeyCode.UpArrow)) { GetComponent<Rigidbody2D>().velocity = transform.up * moveSpeed; } else { if (Input.GetKey(KeyCode.DownArrow)) { GetComponent<Rigidbody2D>().velocity = -transform.up * moveSpeed; } else { GetComponent<Rigidbody2D>().velocity = Vector2.zero; } }
        if (Input.GetKey(KeyCode.LeftArrow)) { transform.Rotate(0, 0, 100 * Time.deltaTime * moveSpeed); } else { if (Input.GetKey(KeyCode.RightArrow)) { transform.Rotate(0, 0, 100 * Time.deltaTime * -moveSpeed); } else { transform.Rotate(0, 0, 0); } }
    }
}

It now works :smile:

1 Like