Search Unity

[Unet] Transform Syncing Problems

Discussion in 'UNet' started by Ice999va, Dec 28, 2017.

  1. Ice999va

    Ice999va

    Joined:
    Jan 2, 2017
    Posts:
    6
    Hi

    I make a billiard online game when i try to sync the transform.position from server to client have i problems by the collisons on the walls ther is to much space between them its lookt wirde and the other balls moves to early befor they touch

    some infos
    Unity version : 2017.3.0f3

    Network : Unet

    Physic : Unity Physics

    all gameobjecs are 2d and on the server side

    this methods i Tried to sync the transform.position

    Unet NetworkTransform : looked laggy

    Vector3.lerp (transform, syncTransform, 0,3) : to much space between the collisons

    Sync the Velocity : looked good but the the position on both sides not the same

    and the asset Smooth Sync : looked okay but ther is to much space between the collison

    i dont really need realtime transmission i need more accuracy

    so i hope you can help me by this problem

    sorry for the bad english i am just a dumb german dude^

    greetings Ice

    here some code that i use but its totaly S***

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class TransformMotion : NetworkBehaviour
    7. {
    8.  
    9.     [SyncVar]
    10.     private Vector3 syncPos;
    11.  
    12.     [SyncVar]
    13.     private float syncYRot;
    14.  
    15.     [SyncVar]
    16.     private Vector2 syncVelo;
    17.  
    18.     [SyncVar]
    19.     private Vector2 syncColVelo;
    20.  
    21.     [SyncVar]
    22.     private Vector3 syncColPos;
    23.  
    24.     //[SyncVar]
    25.     private bool sync;
    26.     private Vector3[] mySyncPos;
    27.     private Vector3 distance;
    28.     private float distance1;
    29.     private Vector3 lastColPos;
    30.     private Vector3 lastPos;
    31.     private Quaternion lastRot;
    32.     private Transform myTransform;
    33.     private Rigidbody2D myRigibody2D;
    34.     private Vector2 lastVelo;
    35.     private bool test = true;
    36.     private int ii = 1;
    37.     private int j = 1;
    38.  
    39.  
    40.  
    41.     [SyncVar]
    42.     private bool isMoving;
    43.  
    44.     [SerializeField]
    45.     private float lerpRate = 10;
    46.     [SerializeField]
    47.     private float posThreshold = 0.5f;
    48.     [SerializeField]
    49.     private float rotThreshold = 5;
    50.  
    51.     // Use this for initialization
    52.     void Start()
    53.     {
    54.         myTransform = transform;
    55.         myRigibody2D = GetComponent<Rigidbody2D>();
    56.         mySyncPos = new Vector3[30];
    57.         mySyncPos[0] = syncPos;
    58.  
    59.     }
    60.  
    61.     // Update is called once per frame
    62.     void FixedUpdate()
    63.     {
    64.  
    65.      
    66.  
    67.  
    68.         Cmd_SynVelo();
    69.         if (!hasAuthority)
    70.         {
    71.             lastVelo = myRigibody2D.velocity;
    72.         }
    73.  
    74.  
    75.  
    76.  
    77.     }
    78.  
    79.  
    80.  
    81.  
    82.  
    83.  
    84.     void Cmd_SynVelo()
    85.     {
    86.         if (hasAuthority)
    87.         {
    88.  
    89.             syncVelo = myRigibody2D.velocity;
    90.             syncPos = transform.position;
    91.  
    92.         }
    93.         // Debug.Log(Vector3.Distance(myTransform.position, syncColPos));
    94.         Debug.DrawLine(myTransform.position, syncPos);
    95.         Debug.DrawLine(myTransform.position, mySyncPos[j], Color.red);
    96.        
    97.         if (!hasAuthority)
    98.         {
    99.             float power = Vector3.Distance(myTransform.position, syncPos);
    100.  
    101.             myTransform.position = Vector3.Lerp(myTransform.position, syncPos, 0.2f);
    102.  
    103.             Debug.Log(power);
    104.  
    105.                 myRigibody2D.velocity = syncVelo;
    106.          
    107.         }
    108.        
    109.     }
    110.    
    111.     void OnCollisionEnter2D(Collision2D col)
    112.     {
    113.         if (hasAuthority)
    114.         {
    115.             syncPos = myTransform.position;
    116.             syncColPos = transform.position;
    117.             syncColVelo = myRigibody2D.velocity;
    118.             Debug.Log("hit");
    119.         }
    120.  
    121.         if (!hasAuthority)
    122.         {
    123.             lastColPos = transform.position;
    124.             if (transform.position == syncColPos)
    125.             {
    126.                 myRigibody2D.velocity = syncColVelo;
    127.                 Debug.Log("SyncColVelo");        
    128.             }
    129.           //  myRigibody2D.velocity = syncColVelo;
    130.  
    131.         }
    132.  
    133.         if (col.gameObject.name == "Dead")
    134.         {
    135.             Destroy(gameObject);
    136.             //   transform.position = new Vector2(Random.Range(-7, 7), Random.Range(-3, 3));
    137.             //  gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(0,0);
    138.         }
    139.  
    140.     }
    141.  
    142.     void OnCollisionExit2D(Collision2D col)
    143.     {
    144.         if (hasAuthority)
    145.         {
    146.  
    147.         }
    148.  
    149.         if (!hasAuthority)
    150.         {
    151.  
    152.         }
    153.  
    154.  
    155.     }
    156.  
    157. }
     
  2. Fuestrine

    Fuestrine

    Joined:
    Feb 13, 2013
    Posts:
    589
    Yo yo, maker of Smooth Sync here. This was happening because it sends every so many seconds and if you hit the wall in-between the send rate, it will not send the position at the wall. I just finished adding a new feature that NetworkTransform doesn't have. You can call forceStateSendNextFrame() when you hit a wall on the host and then the position will be perfect. Import the new version from the store. Let me know if you have any difficulties.