Other players stuttering constantly

Options
I have been trying to sync player movement between clients but have been stuck with this issue: https://imgur.com/lZ9coeN
Players appear to have these big lag spikes pretty frequently: every 10 seconds or so. This happens even in different devices and internet connections.

Players move locally by changing their "Rigidbody.velocity" depending on joystick input. I tried using a lag compensation code that I found when looking for possible fixes, but as you can see in the gif the problem persisted:
private void SyncTransform() { // This is called in Update after player input

        if (photonView.IsMine) { return; }

        double timeToReachGoal = network_currentPacketTime - network_lastPacketTime;
        network_currentTime += Time.deltaTime;

        this.transform.position = Vector3.Lerp(network_positionAtLastPacket, network_position, (float)(network_currentTime/timeToReachGoal));
        this.transform.rotation = Quaternion.Lerp(network_rotationAtLastPacket, network_rotation, (float)(network_currentTime/timeToReachGoal));
        this.rb.velocity = Vector3.Lerp(network_velocityAtLastPacket, network_velocity, (float)(network_currentTime/timeToReachGoal));
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo message) {

        if (stream.IsWriting) {
            stream.SendNext(transform.position);
            stream.SendNext(transform.rotation);
            stream.SendNext(rb.velocity);
        }
        else {
            this.network_position = (Vector3)stream.ReceiveNext();
            this.network_rotation = (Quaternion)stream.ReceiveNext();
            this.network_velocity = (Vector3)stream.ReceiveNext();

            // Lag compensation
            network_currentTime = 0.0f;
            network_lastPacketTime = network_currentPacketTime;
            network_currentPacketTime = message.SentServerTime;
            network_positionAtLastPacket = transform.position;
            network_rotationAtLastPacket = transform.rotation;
            network_velocityAtLastPacket = rb.velocity;
        }
    }

Thanks in advance, guys.

Answers

  • jeffries7
    Options
    Have you tested using a CharacterController for movement instead of a rigid body?

    You might be getting conflicting results by adding velocity to the player as well as setting their position. Maybe try one or the other?

    Another option is not sending the players position across the network but sending their inputs. You'd then drive all non-local players as if they were local, essentially. You'd need to check to their positions to make sure they weren't out of sync or use predictions if data is lost.
  • frare
    Options
    Thanks a lot for the quick reply @jeffries7 ! Movement precision is not a big factor in this project, it's not a competitive game or anything like that, so I am just trying to prevent this visual stuttering and make the network experience look smoother.

    I tested using Rigidbody and without adding velocity, then using Character Controller, but in both cases players still lagged behind frequently like in the gif.
    When testing with 2 PC clients it doesn't look as bad but the game is meant to be played in Android and in those builds it looks terrible... players constantly stuttering and catching up.

    Also tried sending the input direction and moving the respective character locally with that direction vector but couldn't make it work properly, character was overshooting a lot. Could you give me some info in how I can achieve this? I am moving characters with CharacterController.SimpleMove().