Lag Compensation

Options
Hello,
Something i dont understant in this Photon Documentation LagCompensation chapter


This part : Using OnPhotonSerializeView And FixedUpdate To Update The Object
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
    if (stream.IsWriting)
    {
        stream.SendNext(this.m_Body.position);
        stream.SendNext(this.m_Body.rotation);
        stream.SendNext(this.m_Body.velocity);
    }
    else
    {
        networkPosition = (Vector3) stream.ReceiveNext();
        networkRotation = (Quaternion) stream.ReceiveNext();
        rigidbody.velocity = (Vector3) stream.ReceiveNext();

        float lag = Mathf.Abs((float) (PhotonNetwork.Time - info.timestamp));
        networkPosition += (this.m_Body.velocity * lag);
    }
}

Whats means "this.m_Body" exactly ?
whats difference between "this.m_Body" and "rigidbody" here?
How i must to initialize "m_Body" ?
like that ?
if (photonView.IsMine)
        {
            m_Body = GetComponent<Rigidbody>();
        }

Thanks you for your help

Comments

  • Tobias
    Options
    You can find private Rigidbody2D m_Body in the PhotonRigidbody2DView and PhotonRigidbodyView. There, the initialization can be seen.

    I guess the difference is that earlier on, MonoBehaviour had a rigidbody property, which always did a GetComponent internally, so it makes sense to cache the result.

  • Jimanji
    Options
    Hum ok.. then i need to add a PhotonRigidbodyView for use this Lag Compensation Script (whitout change anything) ?
  • Jimanji
    Options
    Finally i have a good result with this change :

    private void Awake()
        {
            rb = GetComponent<Rigidbody>();     
                                                
            moveSpeed = 6;              
            rotationSpeed = 150;           
        }                                       
        public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
        {
            if (stream.IsWriting)
            {
                stream.SendNext(rb.position);
                stream.SendNext(rb.rotation);
                stream.SendNext(rb.velocity);
            }
            else
            {
                networkPosition = (Vector3)stream.ReceiveNext();
                networkRotation = (Quaternion)stream.ReceiveNext();
                rb.velocity = (Vector3)stream.ReceiveNext();
    
                float lag = Mathf.Abs((float)(PhotonNetwork.Time - info.SentServerTime)); // "info.timestamp" is obsolete 
                networkPosition += rb.velocity * lag;
            }
        }
        public void FixedUpdate()
        {
            if (!photonView.IsMine)
            {
                rb.position = Vector3.MoveTowards(rb.position, networkPosition, Time.fixedDeltaTime * moveSpeed);
                rb.rotation = Quaternion.RotateTowards(rb.rotation, networkRotation, Time.fixedDeltaTime * rotationSpeed);
            }
        }
    


    So two changement to the script on the documentation :

    1. Every ridibody are the same, i have change the "m_Body" and "rigidbody" for one only : "rb".

    2. But the script doesnt work , you forget to add in the documentation "* movespeed", at the before last line. Without "*movespeed" the ridibody move ridiculously slowly and very lately.