Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

New Unity3D Networking eBook and Tutorial with Code Samples

Discussion in 'Multiplayer' started by Quadgnim, Nov 19, 2012.

  1. Quadgnim

    Quadgnim

    Joined:
    Sep 10, 2012
    Posts:
    132
    For those interested, the eBook was created as part of a research project for the game I'm working on, Pulsar. Get an early preview of it here: http://youtu.be/5tYYCcBSpMs
     
  2. Pfaeff

    Pfaeff

    Joined:
    Nov 4, 2012
    Posts:
    27
    I don't know if it has been already mentioned, but there seems to be minor flaw in your interpolation code. The movement seems to be very jittery when using a real network connection. Using the following addition in the interpolation part solved the problem for me:

    Code (csharp):
    1. rigidbody.velocity = Vector3.Lerp(lhs.velocity, rhs.velocity, t);
    Is there a reason, this code runs in the Update() routine? Wouldn't FixedUpdate() work just as good or even better (because the code affects physical objects)?
     
    Last edited: Feb 26, 2013
  3. Quadgnim

    Quadgnim

    Joined:
    Sep 10, 2012
    Posts:
    132
    Pfaeff,

    Intersting resolution. Thanks for the post. Yes I agree that FixedUpdate is better. Not to make excuses, but the code I used for NetworkRigidBody was taken from other Unity Tutorials, and never caught the issue before, I think because I was moving by updating the position and not leveraging a force on the rigid body. I gave a special thanks to the Star Trooper Multi-Player Tutorial by Andrius Kuznecovas which is where I got that code. I don't know where he got it from.

    I could see using Velocity for Extrapolation/prediction as we don't know where the position is and need to use velocity to get there. However, during interpolation we know exactly where we want to end up so position should be right. That being said, I agree it wasn't working right. When I changed my own code to use a force I ran into the same issue. I resolved it differently using code from Duhprey, included below, but this is for Photon (similar to Unity Networking). If Pfaeff's code works under all situations, then it's definitely a simpler solution and I would suggest using it. If however using velocity in the interpolation doesn't consistently produce the desired results then the below code might help:


    http://forum.exitgames.com/viewtopic.php?f=17&t=2447#p11442
     
  4. Pfaeff

    Pfaeff

    Joined:
    Nov 4, 2012
    Posts:
    27
    I think the original code was inspired by this. I'm not entirely sure, why you also need to update the velocity either, because, as you mentioned, interpolation should work just fine. I think it has to do with the object being a rigidbody and the position calculated by the physics engine conflicting with the interpolated one. Extrapolation also seems to have some issues (at least in my code), because I have players partially running "into" walls etc. I thought predicting intersections wouldn't be necessary, because the physics engine takes care of it before rendering, but that doesn't seem to be the case.

    Nevertheless, I found your book to be very helpful. Before reading it, I didn't even know I had a huge misconception about the way NetworkViews work. I haven't read the whole book, yet, but I think there is still a lot to learn :).
     
  5. Quadgnim

    Quadgnim

    Joined:
    Sep 10, 2012
    Posts:
    132
    Thanks I appreciate the kind words. We're always learning. I've learned a ton myself since writing the book. If I ever have time I'd like to develop an advanced guide, or maybe a combination advanced + Photon networking. A lot gets tricky when handling changing levels, especially if different players can be in different levels at the same time (such as my game). If you ever have any questions about the eBook or how to do something advanced, please don't hesitate to ask and I'll do what I can.
     
  6. Pfaeff

    Pfaeff

    Joined:
    Nov 4, 2012
    Posts:
    27
    The reason why rigidbody.velocity needs to be interpolated and also the cause for several other issues I had, is that packets received in OnSerializeNetworkView would get the send time assigned as their time stamp. This is later compared with Network.time. So Network.time - interpolationBackTime leads the desired value by the current network latency. If you use Network.time instead of NetworkMessageInfo.timestamp, it should work fine, imo.
     
    Last edited: Mar 1, 2013
  7. Wilbert-Blom

    Wilbert-Blom

    Joined:
    Aug 13, 2011
    Posts:
    109
    Links are dead.
    Is the e-book still available somewhere ?
     
  8. Quadgnim

    Quadgnim

    Joined:
    Sep 10, 2012
    Posts:
    132