Hello! I am following a tutorial to make a multiplayer FPS game using Photon . I made the script to send my transforms over the network after Lerping them. However, it is still very laggy. Video. Is there anything wrong with the code? I tried copy pasting the sample code and still had the same problem. Thanks!
using UnityEngine;
using System.Collections;
using UnityStandardAssets.Characters.FirstPerson;
public class PlayerNetMover : Photon.MonoBehaviour {
[SerializeField] GameObject gun;
[SerializeField] GameObject gunBody;
[SerializeField] GameObject gunMag;
[SerializeField] GameObject gunBlock;
[SerializeField] Rigidbody gravity;
Vector3 position;
Quaternion rotation;
float smoothing = 1f;
float health = 100f;
public delegate void Respawn(float Time);
public event Respawn RespawnMe;
// Use this for initialization
void Start () {
if (photonView.isMine) {
gravity.useGravity = true;
GetComponent<FirstPersonController> ().enabled = true;
GetComponentInChildren<PlayerShooting> ().enabled = true;
GetComponentInChildren<PlayerShooting> ().enabled = true;
foreach (Camera cam in GetComponentsInChildren<Camera>()) {
cam.enabled = true;
}
gun.layer = 10;
gunBody.layer = 10;
gunMag.layer = 10;
gunBlock.layer = 10;
} else {
StartCoroutine("UpdateData");
}
}
IEnumerator UpdateData()
{
while(true)
{
transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * smoothing);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * smoothing);
yield return null;
}
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if(stream.isWriting)
{
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
stream.SendNext(health);
}
else
{
position = (Vector3)stream.ReceiveNext();
rotation = (Quaternion)stream.ReceiveNext();
health = (float)stream.ReceiveNext();
}
}
[PunRPC]
public void GetShot(float damage){
health -= damage;
if (health <= 0 && photonView.isMine) {
if(RespawnMe != null){
RespawnMe(3);
}
PhotonNetwork.Destroy(gameObject);
}
}
}