1

I have a server-reliant multiplayer Unity3D game, where the server is written in Node.js. The server sends updates to the positions of players, and the players lerp to those positions on the client to simulate smooth movement. I am not using Unity physics for player movement. I get around 20 packet updates per second, so I use the time between each update divided by the 0.12 seconds lerp interval.

The code I use to calculate the deltaTime for the lerping of player movement is:

lerpValue = Mathf.Clamp01((DateTime.UtcNow.Millisecond - lastPacket.UpdateTime) / 120;
myTransform.position = Vector3.Lerp (myTransform.position, lastPacket.Pos, lerpValue);

This seems to smooth out the movement of the players, but I cannot get the camera to consistently and smoothly follow the player. I use Time.deltaTime in LateUpdate for the camera smoothing. This makes it choppy, jerky, and stutter. I would also like the camera lerping a little bit behind the player, so if the player movement stutters the camera does not.

I have tried turning VSync on and off, moving player movement to FixedUpdate, and moving the camera from LateUpdate to FixedUpdate, and am looking for any more suggestions.

daniel metlitski
  • 747
  • 3
  • 11
  • 23
  • Try using a lerp % not based on the last update packet interval on the camera. e.g. `camera.pos = Lerp(camera.pos, player.pos, Time.deltaTime)`. It'll have some "bounce" to it as the player moves around (due to only moving 2% closer to the player every frame) it should be smooth. Adjusting the % upwards (e.g. `Time.deltaTime * 4`) would make it more responsive. As the multiplier approaches your frame rate, it'll get choppy again as it's effectively the same as you're doing now. Some value between the two extremes should be sufficient. Play with it and see if it works. – Draco18s no longer trusts SE Apr 25 '17 at 16:17

0 Answers0