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

Multiplayer movement is laggy.

Discussion in 'Multiplayer' started by Uranium, Aug 17, 2013.

  1. Uranium

    Uranium

    Joined:
    Mar 17, 2013
    Posts:
    52
    hello there,
    today i checked my game with another person,
    and when i moved he saw my movement in lags like someone is blocking me everysec,
    and when he moved i saw the same thing.
    i used charactercontroller.move for the movement.
    and on the photonView i made the main object observe it self so the other clients can watch my movement.
    is there anyway to fix that laggy movement?
    the server itself isnt laggy.
     
  2. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    I use Vector3.Lerp to apply smooth movement. You must consider that the network is only sending data a few times a second(best case) therefore you cant expect the movement to be smooth as the local client is asking for a position 60 times a second. To counter this you can lerp between the positions the network has provided in the downtime.

    Code (csharp):
    1. transform.position = Vector3.Lerp(transform.position, truePosition, Time.deltaTime * 5);
    This sets the position of the player to be the transition between the current position and the position provided over the network.

    For example:

    The current position is Vector3(0,0,0) and the position that the network just gave us was Vector3(1,1,1). On frame one the positon will still be (0,0,0) the next frame it will be ~(0.02,0.02,0.02) and so forth. By the time we get the next position from the network we should be at (1,1,1).
     
    Last edited: Aug 19, 2013
  3. Uranium

    Uranium

    Joined:
    Mar 17, 2013
    Posts:
    52
    oh i understand :p
    thank you.
    i will use it instant of my chractercontroller.move and tell you how it works :)
    thanks for the help ! :)

    EDIT:
    its gives me error, because mathf.lerp needs float,float,time.time and i cant use vector3 here...
     
    Last edited: Aug 19, 2013
  4. eskimojoe

    eskimojoe

    Joined:
    Jun 4, 2012
    Posts:
    1,440
  5. Uranium

    Uranium

    Joined:
    Mar 17, 2013
    Posts:
    52
  6. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
  7. Uranium

    Uranium

    Joined:
    Mar 17, 2013
    Posts:
    52
  8. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    People are telling you what you need to do but you don't seem to use it.
    The last received position update from the server is what you need there.
    Do you actually understand what your are doing here ? If not, instead of copying code, you may wan't to ask.
     
  9. Uranium

    Uranium

    Joined:
    Mar 17, 2013
    Posts:
    52
    sorry my english isnt that good...
    uhmm how do i take the last received position update from the server?
     
  10. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    You have to check the PUN docs for the corresponding method but I think your searching for OnSerializePhotonView or something.
     
  11. Uranium

    Uranium

    Joined:
    Mar 17, 2013
    Posts:
    52
    i will check about it.
    thanks
     
  12. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    I feel very generous today, so here is a full guide of how to sync an object across the network with photon.

    • Install the Photon Networking package preferably from the Unity Asset store.


    • Make sure somewhere in your other existing code you connect to Photon Cloud and move into a room.


    • Next, on the object you want synced add a PhotonView(Click on the gameObject -> Add -> PhotonView(script))


    • Add a new script to the GameObject and name it "Sync"(GameObject -> Add -> New Script -> Fill in the requested field)


    • Drag the header of the script "Sync" to the observe field in the PhotonView.

    [HR][/HR]
    Now it's time to script, Yay!


    • Open up "Sync"


    • Your going to need to add the UnityEngine dll if it is not defult.

    Code (csharp):
    1. using UnityEngine;
    This imports all the data from the precomplied dll. More info on using and creating dll : http://msdn.microsoft.com/en-us/library/3707x96z.aspx


    • You also need to have the class inherent from Photon.MonoBehavior instead of MonoBehavior.

    Code (csharp):
    1.  public class Sync : Photon.MonoBehavior
    This tells the class to inherit all of the classes from Photon's version of MonoBehavior.


    • Generate some varibles!


    Code (csharp):
    1.     //sync vals
    2.     Vector3 trueLoc;
    3.     Quaternion trueRot;
    4.     PhotonView pv;
    5.  
    This will hold the position sent across the network and the objects photonView.

    • Now we need to cache the photonView for future use.
    Code (csharp):
    1. void Start(){
    2.        pv = GetComponent<PhotonView>();
    3. }
    This will get the PhotonView on the object this script is attached too.



    • Now, we are going to look at the method OnSerializePhotonView();

    More info about methods: http://msdn.microsoft.com/en-us/library/ms173114.aspx

    Code (csharp):
    1. public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    2.     {
    3.         //we are reicieving data
    4.         if (stream.isReading)
    5.         {
    6.                 //receive the next data from the stream and set it to the truLoc varible
    7.                 if(!pv.isMIne){//do we own this photonView?????
    8.                 this.trueLoc = (Vector3)stream.ReceiveNext(); //the stream send data types of "object" we must typecast the data into a Vector3 format
    9.                }
    10.         }
    11.         //we need to send our data
    12.         else
    13.         {
    14.                 //send our posistion in the data stream
    15.                 if(pv.isMine){
    16.                 stream.SendNext(transform.position);
    17.                 }
    18.         }
    19.     }

    More info on typecasting : http://msdn.microsoft.com/en-us/library/ms173105.aspx

    [HR][/HR]

    • Ok, so now we have the position of the object and we send ours to other clients. Now we have to do something with it!

    Code (csharp):
    1. void Update()
    2.     {
    3.             if(!pv.isMine){
    4.             transform.position = Vector3.Lerp(transform.position, trueLoc, Time.deltaTime * 5);
    5.             transform.rotation = Quaternion.Lerp(transform.rotation, trueRot, Time.deltaTime * 5);
    6.             }
    7.  
    8.     }
    Ok lets break this code down:

    Code (csharp):
    1. void Update(){}
    Update is called every frame, therefore we are going to update the position/rotation every frame.

    Code (csharp):
    1. transform.position = Vector3.Lerp(transform.position, trueLoc, Time.deltaTime * 5);
    Now we take the position of our object and set it equal to the product of Vector3.Lerp(Vector3, Vector3, time);

    Code (csharp):
    1. Vector3.Lerp(transform.position, trueLoc, Time.deltaTime * 5);
    This method takes two positions and moves between them over a set amount of time. Here we move between our current position and the position the network tells us we should be in five seconds. We use this method to avoid choppiness in our movement due to slow network update times. More info on Vector3.Lerp: http://docs.unity3d.ru/ScriptReference/Vector3.Lerp.html

    Code (csharp):
    1. Quaternion.Lerp(transform.rotation, trueRot, Time.deltaTime * 5);
    This is the exact same method except the Quaternion version. Here we take two Quaternion(rotations) and lerp between them over 5 seconds. More info: http://docs.unity3d.ru/ScriptReference/Quaternion.Lerp.html

    [HR][/HR]

    There you have it! The object should sync across two networked computers. If anyone needs a better explanation of any topics within this post(or not ;)) please ask and I'll try my best to answer.

    PS: If anybody finds some typos, errors, bad code, etc. Please inform me!

    Edit: Thinking about making this post a thread. Opinions?
     
    Last edited: Aug 20, 2013
    Jalzu07, pearsauce, DDorukA and 4 others like this.
  13. Uranium

    Uranium

    Joined:
    Mar 17, 2013
    Posts:
    52
    i was trying to do so but i cant put my script on the "observe" option on my photonview :/
    why cant i?

    edit:
    nvm worked :D
    waiting for your editing xD
     
    Last edited: Aug 19, 2013
  14. fire95

    fire95

    Joined:
    Jan 21, 2013
    Posts:
    2
    landon91235, I LOVE YOU!
     
  15. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350