Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Unity 2022.2 is now available as the latest Tech release.
    Dismiss Notice
  3. We are making some changes to the DOTS forums.
    Dismiss Notice
  4. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Prediction & Smoothing

Discussion in 'Multiplayer' started by davej256, Nov 23, 2022.

  1. davej256

    davej256

    Joined:
    Nov 11, 2022
    Posts:
    8
    Hello. I have an authoritative server with a standard client that is predicting his own movements and rolling back as needed. All is working well so far, but it's jittery since the simulation rate is much lower than my monitor refresh rate. I have a single question about interpolation, so that I can smooth this out.

    When I spawn my player in, should I clone it twice? One player would be the simulation object. The other would be the interpolation object. The interpolation object would contain the camera, audio listener, etc and would (obviously) interpolate towards the simulation object to provide smooth movement at the monitor refresh rate.

    OR

    Would you use a single gameobject?
    Every simulation tick, the simulation would regain control over this object, move it as needed, and store the previous and current position/rotation. After this is done, the object goes back into interpolation mode, and interpolates between last/current simulation position for the next few frames, etc.

    There is really hardly any information about this specific part of netcode online. I've spent yesterday and today googling and watching youtube videos. They cover prediction, but not the local interpolation part. Please keep in mind that I am only talking about interpolating local predicted objects, not remote objects sent from the server.
     
  2. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    260
    This is quite a task that many struggle with. It's weird that it's jittery if the tick rate is LOWER than the refresh rate though.

    When the tick rate is lower than the refresh rate that means the likelihood of multiple ticks per frame are very low. When multiple ticks occur in a single frame interpolation could be a little trickier to manage, but not the other way around.

    Unfortunately I don't think I could describe how to resolve this issue in so few words because there's so much to consider. Is there a reason you're not using a framework that includes CSP?
     
  3. davej256

    davej256

    Joined:
    Nov 11, 2022
    Posts:
    8
    Sorry bit of a misunderstanding.
    I meant that it jitters when there's no interpolation; a natural consequence of running things at a fixed rate ;)

    All I'm asking is the correct way to do local interpolation when predicting. Either:

    A.) Use two gameobjects. One as a simulation body and one as an interpolation body. The simulation body is invisible and runs a character controller or rigidbody. Interpolation body contains the player camera and interpolates towards the sim body at the framerate.

    B.) Use a single gameobject as both. When the tick needs to be processed, enable the character controller, run the inputs on it. After the tick, disable the CC and switch it to interpolation mode and interpolate between last and current simulation positions for a few update frames until the simulation tick needs to be processed again. Repeat.
    By the time the simulation tick comes around, the lerp alpha will be >= 1 so the body should already be in the right spot for the simulation to be processed again.
    After a few hours of thinking, I think this (option B) is the right way to do it :). Please let me know if it's not.
     
    Last edited: Nov 23, 2022
  4. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    260
    Oh, okay. It's actually a lot easier than either of those.
    You just put the graphics on a child object, reset them to position before tick, and smooth to new position over duration of a tick (in update).
     
    davej256 likes this.