- Home /
PUN : Issue in playing over across Continents
Hello guys, I just started playing around with PUN like a few days ago and made a simple multiplayer tank shooting game , it works perfectly when I test it with my friends around my city , but when I asked my friend from US (I am from india) to give it a go it had problems , like only one of us could see other player , no matter how many times we restarted the game , and in the Editor I could see all three tanks just fine, is this a common issue or maybe I am doing something wrong ? (I am following the Unity Multiplayer FPS Tutorial Using Pun)
the weird thing is Missile/Bullet which is instantiated by the tank is visible to both the players and is syncing its position fine , just not the tanks.
I am just syncing the movement across using a lerp function as the documentation states. TankNetworkMover Script :
using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
using UnityEngine.UI;
public class TankNetworkMover : Photon.MonoBehaviour {
public delegate void Respawn(float time);
public event Respawn RespawnMe;
Vector3 position;
Quaternion rotation;
float smoothing = 10f;
[Range(2,100)]
public float minimumForce = 5;
[Range(20,300)]
public float maxForce = 50;
public float chargeSpeed = 2;
public float currentForce ;
public float minUpThrust = 50;
public float maxUpThrust = 300;
public float currentUpThrust ;
public GameObject missilePoint; //missile spawn point
public GameObject cam;
GameObject Power; //power UI bar
Image powerUp; // image attached to power
public float Health = 100;
public Image healthGameObject;
public float shootDelay = 1.5f;
bool canShoot = true;
GameObject shootButton;
void Start () {
Invoke ("FindHealthBar", .4f);
if (photonView.isMine) {
GetComponent<Rigidbody>().useGravity = true;
GetComponent<TankCharacterController> ().enabled = true;
gameObject.tag = "me";
Power = GameObject.FindGameObjectWithTag("power");
cam = GameObject.FindGameObjectWithTag("camera");
shootButton = GameObject.FindGameObjectWithTag("shoot");
cam.GetComponent<CameraControl> ().myTank = this.gameObject;
cam.transform.position = transform.position;
powerUp = Power.GetComponent<Image> ();
Invoke ("FindMissileSpawnPoint", .2f);
Invoke ("changeColorRed", .41f);
}
else{
StartCoroutine("UpdateData");
gameObject.tag = "other";
Invoke ("changeColorBlue", .41f);
}
}
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 ();
}
}
void Update()
{
if(CrossPlatformInputManager.GetButtonUp("Shoot") && canShoot && photonView.isMine)
{
GameObject missile = PhotonNetwork.Instantiate("Missile",missilePoint.transform.position,missilePoint.transform.rotation,0);
missile.GetComponent<Rigidbody> ().velocity = transform.forward * currentForce ;
missile.GetComponent<Rigidbody> ().AddForce(transform.up * currentUpThrust );
StartCoroutine (ShootDelay (shootDelay));
canShoot = false;
currentForce = minimumForce;
currentUpThrust = minUpThrust;
powerUp.fillAmount = 0;
}
if (CrossPlatformInputManager.GetButton ("Shoot") && canShoot && photonView.isMine) {
if(currentForce<maxForce)
currentForce ++;
if(currentUpThrust < maxUpThrust)
currentUpThrust += 5;
powerUp.fillAmount = Mathf.Lerp (0, 1, Mathf.InverseLerp (minimumForce, maxForce, currentForce)); // UI red power Slider
}
}
IEnumerator ShootDelay(float timer)
{
shootButton.GetComponent<Image> ().CrossFadeAlpha (.1f, 0, true);
shootButton.GetComponent<Image> ().CrossFadeAlpha (1, timer, true);
yield return new WaitForSeconds (timer);
canShoot = true;
}
void FindMissileSpawnPoint()
{
missilePoint = GameObject.FindGameObjectWithTag("missilePoint");
}
void FindHealthBar()
{
//healthGameObject = GameObject.FindGameObjectWithTag("health");
Image[] img = GetComponentsInChildren<Image>();
foreach(Image i in img)
{
if (i.type == Image.Type.Filled)
healthGameObject = i;
}
}
void changeColorRed( )
{
healthGameObject.color = Color.red;
}
void changeColorBlue( )
{
healthGameObject.color = Color.blue;
}
[PunRPC]
public void ApplyDamage(float damage)
{
Health -= damage;
if(Health<=0 && photonView.isMine)
{
if(RespawnMe != null)
RespawnMe(3f);
PhotonNetwork.Instantiate ("Explosion", transform.position, transform.rotation, 0);
PhotonNetwork.Destroy (gameObject);
}
healthGameObject.fillAmount = Health / 100;
}
//to do
}
MissileNetworkMover Script :
sing UnityEngine;
using System.Collections;
public class MissileNetworkMover : Photon.MonoBehaviour {
Vector3 position;
float smoothing = 10f;
// Use this for initialization
void Start () {
if (photonView.isMine) {
GetComponent<Missile>().enabled = true;
}
else{
StartCoroutine("UpdateData");
}
}
// Update is called once per frame
void Update () {
}
IEnumerator UpdateData()
{
while(true)
{
transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * smoothing);
yield return null;
}
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if(stream.isWriting)
{
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else
{
position = (Vector3)stream.ReceiveNext();
}
}
}
Network Manager Script :
using System.Collections;
using UnityEngine.UI;
public class NetworkSetup : MonoBehaviour {
public Text connectionText;
public Transform[] spawnPoints;
public Camera Cam;
GameObject player;
// Use this for initialization
void Start () {
PhotonNetwork.logLevel = PhotonLogLevel.Full;
PhotonNetwork.ConnectUsingSettings ("1.0");
}
// Update is called once per frame
void Update () {
connectionText.text = PhotonNetwork.connectionStateDetailed.ToString ();
}
void OnJoinedLobby()
{
RoomOptions ro = new RoomOptions (){isVisible = true, maxPlayers = 10};PhotonNetwork.JoinOrCreateRoom ("Mike", ro, TypedLobby.Default);
}
void OnJoinedRoom()
{
StartSpawnProcess (0f);
}
void StartSpawnProcess (float respawnTime)
{
StartCoroutine ("SpawnPlayer", respawnTime);
}
IEnumerator SpawnPlayer(float respawnTime)
{
yield return new WaitForSeconds(respawnTime);
int index = Random.Range (0, spawnPoints.Length);
player = PhotonNetwork.Instantiate ("Tank",
spawnPoints [index].position,
spawnPoints [index].rotation,
0);
player.GetComponent<TankNetworkMover> ().RespawnMe += StartSpawnProcess;
}
}
Thanks.
p.s Please excuse any stupid things in my code , I am not a programmer :p
Your answer
Follow this Question
Related Questions
Photon Pun 2 "using photon.pun" not working. 8 Answers
Photon RPC is not working in photon? 2 Answers