Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Change Network Transform Mode

Discussion in 'Multiplayer' started by Ali-Present, Nov 8, 2015.

  1. Ali-Present

    Ali-Present

    Joined:
    Nov 8, 2015
    Posts:
    3
    Hi fellas , i searched through a Multiplayer Topics and couldn't find the answer to fix my problem .
    i want to change NetworkTransformMode by C# code but for some reason compiler dosen't accept my
    syntax. to here is my piece of code :

    Code (CSharp):
    1. if (GetComponent<Rigidbody2D>().velocity.x > 0)
    2.          {
    3. GetComponent<NetworkTransform> ().TransformSyncMode = GetComponent<NetworkTransform> ().TransformSyncMode.SyncTransform;
    4.  
    5.        transform.localScale = new Vector2 (1f, 1f);
    6.        GetComponent<NetworkTransform> ().TransformSyncMode = GetComponent<NetworkTransform> ().TransformSyncMode.SyncRigidbody2D;
    7.          }
    8.        else if (GetComponent<Rigidbody2D> ().velocity.x < 0)
    9.          {
    10.  
    11.        GetComponent<NetworkTransform> ().TransformSyncMode = GetComponent<NetworkTransform> ().TransformSyncMode.SyncTransform;
    12.        transform.localScale = new Vector2 (-1f, 1f);
    13.        GetComponent<NetworkTransform> ().TransformSyncMode = GetComponent<NetworkTransform> ().TransformSyncMode.SyncRigidbody2D;
    14.          }
    and give me a error like this :
    Assets/MyProject/Script/Char_Controller.cs(77,113): error CS0572: `TransformSyncMode': cannot reference a type through an expression; try `UnityEngine.Networking.NetworkTransform.TransformSyncMode' instead
     
  2. Lork

    Lork

    Joined:
    Jul 12, 2013
    Posts:
    79
    Did you try doing following the advice of the error message? Instead of referencing an instance of NetworkTransform like this
    Code (csharp):
    1.  GetComponent<NetworkTransform>().TransformSyncMode= GetComponent<NetworkTransform>().TransformSyncMode.SyncRigidbody2D;
    and so on, you should be using a static reference to the enum like this :
    Code (csharp):
    1.  GetComponent<NetworkTransform>().transformSyncMode = NetworkTransform.TransformSyncMode.SyncRigidbody2D;
    Pretty much exactly like the error message says. That should at least make the code compile, though I suspect that won't be the end of your problems. What exactly are you trying to do?

    On the topic of changing the mode of NetworkTransform, I feel like it's worth mentioning (read: complaining) that it can't be done during runtime. Or if it is possible, it can't be done gracefully anyway, as it generates exceptions when data from the server differs from the way it was before you changed the mode, even if you change it on both ends.

    Say I want to sync both the position and rotation of an object sometimes, but only the position other times. NetworkTransform should be able to accommodate this.

    Edit: actually, you're also trying to change the definition of the enum rather than the member making use of it. You need to be setting transformSyncMode, NOT TransformSyncMode. I've changed my example to match.
     
    Last edited: Nov 8, 2015
  3. Ali-Present

    Ali-Present

    Joined:
    Nov 8, 2015
    Posts:
    3
    Hi Lork and Thanks for your answer:)

    Actually am trying to make 2d platformer game (LAN only). when i try SyncTransform on NetworkTransform Component to sync my Sprite it's getting super laggy on the other machine and when i try SyncRigidbody2D it's smooth but cant Turn the character.:(
     
  4. Lork

    Lork

    Joined:
    Jul 12, 2013
    Posts:
    79
    In that case I'd suspect that there's either something wrong with your movement and rotation code, or there's a bug with SyncRigidbody2D mode. Either way, I think you'd be much better off attacking this from other angles rather than trying to rapidly switch modes. I don't think that's ever going to work, even if they implement proper support for switching modes mid-game as I've requested.

    If I'm reading the intent of your code right, it looks like the way you "turn" your characters is by changing their scale to "stretch" them in the direction they're facing, correct? That's probably not being picked up by SyncRigidody2D as it's a change to the transform that doesn't affect the rigidbody. You could try using a different method for turning your characters, such as one that actually rotates them in physical space. Another solution might be to handle the "stretching" on the client side, so rather than changing the scale on the server and attempting to send that to the client, you would simply send the velocity, then change the scale in response to that on the client.
     
  5. Ali-Present

    Ali-Present

    Joined:
    Nov 8, 2015
    Posts:
    3
    Exactly

    i think lack of knowledge and experience in network programming driven me into this problem .
    like you said i have to attack this issue from another angel which in this case is sending a arguments to server.
    i will be appreciation if you link me an affective tutorial, because Scripting Reference and unity manual didn't help me that much .

    Thanks:)