Search Unity

[Solved ]Network Transform wont update Rotation 2D

Discussion in 'Multiplayer' started by Gunging, Dec 9, 2016.

  1. Gunging

    Gunging

    Joined:
    Sep 6, 2016
    Posts:
    139
    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:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using UnityEngine.Networking;
    5.  
    6. public class Movement : NetworkBehaviour {
    7.      [SyncVar] Quaternion rot;
    8.    
    9.      void Update() {
    10.      if (!isLocalPlayer) {transform.rotation = rot; }
    11.      if (!isLocalPlayer) { return;}
    12.      rot = transform.rotation;
    13.      }
    14. }
    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
     
  2. Gunging

    Gunging

    Joined:
    Sep 6, 2016
    Posts:
    139
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class Movement : NetworkBehaviour {
    6.     public float moveSpeed;
    7.  
    8.     void Update () {
    9.         if (!isLocalPlayer) { return; }
    10.         Move();
    11.     }
    12.  
    13.     void Move()
    14.     {
    15.         GetComponent<Rigidbody2D>().angularVelocity = 0;
    16.         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; } }
    17.         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); } }
    18.     }
    19. }
    It now works :D
     
    RemDust likes this.
unityunity