Mirror Networking Moving platform jittering

Hello! I have a problem with rigidbody2d player and rigidbody2d platform moving. When there’s only one player on platform, it looks perfect but if there’s two players at one platform, then the second client (not host) sees how a platform and a player jitters and host only sees how second client is jittering. I went through a bunch of options but couldn’t solve this problem so I need help

You’re probably going to have to thoroughly explain how your jumping on platform system works, as well as what you’re using to sync this to clients. Just saying you have a problem doesn’t really give anyone a place to start as far as helping.

OnTriggerEnter2D and OnTriggerExit2D handle player’s activeShip variable which is used in PlayerMovement script to sum up ship velocity and player velocity

ShipMovement script snippet:

        [SyncVar] public Vector2 velocity;

        [Server]
        private void OnTriggerEnter2D(Collider2D collision)
        {
            if (collision.TryGetComponent(out PlayerMovement player))
            {
                player.activeShip = this;
                player.TargetSetActiveShip(gameObject);
            }
        }

        // When player leaves a platform
        [Server]
        private void OnTriggerExit2D(Collider2D collision)
        {
            if (collision.TryGetComponent(out PlayerMovement player))
            {
                if (player.activeShip == this)
                {
                    player.activeShip = null;
                    player.TargetSetActiveShip(null);
                }

            }
        }

        private void FixedUpdate()
        {
            MovePlatform(); // Server moves a platform
        }

        [Server]
        private void MovePlatform()
        {
            Rigidbody.velocity = velocity = direction * speed;
        }

PlayerMovement script snippet:

        public ShipMovementNew activeShip;

        [Client]
        private void FixedUpdate()
        {
            if (!hasAuthority) return;

            GetInput();
            Move();
        }

        [TargetRpc]
        public void TargetSetActiveShip(GameObject ship)
        {
            if (ship != null)
                activeShip = ship.GetComponent<ShipMovementNew>();
            else
                activeShip = null;
        }

        private void GetInput()
        {
            float horizontal = Input.GetAxisRaw("Horizontal");
            float vertical = Input.GetAxisRaw("Vertical");

            direction = new Vector2(horizontal, vertical).normalized;
        }

        private void Move()
        {
            Vector2 velocity = direction * speed;
            if (activeShip != null)
                velocity += activeShip.velocity;

            Rigidbody.velocity = velocity;
        }

Ship GameObject:

Player GameObject:

Is the “platform” and “ship” the same thing here? I’m assuming so.

So my guess as to the problem is this. In MovePlatform you are assigning to the SyncVar “velocity” while you are also assigning to the velocity of the platform. Your Player objects are using client authority, which should mean that movement is done locally on the client and then sent to the server/host, which then sends out that movement to all other clients. The movement of your player when on a platform is based on velocity SyncVar.

So you’ve got the server moving the platform, sending the velocity of the platform to the client, which then takes that velocity and applies it to the player, which then sends an update of the Player’s movement back to the server via NetworkTransform. None of that back and forth network communication is instantaneous, so you will see Player objects representing clients lagging behind the platform movement.

This shouldn’t be an issue for a Player object representing the host as viewed from the host itself, since communication within the host to itself actually is instantaneous.

I don’t know if NetworkTransform is applying any interpolation and how fast it is really sending updates. You’ve got it set to send updates pretty quickly, but if your Player’s movements aren’t more than 0.01 units then no movement update is sent. That might be causing some jitter.

So all in all, I see the issue really that your Player is supposed to be on the platform, but you’re actually moving them independently of each other, and you have entirely different computers in charge of their movement but trying to coordinate it so it looks smooth. I don’t think that can work.

You might want to consider when on a platform you instead have the Player set its position relative to the platform on both client and host instead of using NetworkTransform to sync their positions. You probably can just disable the NetworkTransform component. Send Commands and ClientRpcs for position updates relative to the platform but otherwise just every Update adjust the Player based on the current position of the platform. Then go back to using NetworkTransform when you leave the platform. That would probably allow for smooth movement for the players with the platform on all clients. (just my suggestion to investigate, as I haven’t actually done these moving platform mechanics in a network game)

1 Like