Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TrivialRoo · Dec 22, 2013 at 04:08 AM · photonrespawn

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;
     }
 
 }
 
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Dec 22, 2013 at 01:08 PM 1
Share

$$anonymous$$y suggestion : watch the Quill18 videos again (and again).

Rough guess : actually enable the standbyCamera after you find it.

avatar image TrivialRoo · Dec 22, 2013 at 01:46 PM 0
Share

Yeah, if I enable the standbyCamera, it works in $$anonymous$$ultiplayer mode, but once I try to go single player, it fails.

avatar image AlucardJay · Dec 22, 2013 at 05:21 PM 0
Share

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
 }
avatar image TrivialRoo · Dec 23, 2013 at 01:26 PM 0
Share

Thank you!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges