Sync Problems

Hi, im trying to Sync 2 player´s position and all the time I´m having "lag".
This is the code I used:

void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
stream.SendNext(rb2d.position);
else
rb2d.position = (Vector3)stream.ReceiveNext();

Debug.Log("Info: " + rb2d.position);
}

But I have this error and I don´t now if this error may be the problem:
https://gyazo.com/33a9dd929ebce5bb1a3ed378b4ee2d67

I also used the interpolation metod but I had the same "lag".

Comments

  • OneManArmy
    OneManArmy ✭✭✭
    edited February 2018
    Hi, info is not sent every frame, so movement can't be smooth.
    You must smooth out movement by yourself. You can use:
    https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

    Or you can use Photon Transform View.
    Set Interpolation option to Lerp and adjust Lerp speed.
  • I used Vector3.Lerp and it worked a bit.

    This is how it looks now:
    https://gyazo.com/c6c89ec9a943fd3396e54117b6a1bd00

    But if I can I would like to improve it.

    In the code I writted this:
    https://gyazo.com/fd00aecb73bf5019b7f1901c5cca63b2

    There is the Lerp function:
    https://gyazo.com/68d1666f1b8d1ffffb02a09c913a96a2

    realPosition is a Vector3 and realRotation is a Quaternion.
  • for transform.position replace "0.5f" with "5.0f * Time.deltaTime".
    if not enough smooth movement, then increase 5.0f to 6.0f or higher
  • I´ve trying some values from "5.0f* Time.deltaTime" to "1000000.0f * Time.deltaTime", It looks a bit better but I don´t think i have to put more than 1000000.0f it looks absurd
  • wait, do you disable rigidbody on remote clients?
  • Probably no, because i don´t know how to do it
  • OneManArmy
    OneManArmy ✭✭✭
    edited February 2018
    By default isKinematic must be set to true and useGravity to false.
    when you instantiate player locally then set isKinematic = false and useGravity = true

    Example:
    GameObject player = PhotonNetwork.Instantiate ("Player", Vector3.zero, Quaternion.identity, 0);
    Rigidbody rb = player.GetComponent<Rigidbody>();
    rb.isKinematic = false;
    rb.useGravity = true;
  • I put it in my NetworkManager.cs where I have the instanciate, here it is:
    https://gyazo.com/8994689c69c47e0a99bc9897f5f3c456

    But it doesn´t work

    gif of the game:
    https://gyazo.com/8bf7a79a3517893d62fbbe0b990990f4
  • useGravity by default is disabled?
    what values you use for lerp?
  • javucha
    javucha
    edited February 2018
    My Lerp function is this right now:

    transform.position = Vector3.Lerp(transform.position, realPosition, 7.0f * Time.deltaTime);
    transform.rotation = Quaternion.Lerp(transform.rotation, realRotation, 7.0f * Time.deltaTime);

    And about the useGravity in the "player" which is dynamic, the gravity is enabled, but i don´t really know if this is what you mean with "by default".
  • select player prefab in your project and in Inspector window untick "Use Gravity" and tick IsKinematic on your rigidbody component.
  • My "player" is using a RigidBody2D so there aren't those options, should I put the normal RigidBody in a 2D game or it doesn´t matter?
  • OneManArmy
    OneManArmy ✭✭✭
    edited February 2018
    Ah right. Then you can set bodyType to Kinematic in your prefab and then:
    GameObject player = PhotonNetwork.Instantiate ("Player", Vector3.zero, Quaternion.identity, 0);
    Rigidbody rb = player.GetComponent<Rigidbody2D>(); 
    rb.bodyType = RigidbodyType2D.Dynamic;
    Or, simply disable rigidbody component and locally enable.
  • javucha
    javucha
    edited February 2018
    It looks better I think:
    https://gyazo.com/4127398432133154f57fedbf651d68c0

    But I saw that the player who joins throught the .exe is kinematic and the player who is playing using unity is dynamic.
  • remove Transform from observed components.
  • ¡Eureka, finally it works! thank you very much men, it looks fine now.

    With a little delay but i think this is because of the servers
    gif:
    https://gyazo.com/fe0434c9bb3f6a48aa4f72b185c05328
  • There always will be some delay.
    To get best result try to adjust smoothing.
  • Hi again, I have another problem trying to sync one GameObject.

    When someone enters the room with "InstantiateSceneObject" I create a cube wich can be caught and thrown.

    The problems is, when there are 2 players in the room, one is the owner of that cube and when the owner moves the cube no problem, but when the other player try to move the cube the owner don´t see how the cubes moves.

    I try to use the ownership transfer but i don´t know if I use it well.
  • Hi @javucha, hi @OneManArmy,

    we have published a new documentation page about Lag Compensation which hopefully helps removing most of the delay which occurs upon synchronization. You can have a look at it over here.

    I try to use the ownership transfer but i don´t know if I use it well.


    Transferring the Ownership is the right way to go here. You can take a look at the Ownership Transfer demo from the PUN package to see how it is implemented there. There is also a related documentation page available.