- Home /
[PUN] Movement Interpolation
Hello guys, I have a problem about the movement interpolation. I followed the Marco Polo tutorial, but when I moved my character and viewed from another PC, my character jerks continuously. The same with the other player's character when viewed in my PC.
This is the part of my coding of the character: (Yes, I got it from the tutorial, then slightly modified it)
private Vector3 correctPlayerPos;
private Quaternion correctPlayerRot;
void Update()
{
if (!photonView.isMine)
{
transform.position = Vector3.Lerp(transform.position, correctPlayerPos, Time.deltaTime * 5);
transform.rotation = Quaternion.Lerp(transform.rotation, correctPlayerRot, Time.deltaTime * 5);
}
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else
{
correctPlayerPos = (Vector3)stream.ReceiveNext();
correctPlayerRot = (Quaternion)stream.ReceiveNext();
}
}
I tried removing the Lerp function, by setting transform.position = correctPlayerPos. But it my character doesn't move from the other PC. After a few debugging, I found out that the two variables correctPlayerPos and correctPlayerRot doesn't update from the other PC. They just remain zero values.
So far, my only solution is to directly setting the transform.position and transform.rotation inside the OnPhotonSerializeView, which gives lag movement (because I use the default sendrate).
Any help would be appreciated. Thank you. :)
Answer by tobiass · Dec 02, 2014 at 09:57 AM
The description is a bit confusing. You say this code does not update correctPlayerPos and correctPlayerRot but if you set transform.rotation it will move your character? That doesn't fit together.
I would make extra sure you instantiate the prefabs when you are in the room and not before that. Also, make sure that you drag the prefab's script with OnPhotonSerializeView into the PhotonView "observe" field. Else, the PhotonView won't call your OnPhotonSerializeView. Make sure there are 2 players connected, or else PUN will skip sending updates (as the room is empty aside from the one client).
Hey thanks man! It worked :)
I just placed photonView.observed = this;
in the Start function, and it runs smoothly now!