Author Topic: What is the best way to sync positions, rotations ?  (Read 7959 times)

JackWhite20

  • Guest
What is the best way to sync positions, rotations ?
« on: December 07, 2013, 05:45:49 AM »
Hey all,
does anybody have a good way to sync postions and rotations so that it is very smooth ?
At the moment i am using this code:
  1.     private Vector3 lastPosition;
  2.  
  3.     private Quaternion lastRotation;
  4.  
  5.     private Transform myTransform;
  6.  
  7.     void Start()
  8.     {
  9.         if (tno.isMine == true)
  10.         {
  11.             myTransform = transform;
  12.  
  13.             tno.Send(2, Target.Others,
  14.                             myTransform.position, myTransform.rotation);
  15.  
  16.             tno.SendQuickly(1, Target.Others,
  17.                 TNManager.playerName);
  18.  
  19.  
  20.         }
  21.         else
  22.         {
  23.             enabled = false;
  24.         }
  25.     }
  26.  
  27.     void Update()
  28.     {
  29.         if (Vector3.Distance(myTransform.position, lastPosition) >= 0.1)
  30.         {
  31.             lastPosition = myTransform.position;
  32.  
  33.             tno.SendQuickly(2, Target.Others,
  34.                             myTransform.position, myTransform.rotation);
  35.         }
  36.  
  37.  
  38.         if (Quaternion.Angle(myTransform.rotation, lastRotation) >= 1)
  39.         {
  40.             lastRotation = myTransform.rotation;
  41.  
  42.             tno.SendQuickly(2, Target.Others,
  43.                             myTransform.position, myTransform.rotation);
  44.         }
  45.     }
  46.  
  47.  
  48.     [RFC(2)]
  49.     void updateMovement(Vector3 newPosition, Quaternion newRotation)
  50.     {
  51.         transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime * 8);
  52.         transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * 8);
  53.     }
  54.  
  55.     [RFC(1)]
  56.     void changeName(string newname)
  57.     {
  58.         gameObject.name = newname;
  59.     }

And i am not very happy with this :/ I want to have a very very smooth sync because its importent...
I have read about interpolation and its sounds good and looks nice. Did anybody know how to do it with TNet ? Because i want to use TNet but if i dont find a better way to sync objects smooth i have to use Photon or so :/ :(
Pls help :)

Manmax75

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 3
  • Posts: 32
    • View Profile
Re: What is the best way to sync positions, rotations ?
« Reply #1 on: December 07, 2013, 08:39:09 AM »
I implemented an interpolation method which you can use here if you like:

http://www.tasharen.com/forum/index.php?topic=5788.msg30798#msg30798

JackWhite20

  • Guest
Re: What is the best way to sync positions, rotations ?
« Reply #2 on: December 07, 2013, 08:53:28 AM »
I have now the three Classes(Global, NetworkTime and SyncNetworkObject).
What did i have to do now exactly to work with it correctly ?
Thx for the help :)

Manmax75

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 3
  • Posts: 32
    • View Profile
Re: What is the best way to sync positions, rotations ?
« Reply #3 on: December 07, 2013, 09:30:29 AM »
The Global and NetworkTime classes are just singletons. Just whack them on any gameobject and it should remain persistent through the scene.

Then put the SyncNetworkObject on the object you want to sync data for (e.g. a rigidbody or whatever)

JackWhite20

  • Guest
Re: What is the best way to sync positions, rotations ?
« Reply #4 on: December 07, 2013, 09:35:16 AM »
So i have a NetworkManager object with TNManager etc on. Can i place it there ?

JackWhite20

  • Guest
Re: What is the best way to sync positions, rotations ?
« Reply #5 on: December 07, 2013, 10:25:25 AM »
When i want to add "Global" to the Object it says: "Can't add script behaviour Global. The script class can't be abstract!"

JackWhite20

  • Guest
Re: What is the best way to sync positions, rotations ?
« Reply #6 on: December 07, 2013, 10:54:27 AM »
When i am using SyncNetworkObject it very laggy and its jerking...Did i made something wrong ?? Pls helped me i dont want to use Photon :(((
I only want a very smooth sync method :/

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: What is the best way to sync positions, rotations ?
« Reply #7 on: December 07, 2013, 04:24:42 PM »
Most common mistake is synchronizing too often. In most cases what you want to do is sync infrequently -- 1-2 times per second. Rest of the time use interpolation.

I did that in the MPGSK (Multi-Purpose Game Starter Kit) available on the Asset Store. It's a physics-driven game kit where I sync pos/rot/velocity only when somethinig happens (like explosion), or infrequently (4 times per second there, but even that is too much). The renderer is using a spring lerp to move after the rigidbody, so it's always ever-so-slightly behind, but any irregularities in movement get smoothed automatically.

JackWhite20

  • Guest
Re: What is the best way to sync positions, rotations ?
« Reply #8 on: December 07, 2013, 04:53:27 PM »
Did you have some code for me to work with ?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: What is the best way to sync positions, rotations ?
« Reply #9 on: December 07, 2013, 08:21:47 PM »
If you are using TNAutoSync or SyncRigidbody scripts, set them to update 1 to 4 times per second (in inspector).

If something important happens, Sync().

Attach the Lag script to the renderer, and make sure that the renderer and rigidbody are on different objects. Look at the 2nd example that comes with TNet, I do it there.