- Home /
Question by
_samuel12345 · Jun 02, 2018 at 06:49 PM ·
physicsnetworkingnetworkballnetworkview
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:
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
Comment