Search Unity

Third Party 2D Platformer Lag Compensation Using Photon

Discussion in 'Multiplayer' started by markahughes88, Aug 30, 2020.

  1. markahughes88

    markahughes88

    Joined:
    Aug 24, 2020
    Posts:
    19
    Hi Guys.

    So I'm new to the whole Photon/Multiplayer/Networking side of things.

    I'm trying to implement some simple player movement at the moment, where you're able to move either left or right and jump.

    However, when it comes to sending the data to the server and back to clients there is quite a considerable lag. I know you need to account for this, so I tried implementing the suggestions from the Photon documentation (https://doc.photonengine.com/en-us/pun/v2/gameplay/lagcompensation), and I've tried a couple different youtube tutorials on the subject but I Just can't seem to find a solution that works.

    I think the movement along the x axis seems to be better since adding the NetworkPlayer script but still isn't perfect, but jumping just wont play ball at all.

    I've attached a video to show what is happening (Ignore the fact the gameobjects just fall straight over, as it's still just a basic player object and I'm just focusing on the lag issues as it stands).

    I'm running a PlayerMovement script that handles the movement and checks if the prefab is yours or not, and then im also running another script called NetworkPlayer than checks if it isn't yours and attempts to apply the solutions presented in the Photon documentation.

    I've not made any changes to anything in photon such as how many packets are sent and received etc.

    Any help on this would be great, as I've spent many hours at this point trying to sort it and feel like I'm banging my head against the wall o_O

    Video Link:


    PlayerMovement.cs
    Code (CSharp):
    1.  using Photon.Pun;
    2. using Photon.Realtime;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using System;
    7.  
    8. public class PlayerMovement : MonoBehaviourPun
    9. {
    10.     public float m_MoveSpeed = 3;
    11.     public float m_JumpHeight = 4.5f;
    12.     Rigidbody2D rb;
    13.  
    14.     public PlayerState m_State = PlayerState.ISGROUNDED;
    15.  
    16.     void Start()
    17.     {
    18.         rb = GetComponent<Rigidbody2D>();
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         if (rb == null)
    24.             return;
    25.  
    26.         if (photonView.IsMine)
    27.             TakeInput();
    28.     }
    29.  
    30.     private void TakeInput()
    31.     {
    32.         if (Input.GetKeyDown(KeyCode.Space) && (m_State == PlayerState.ISGROUNDED))
    33.         {
    34.             m_State = PlayerState.ISJUMPING;
    35.             rb.velocity = new Vector2(rb.velocity.x, m_JumpHeight);
    36.         }
    37.  
    38.         if (Input.GetKey(KeyCode.D))
    39.         {
    40.             rb.velocity = new Vector2(m_MoveSpeed, rb.velocity.y);
    41.         }
    42.  
    43.         if (Input.GetKey(KeyCode.A))
    44.         {
    45.             rb.velocity = new Vector2(-m_MoveSpeed, rb.velocity.y);
    46.         }
    47.     }
    48.  
    49.     void OnCollisionEnter2D(Collision2D theCollision)
    50.     {
    51.         Debug.Log("Back on the ground");
    52.         if (theCollision.gameObject.name == "Ground")
    53.         {
    54.             m_State = PlayerState.ISGROUNDED;
    55.         }
    56.     }
    57.  
    58.     public enum PlayerState
    59.     {
    60.         ISGROUNDED,
    61.         ISJUMPING
    62.     }
    63. }
    64.  
    NetworkPlayer.cs
    Code (CSharp):
    1. using Photon.Pun;
    2. using Photon.Realtime;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using System;
    7.  
    8. public class NetworkPlayer : MonoBehaviourPun
    9. {
    10.     public Vector3 m_NetworkPosition = Vector3.zero;
    11.     Rigidbody2D rb;
    12.  
    13.     void Start()
    14.     {
    15.         rb = GetComponent<Rigidbody2D>();
    16.     }
    17.    
    18.     public void Update()
    19.     {
    20.         if (!photonView.IsMine)
    21.         {
    22.             rb.position = Vector3.MoveTowards(rb.position, m_NetworkPosition, Time.fixedDeltaTime);
    23.         }
    24.     }
    25.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    26.     {
    27.         if (stream.IsWriting)
    28.         {
    29.             stream.SendNext(this.rb.position);
    30.             stream.SendNext(this.rb.velocity);
    31.         }
    32.         else
    33.         {
    34.             m_NetworkPosition = (Vector3) stream.ReceiveNext();
    35.             rb.velocity = (Vector3) stream.ReceiveNext();
    36.  
    37.             float lag = Mathf.Abs((float) (PhotonNetwork.Time - info.timestamp));
    38.             m_NetworkPosition.x += (this.rb.velocity.x * lag);
    39.             m_NetworkPosition.y += (this.rb.velocity.y * lag);
    40.         }
    41.     }
    42. }
    43.  
     
  2. DenisRiho1

    DenisRiho1

    Joined:
    Nov 27, 2020
    Posts:
    1
    Hello, I am having the same problem. I improved it little bit with simple lerping but It's not as good as I would want it to be. Did you found a better solution?