I’ve been experimenting with Unity’s networking, and I’ve got some “tanks” that can move around, turn, and swivel their child barrels. I got the networking to synchronize the state, and I started to implement very simple interpolation, but I’ve come across a bug that I don’t understand.
My interpolation is simply lerping and slerping using the average time between network updates, and it works pretty well for the position and rotation of the body of the tank, but for some reason the barrel child is still very choppy. Here’s the code:
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
if (stream.isWriting)
{
Vector3 pos = transform.position;
stream.Serialize(ref pos);
Quaternion rot = transform.rotation;
stream.Serialize(ref rot);
Quaternion swiv = barrelBase.transform.rotation;
stream.Serialize(ref swiv);
}
else
{
//get average delta
lastDelta = ++lastDelta % NUMDELTAS;
deltas[lastDelta] = Time.time - lastUpdate;
aveDelta = 0;
for (int i = 0; i < NUMDELTAS; i++)
{
aveDelta += deltas[i];
}
aveDelta /= NUMDELTAS;
//log this update's time
lastUpdate = Time.time;
//get next pos
Vector3 pos = Vector3.zero;
stream.Serialize(ref pos);
lastPos = transform.position;
nextPos = pos;
//get next rot
Quaternion rot = Quaternion.identity;
stream.Serialize(ref rot);
lastRot = transform.rotation;
nextRot = rot;
//get next swiv
Quaternion swiv = Quaternion.identity;
stream.Serialize(ref swiv);
lastSwiv = barrelBase.transform.rotation;
nextSwiv = swiv;
}
}
void Update()
{
//just messing around with two static tanks
if ((Network.isServer id == 0) || (Network.isClient id == 1))
{
handleInput();
}
if (Network.isClient)
{
float updateRatio = (Time.time - lastUpdate) / aveDelta;
transform.position = Vector3.Lerp(lastPos, nextPos, updateRatio);
transform.rotation = Quaternion.Slerp(lastRot, nextRot, updateRatio);
barrelBase.transform.rotation = Quaternion.Slerp(lastSwiv, nextSwiv, updateRatio);
}
}
Any ideas why the slerp on the child wouldn’t work? I’ve also noticed that the choppiness acts a bit different if I try to sync and lerp the angle of the only axis that I’m interested in.