- Home /
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;
}
}
$$anonymous$$y suggestion : watch the Quill18 videos again (and again).
Rough guess : actually enable the standbyCamera after you find it.
Yeah, if I enable the standbyCamera, it works in $$anonymous$$ultiplayer mode, but once I try to go single player, it fails.
Well that is most strange, but there is nothing here that would explain why.
I taught myself Photon using Quill18s $$anonymous$$ultiplayer Space Shooter and his LD26 game. I watched this series to see if I could pick up anything new. Although Quills videos are usually awesome, this series is a bit messy due to lots of problems (Unity4 problems, mixing languages, some other problems I felt were there but won't mention so not to seem critical).
So that's why I suggested watching them more than once, to see what individual mistakes are being fixed, and to try and understand the Photon commands being used. If you understand the concept, then the mistakes become easier to understand and fix.
If you are just following along to make that project, then watch the videos more than once (watch the same video you're on twice before moving to the next), or simply download the project files to read and compare to yours.
In my projects I do a lot of things differently, the main one being : once a player object is created, it never gets destroyed until the end of the match (when returned to the lobby). This is easy to do, just have a boolean called isAlive. Then if the player isAlive, let all the RPC calls through. If not, then ignore them.
Secondly, cache the standby and player cameras ins$$anonymous$$d of finding them. Then when the player dies, set the player isAlive to false, disable the player camera and listener, and enable the standby camera and listener. For example (pseudocode in uJS) :
GA$$anonymous$$E $$anonymous$$ANAGER
var standbyCamera : Camera;
var standbyListener : AudioListener;
function EnableStandbyCamera( theBool : boolean )
{
standbyCamera.enabled = theBool;
standbyListener.enabled = theBool;
}
PLAYER $$anonymous$$ANAGER
function Die()
{
playerController.photonView.RPC( "PlayerDies", PhotonTargets.All );
// disable the player camera (I use a function just like in the game manager)
playerCamera.enabled = false;
playerListener.enabled = false;
// enable the standby camera
game$$anonymous$$anager.EnableStandbyCamera( true );
}
function Respawn()
{
playerController.photonView.RPC( "PlayerRespawns", PhotonTargets.All );
// enable the player camera
playerCamera.enabled = true;
playerListener.enabled = true;
// enable the standby camera
game$$anonymous$$anager.EnableStandbyCamera( false );
}
PLAYER CONTROLLER
@RPC
function PlayerDies()
{
isAlive = false;
transform.position = hiddenPosition;
}
@RPC
function PlayerRespawns()
{
isAlive = true;
transform.position = spawnPosition;
}
@RPC
function Shoot()
{
if ( !isAlive )
{
return;
}
// player is alive, do normal shoot stuff
}
Answer by GodsTheGuy · Dec 25, 2019 at 09:14 AM
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 :p
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Instantiate then access component(s) 2 Answers
Photon Network Muzzleflash 0 Answers
Why can't I view my grid? 1 Answer
Cursor GUITexture 1 Answer