- Home /
Lerp: inconsistent movement(multiplayer related)
Hi, I'm currently making a multiplayer game where players can fire with cannons off a ship, while an other player controls it. The ships position is lerped on the clients from the current position to the next received position. When the players, which do not own the ship and therefore lerp the position of it, shoot with a cannon while the ship is moving, the cannonballs jitter back and fourth. I think the problem would also be noticed if an other ship(or an other object) with the same velocity moves alongside the ship. Thats propably because of dropped network packets and always changing distances Vector3.Lerp got to bypass. Does someone got a similiar problem or knows a solution to avoid this problem? It would be great if you could help me :).
Answer by JanWosnitza · Oct 16, 2012 at 05:06 PM
At first you should not lerp to the new position but use the new position + velocity to estimate the REAL current position (reduces lag and may also reduce your jitter problem). This will give a position which you could use with Vector3.SmoothDamp to calculate a smoothed display position:
EDIT:
Updates for a single ship should at least contain these values:
ship's ID (the way you need it)
ship's current position
ship's current velocity
current round time (= time since battle started)
The client who displays the ship should look similar like this:
///////////////////////////////
// Ship's pseudo declaration:
var networkPosition = Vector3.zero;
var networkVelocity = Vector3.zero;
var networkTime = Vector3.zero;
var velocity = Vector3.zero;
///////////////////////////////
// Position Update received
var networkUpdate = ...;
var ship = getShip( networkUpdate.ID );
ship.networkPosition = networkUpdate.position;
ship.networkVelocity = networkUpdate.velocity;
ship.networkTime = networkUpdate.time;
///////////////////////////////
// every frame (Update) for each ship
var ship = ...;
var estimatedPosition = ship.networkPosition + ship.networkVelocity * ( currentRoundTime - ship.networkTime );
ship.transform.position = Vector3.SmoothDamp( ship.transform.position, estimatedPosition, ref ship.velocity, 0.2f );
Hey Jan, thank you very much for your answer :). That is a similiar solution as I'm working on at the moment. There is one problem: After a longer play session, the positions propably get more and more apart. What to do in this case?
EDIT: Sorry I misread your answer. You use the new position + velocity ins$$anonymous$$d of the current position. Then the problem doesn't appear. I will try your solution, now :)
Good luck! :) Of course you have to multiply the velocity with the elapsed time:
estimatedPositon = networkPosition + networkVelocity * ( Time.time - receivedNetworkTime );
Or much better ins$$anonymous$$d of Time.time have a "roundTime" which should be the same on each client. "receviedNetworkTime" could then be sent with your position + velocity updates for more lag reduction
Hi Jan, I just tried your solution but it doesn't solve the problem, but made it a little better. I do have a kilometre per hour/miles per hour display on the screen that I think illustrates the problem. While on the screen of the player who controls the ship miles per hour is tacked at for example 35, on the screen of the remote players it goes higher and lower every second around 35. So sometimes its 36,34 oder even 40. And because of that constantly velocity change the objects that are not on the ship, like the cannonballs, "jitters". Do you know what else I could try? What do you mean with receviedNetworkTime? The time since the positon was send? Like: Network.time - info.timestamp?
EDIT: The higher the velocity of the ship, the more the problem appears. When I change the smoothTime to, lets say 0.4, the jitter is sometimes barely noticable. But every few seconds, miles per hour jumps from maybe 35 to even 50 for a second. Then the cannonball makes a huge "step". But I think setting smoothTime higher also leads to a "huge" position difference between the clients and the one player who controls the ship? Because I use photon cloud for the networking part, an authoriative server, where every player gets the data send from the server, is not an option for me. I think when the ping to the server is higher, packets more often get lost or send late, and then miles per hour can make such huge jumps from 35 to 40-50. When I host the server on my computer ins$$anonymous$$d, and don't use the cloud server, the ping is lower and it doesn't make such huge changes.
So in conclusion, I think I need a solution that let me set the smoothTime to maybe 0.5f or 0.6f but also prevent to let the ship position of the clients and the one who owns the ship diverse so much(around 10 units with 0.5). On another note, how would you do the same thing with rotation? Quaternion does not contain a method smoothDamp.
@receivedNetworkTime: exactly what you said, the "round time" on which the position + velocity update was sent.
Do you send ship's velocities over network, too? Or are you calculating it from the position updates?
I updated my post accordingly to this, hope it will clear it up. If your velocity is still "jittering" the most reasonable problem is that the updates over network are corrupted/desialized incorrect!
btw. may you could help him: http://answers.unity3d.com/questions/332399/photon-cloud-doesnt-work-correctly.html ;)
Your answer

Follow this Question
Related Questions
Physics + first person camera = jitter? 4 Answers
Camera jitter when using Lerp/Slerp 1 Answer
Lerping orthographic camera size jitters 1 Answer
GUI objects jitter when following "lerped" Gameobject [SOLVED] 1 Answer
Camera Lerp Jitter 0 Answers