Colyseus position schema "onChange" callback calls too rarely

Hi all, I am trying Colyseus extension. The problem I faced is when I change position schema in the back end, the front end onChnage callback calls only after 3-4 frames of actual game, so it not moves smoothly as if I will change position in cocos component update method. I can understand it took some time to bring data from back end, but how developers do this smooth movement in multiplayer games?

Hey @Horchynskyi,

As you observed, the frequency you receive updates from the server is going to be lower than your local update/frame rate. If you simply apply the new x/y position on the client-side, you will get a lagged feel.

A common approach to solve this is to apply linear interpolation. As you have more render frames than frames receiving data, those extra frames are used to “smooth out” the local position towards the latest position sent by the server.

There’s a guide for Phaser available here which you can adapt for Cocos Creator: Part 2: Linear Interpolation

Hope that helps! Cheers!

1 Like

Hi thanks for replay, I also found the method for the room is setPatchRate what by default is 50ms, I set it to 16.6 and now players run smoothly.
https://docs.colyseus.io/colyseus/server/room/#setpatchrate-milliseconds

And yes, linear interpolation also good, because if high ping will be, setPatchRate will not help.
I am using this method for interpolation:

    protected update(dt: number) {
        const { node, newPositionFromServer } = this;

            node.position = Vec3.lerp(
                node.position,
                node.position,
                newPositionFromServer,
                0.05
            );
    }

hope for someone it will help