# Multiplayer movement is laggy.

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.

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.

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).

oh i understand
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â€¦

See:

well i cant use mathf.lerp since its taking float,float,time.time and i cant use transform.position there or Vector3 thereâ€¦

Vector3.Lerp
static Vector3 Lerp(Vector3 from, Vector3 to, float t);

Description
Linearly interpolates between two vectors.

http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html

what should i type on my â€śtruePositionâ€ť?
transform.position = Vector3.Lerp(transform.position, truePosition, Time.deltaTime * 5);

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.

sorry my english isnt that goodâ€¦
uhmm how do i take the last received position update from the server?

You have to check the PUN docs for the corresponding method but I think your searching for OnSerializePhotonView or something.

thanks

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.

Now itâ€™s time to script, Yay!

• Open up â€śSyncâ€ť

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

using UnityEngine;

This imports all the data from the precomplied dll. More info on using and creating dll : How to: Create and Use Assemblies Using the Command Line (C# and Visual Basic) | Microsoft Learn

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

This tells the class to inherit all of the classes from Photonâ€™s version of MonoBehavior.

• Generate some varibles!
//sync vals
Vector3 trueLoc;
Quaternion trueRot;
PhotonView pv;

This will hold the position sent across the network and the objects photonView.

• Now we need to cache the photonView for future use.
void Start(){
pv = GetComponent<PhotonView>();
}

This will get the PhotonView on the object this script is attached too.

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

public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
//we are reicieving data
{
//receive the next data from the stream and set it to the truLoc varible
if(!pv.isMIne){//do we own this photonView?????
this.trueLoc = (Vector3)stream.ReceiveNext(); //the stream send data types of "object" we must typecast the data into a Vector3 format
}
}
//we need to send our data
else
{
//send our posistion in the data stream
if(pv.isMine){
stream.SendNext(transform.position);
}
}
}

More info on typecasting : Casting and type conversions - C# | Microsoft Learn

• 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!
void Update()
{
if(!pv.isMine){
transform.position = Vector3.Lerp(transform.position, trueLoc, Time.deltaTime * 5);
transform.rotation = Quaternion.Lerp(transform.rotation, trueRot, Time.deltaTime * 5);
}

}

Ok lets break this code down:

void Update(){}

Update is called every frame, therefore we are going to update the position/rotation every frame.

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);

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

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

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!

7 Likes

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