Photon Respawning?

I am currently trying to figure out how to respawn a character after they have “died” with Photon. I have tried a variety of ways, and the camera always “freezes” after I run this code… The game does not encounter any errors, but the character is “destroyed”, so the camera is inactive and the game just freezes, owing to no camera.

Here is my code, please give suggestions!

using UnityEngine;
using System.Collections;

public class Health : MonoBehaviour {

	SpawnSpot[] spawnSpots;

	public float hitPoints = 100f;
	float currentHitPoints;

	GameObject standbyCamera;

	public int spawnTime = 3;

	// Use this for initialization
	void Start () {
		spawnSpots = FindObjectsOfType<SpawnSpot>();
		currentHitPoints = hitPoints;
	}

	[RPC]
	public void TakeDamage(float amt) {
		currentHitPoints -= amt;

		if(currentHitPoints <= 0) {
			Die();
		}
	}
	
	void Die() {
		if(GetComponent<PhotonView>().instantiationId == 0){
			Destroy(gameObject);
		}
		else {
			/*if(PhotonNetwork.isMasterClient) {
				PhotonNetwork.Destroy(gameObject);
			}
			else {
				Debug.Log ("Not MasterClient! You just wait patiently!");
			}*/

			/*if(PhotonNetwork.isMasterClient) {
				PhotonNetwork.Destroy (gameObject);
			}*/
			standbyCamera = GameObject.Find ("spectatorCamera");
			gameObject.transform.FindChild("Main Camera").gameObject.camera.enabled = false;

			//Respawn ();

		}
	}

	void Respawn() {
		SpawnSpot mySpawnSpot = spawnSpots[Random.Range(0, spawnSpots.Length)];

		GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate("PlayerController", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);

		//((MonoBehaviour)myPlayerGO.GetComponent("FPSInputController")).enabled = true;
		((MonoBehaviour)myPlayerGO.GetComponent("MouseLook")).enabled = true;
		((MonoBehaviour)myPlayerGO.GetComponent("PlayerMovement")).enabled = true;
		((MonoBehaviour)myPlayerGO.GetComponent("PlayerShooting")).enabled = true;
		//((MonoBehaviour)myPlayerGO.GetComponent("CharacterMotor")).enabled = true;
		myPlayerGO.transform.FindChild("Main Camera").gameObject.camera.enabled = true;
	}

}

Hello Trivialroo,
I’m not an expert in Photon but I think that the best way to prevent your camera from freezing is to set camera’s parent to another empty prefab, name “Camera Holder” created beforehand using Transform.SetParent

Also uniquely set the name of the camera, maybe the player Photon View ID or anything unique name corresponding to that player.

After that call Destroy()


When your character respawns, add a piece of code which finds the
Camera Holder Gameobject using Find() or Findwithtag()

then search the Child with name equal to that of player’s ID and at last, set it’s parent again as the player.


PS: I have custom camera controller which focuses the camera to its parent, so the camera will automatically move to the players transform. You can Lerp the Transform of Camera to your desired location by any way you want, it’s your choice.

Hope this helps, and sorry in advance if there is any error in the text :stuck_out_tongue: