PUN2 car game, synchronization issue

Hi, I make a car game using PUN2 and I encounter a problem with synchronization my car seems to lag on remote pc.
You can check that in my video on yt: https://youtu.be/i8gDE-IQV9Y

Here is my CarCotroller Script with synchronization.
public class CarControllerWithoutPhysic : MonoBehaviour, IPunObservable
{
    float inputX;
    int width;

    static float speed = 30.0f;
    float turnSpeed = 240.0f;
    Vector3 direction = new Vector3(0.0f, 0.0f, speed);

    Vector3 networkPosition;
    Quaternion networkRotation;

    PhotonView photonView;

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting)
        {
            stream.SendNext(transform.position);
            stream.SendNext(transform.rotation);
        }
        else
        {
            networkPosition = (Vector3)stream.ReceiveNext();
            networkRotation = (Quaternion)stream.ReceiveNext();

            transform.position = Vector3.Lerp(transform.position, networkPosition, 0.5f);
            transform.rotation = Quaternion.Lerp(transform.rotation, networkRotation, 0.5f);
        }
    }

    void Awake()
    {
        PhotonNetwork.SendRate = 45;
        PhotonNetwork.SerializationRate = 45;
        width = Screen.width / 2;
        photonView = GetComponent<PhotonView>();
    }

    void Start()
    {
        photonView = this.gameObject.GetComponent<PhotonView>();
        CameraFollow cameraFollow = this.gameObject.GetComponent<CameraFollow>();

        if (cameraFollow != null)
        {
            if (photonView.IsMine)
            {
                cameraFollow.OnStartFollowing();
            }
        }
        else
        {
            Debug.LogError("<Color=Red><a>Missing</a></Color> CameraWork Component on playerPrefab.", this);
        }
    }

    void Update()
    {

        if (photonView.IsMine)
        inputX = Input.GetAxis("Horizontal");

        if (photonView.IsMine)
        if (Input.touchCount == 1)
        {
            Touch touch = Input.GetTouch(0);

            if (touch.position.x > width)
                inputX = 1.0f;
            else
                inputX = -1.0f;         
        }

        transform.position = Vector3.Lerp(transform.position, transform.position + transform.TransformDirection(direction), 0.5f * Time.deltaTime);
      
        Vector3 euler = new Vector3(0.0f, inputX * turnSpeed, 0.0f);
        transform.Rotate(euler * Time.deltaTime);
  
    }
}

I have a ask to you how improve this, how can I get smoothly movement of my car on remote pc. I increased SendRate and SerializationRate to 45, also I write and read position and rotation in OnPhotonSerializeView function and with Vector3.Lerp I try to assign newPosition based on currentPosition of car and networkPosition that I recived.
Give some advice if you can. Thanks for help.

Comments

  • OneManArmyOneManArmy ✭✭✭
    Hi, why do lerp position twice?
    Move lerp of pos/rot from OnPhotonSerializeView to Update.

    Replace "0.5f" with "smooth * Time.deltaTime" and adjust smooth.
    Start with 5f and increase. Lower number = smoother, higher = more precise position/rotation.
    And of course lerp pos/rot in if (!photonView.IsMine).

  • OneManArmy wrote: »
    Hi, why do lerp position twice?
    Move lerp of pos/rot from OnPhotonSerializeView to Update.

    Replace "0.5f" with "smooth * Time.deltaTime" and adjust smooth.
    Start with 5f and increase. Lower number = smoother, higher = more precise position/rotation.
    And of course lerp pos/rot in if (!photonView.IsMine).

    OK thats help, thanks, now I have smooth movment but what's about accuracy of position synchronization. Is it a way to "predict" some way how car could move to get very close car position on remote pc bcs now I have a big diffrence.
  • OneManArmyOneManArmy ✭✭✭
    edited July 23
    what is your current smoothing value?
    and max planed players per room?
  • Max player per room it's 5 now and my smoothing value is 6f, if I increase over this value car seems laggy. Precision in that case is not thath good I want.
Sign In or Register to comment.