- Home /
Photon Network Problems
Hello all, I am having a few issues with the photon system, I have just started messing around with this and I am following the Merry Fragmas tutorial, but I have ran into an issue. After a player dies and re-spawns, he re-spawns two instances, the second time he will spawn three instances, third is four and so on. Dont know where to even begin to find the issue, any help would be greatly appreciated, here are the scripts I am using, these are based off of the tutorial
PlayerNetworkMover
public delegate void Respawn(float time);
public event Respawn RespawnMe;
Vector3 position;
Quaternion rotation;
float smoothing = 10f;
float armor = 100f;
Animator anim;
float speed = 0.0f;
float strafe = 0.0f;
void Start () {
Weapon[] weapons = GetComponentsInChildren<Weapon>();
armor = GetComponent<Armor> ().armor;
anim = GetComponent<Animator>();
if(photonView.isMine)
{
gameObject.tag = "Player";
GetComponent<CharacterController>().enabled = true;
GetComponent<MechCharacterController>().enabled = true;
GetComponentInChildren<Finder>().enabled = true;
GetComponentInChildren<FollowTransform>().enabled = true;
GetComponentInChildren<Camera>().enabled = true;
GetComponentInChildren<AudioListener>().enabled = true;
}
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);
anim.SetFloat("Speed",speed);
anim.SetFloat("Strafe",strafe);
yield return null;
}
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if(stream.isWriting)
{
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
stream.SendNext(armor);
stream.SendNext(anim.GetFloat("Speed"));
stream.SendNext(anim.GetFloat("Strafe"));
}
else
{
position = (Vector3)stream.ReceiveNext();
rotation = (Quaternion)stream.ReceiveNext();
armor = (float)stream.ReceiveNext();
speed = (float)stream.ReceiveNext();
strafe= (float) stream.ReceiveNext();
}
}
[RPC]
public void GetShot(int damage)
{
armor -= damage;
if(armor <= 0 && photonView.isMine)
{
if(RespawnMe != null)
RespawnMe(3f);
PhotonNetwork.Destroy (gameObject);
}
}
and the NetworkManager
[SerializeField] Text connectionText;
[SerializeField] Transform[] spawnPoints;
[SerializeField] Camera sceneCamera;
[SerializeField] string playerName;
GameObject player;
void Start () {
PhotonNetwork.logLevel = PhotonLogLevel.Full;
PhotonNetwork.ConnectUsingSettings ("0.1");
}
void Update () {
connectionText.text = PhotonNetwork.connectionStateDetailed.ToString ();
}
void OnJoinedLobby()
{
RoomOptions ro = new RoomOptions (){isVisible = true, maxPlayers = 10};PhotonNetwork.JoinOrCreateRoom ("TestCell", ro, TypedLobby.Default);
}
void OnJoinedRoom()
{
StartSpawnProcess (0f);
}
void StartSpawnProcess (float respawnTime)
{
sceneCamera.enabled = true;
StartCoroutine ("SpawnPlayer", respawnTime);
}
IEnumerator SpawnPlayer(float respawnTime)
{
yield return new WaitForSeconds(respawnTime);
int index = Random.Range (0, spawnPoints.Length);
player = PhotonNetwork.Instantiate (playerName,
spawnPoints [index].position,
spawnPoints [index].rotation,
0);
player.GetComponent<PlayerNetworkMover> ().RespawnMe += StartSpawnProcess;
sceneCamera.enabled = false;
}
sorry for the huge amount of code, but as is stated before, I cannot figure out where the issue is
Answer by lordlycastle · Apr 16, 2015 at 03:39 AM
Posting all this code is useless, try to think; the error will definitely won’t be in start or update functions, or OnJoinRoom
. It only happens when player dies, so simply follow from the code that is triggered when a player dies, and all the functions it calls. This should decrease the code significantly.
You problem is in player.GetComponent ().RespawnMe += StartSpawnProcess;
This added number of times SpawnPlayer()
is called, from RespawnMe
event.
how do i go about fixing it? i thought it might be that line but was unsure, when i tried to change it, i cannot change the operator it has to be += or -= unless it is used with in the script witch it is not
GOT IT!!! thanks for your help. i changed the line to get the Network$$anonymous$$anager and started its StartSpawnProcess. thanks a ton i have been fighting this for two days and could not figure out what was wrong. Thank again!
Hello , How can I solve the same problem? pls link code please
Your answer
Follow this Question
Related Questions
Photon wont sync for the masterclient. 1 Answer
Is this bad for the network? 2 Answers
Unity3D Photon movement synchronization 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers