Photon Unity Networking fix physics delay
Hi, I am making a simple soccer game that relies on physics. The game is two player and one of the clients is the server, and they send the ball's position to the other player. The problem is that on the client that isn't the server there is a delay between when the player hits the ball and when the ball moves. This is because when the player moves it is instantaneous but its position hasn't yet been updated by the client that is the server, which controls the ball's position. Any ideas on how to fix this? Here is my code that is being observed in a Photon View component on the player:
And this code is being observed in a Photon View component on the ball:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class playerMove : Photon.MonoBehaviour { Rigidbody rb; Vector3 targetPos; Quaternion targetRot; PhotonView PhotonView; void Start(){ PhotonView = GetComponent<PhotonView> (); rb = GetComponent<Rigidbody>(); } // Update is called once per frame void Update () { if (!PhotonView.isMine ) { smoothMove (); } } private void smoothMove(){ transform.position = Vector3.Lerp(transform.position, targetPos, 0.25f ); transform.rotation = Quaternion.RotateTowards (transform.rotation, targetRot, 500 * Time.deltaTime); } private void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){ if (stream.isWriting) { stream.SendNext (transform.position); stream.SendNext (transform.rotation); } else { targetPos = (Vector3)stream.ReceiveNext (); targetRot = (Quaternion)stream.ReceiveNext (); } } }
And this code is being observed in a Photon View component on the ball:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ballMoveNetworking : MonoBehaviour { [SerializeField] Rigidbody ballRB; private void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){ if (stream.isWriting) { //send ballRB pos and velocity stream.SendNext(ballRB.position); stream.SendNext(ballRB.velocity); } else { //update ballRB velocity and pos ballRB.position = (Vector3) stream.ReceiveNext(); ballRB.velocity = (Vector3) stream.ReceiveNext(); } } }Thanks in advance
0
Answers
-
I am by no means an expert, but have you tried increasing the send rate?0
-
Thanks for your input but there is still a small delay that occurs0
-
Hi @Samuel,
you can try to use Lag Compensation and see if this helps you getting mostly rid of the delay.0