Sync transform all via server

Options
Hi,
By now I need to make a multiplayer game.
When i connect two client to same room, its movement seem does not have same position.
How can I sync the transforms of multi clients all via server?

Comments

  • Hi @rAinmAker,

    are those game objects synchronized at all right now? An easy way to synchronize the transform component of multiple game objects is to use PUN's PhotonTransformView component that is included in the PUN package. This also provides you some synchronization options you can check out and test which combination fits best.
  • rAinmAker
    Options
    @Christian_Simon I already tried PhotonTransformView but i realise that on local client it change position faster than remote. This is nonsense becuz I will have many players on the scene.
  • This is expected behaviour because the local client will update his position immediately, however the remote client needs to wait until he receives this update. So you basically have this situation:

    - Time for sending a message from client to the server
    - A few ms for handling the message on the server
    - Time for sending a message from server to another client

    Based on your location, connection and routing to the Photon Cloud Servers this might take some milliseconds.
  • Hasi
    Hasi
    edited July 2017
    Options
    Hi @rAinmAker ,

    I was also facing the same problem and I will tell you a way that how I achieved the required behavior by using custom programing. You can use OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) and in this function you can check that if stream.isWriting() it means that this is your player and you need to send you transform positions to other players and and if stream.isreading() that you need to receive transform positions from other clients. I am not posting the full code as I am expecting you to do it by yourself so try and if still there you are facing problems then let me know I will help you more.

    Note: stream.SendNext(transform) is used to send data.
    Note: stream.ReceiveNext(transform) is used to receive data.

    I hope it helps.

  • rAinmAker
    Options
    @Hasi I will try and let you know the result. Thank for suggestion.
  • rAinmAker
    Options
    @Hasi I think stream.SendNext(transform) and stream.ReceiveNext(transform) is default implemented with PhotonTransformView. What do I miss?
  • Hasi
    Options
    @rAinmAker Yes you are right stream.sendNext() and stream.recieve.next() is default implementation but in Update() method you can call this


    if(photonView.isMine){
    ///Do Nothing
    }
    else{
    //This is not your player you need to set his position which are received by stream.recieveNext()
    //You can do it like this

    transform.position = Vector3.Lerp (transform.position, Position, 0.1f);
    transform.rotation = Quaternion.Lerp (transform.rotation, Rotation, 0.1f)
    ;
    }
  • Hasi
    Options
    @rAinmAker

    Note: 0.1f is a float which is used tell that after 0.1f seconds you need to change transform position.
  • rAinmAker
    Options
    @Hasi
    As you said, I think this will not resolve my problem.

    Which I want is something like:
    - When I click to a position on screen, locally the position of player will not change until he receive signal from server to update his position.
    - And from other client update his position from server too.

    Please help me out!
  • Hasi
    Options
    @rAinmAker

    Sorry as I misunderstood your question. Ok I can give you an idea as I don't have proper implementation of your problem. Idea is that you need to save the position of object when clicked somewhere and don't update the position just save it in a vector3 or transform. And make a PunRPC call from other client which can be treated as a server that player clicked here so you need to change the position and change the position.
  • rAinmAker
    Options
    @Hasi
    Thank you for suggestion.
    Then I will need to call at least 10 RPC per second. Is this efficient?