x


Camera entering infinite loop at respawn

Hey guys,

I'm very new to Unity and thought I'd try my hand at creating my own game. At the moment, I'm writing a death camera that does a cool rotation and zoom out when one dies. When this is complete, it sends a signal to the player object to respawn. However, when it does respawn, the death camera keeps running and the object keeps respawning at the end of the animation. Unless my eyes are weary, all of my code looks fine and should not be causing this. I will post all of my code for you to look through (it IS a bit messy, but that is because I've been trying different formats for the inputs and testing whether this affects the outcome; it doesn't).

PlayerChampion.cs

using UnityEngine;
using System.Collections;

public class PlayerChampion : BaseChampion{

    PlayerChampion()
    {
       Health = 500;
       Mana = 100;
       HealthRegen = 5;
       ManaRegen = 3;
       AP = 0;
       AD = 50;
       Crit = 2;
       Lifesteal = 0;
       SpellVamp = 0;
       MovementSpeed = 300;
       AttackSpeed = 0.70;
    }

    PlayerChampion champ;
    DeathCamera deathCam;


    void Start () {
       GameObject newGO = GameObject.Find("First Person Controller");
       champ = (PlayerChampion)newGO.AddComponent("PlayerChampion");
       GameObject cam = GameObject.Find("Main Camera");
       deathCam = (DeathCamera)cam.GetComponent(typeof(DeathCamera));
       print("champ.Health = " + champ.Health);

    }

    void Update(){
       champ.Health = champ.Health - 1;
       //print("Health = " + champ.Health);
       champ.DeathCheck();
       if (champ.respawnOk == true)
       {
          if (deathCam.complete == true){
              champ.respawn();


          }
       }

    }

    void DeathCheck(){
         if (this.Health <= 0 && this.dead == false && this.deathChecked == false)
         {
          print("Passing Death");
          this.dead = true;
         }
         if (this.dead == true){
          print("Dead");
          GameObject playa = GameObject.Find("First Person Controller");
          FPSWalker mod = (FPSWalker)playa.GetComponent(typeof(FPSWalker));
          mod.speed = 0;
          this.MovementSpeed = 0;
          this.DeathAnimation();
          this.dead = false;
          this.deathChecked = true;

         }

    }

    void DeathAnimation(){
       deathCam.actv = true; 
       this.respawnOk = true;
    }

    void respawn(){

       deathCam.complete = false;
       deathCam.actv = false;
       champ.respawnOk = false;
       champ.dead = false;
       champ.Health = 500;
       champ.deathChecked = false;
       print("RespawnOk = " + champ.respawnOk);
       print("RespawnHealth = " + champ.Health);
       print("dead = " + champ.dead);
       print("deathcam.complete = " + deathCam.complete);
       print("deathCam.actv == " + deathCam.actv);
       GameObject.Find("First Person Controller").transform.position = new Vector3(250,70,250);
       Camera.main.transform.position = new Vector3(250,70,250);
       Camera.main.transform.localPosition = new Vector3(0,(float)0.9070835,0);
       GameObject playa = GameObject.Find("First Person Controller");
       FPSWalker mod = (FPSWalker)playa.GetComponent(typeof(FPSWalker));
       mod.speed = 20;


    }

    public int gold;
    public bool dead;
    public bool afflicted;
    public int MovementSpeed;
    bool deathChecked;
    bool respawnOk = false;

    // Update is called once per frame


    // Update is called once per frame

    }




**BaseChampion.cs**

    using UnityEngine;
using System.Collections;

public class BaseChampion : MonoBehaviour{

    public BaseChampion()
    {

    }



    // Update is called once per frame
    public int Health = 500;
    public int Mana = 100;
    public int HealthRegen = 5;
    public int ManaRegen = 3;
    public int AP = 0;
    public int AD = 50;
    public int Crit = 2;
    public int Lifesteal = 0;
    public int SpellVamp = 0;
    public double AttackSpeed = 0.70;

    }



**DeathCamera.cs**

    using UnityEngine;
using System.Collections;

public class DeathCamera : MonoBehaviour {

    float x;
    float tfx, tfy, tfz;
    int i = 0;  //used to increment camera translate
    bool shifted = false;

    public bool actv = false;
    public bool complete = false;
    bool shifteddone = false;
    GameObject mouseLookScript, mouseLookScript2;
    MouseLook mod, mod2;
    float k;


    // Use this for initialization
    void Start () {


    }

    // Update is called once per frame
    void Update () {

       if (i < 100 && actv == true)
       {

         if(i==0){
          complete = false;
          print("entering deathcam loop");
       x = Camera.main.transform.localEulerAngles.x;
       mouseLookScript = GameObject.Find("Main Camera");
       mouseLookScript2 = GameObject.Find("First Person Controller");
       mod = (MouseLook)mouseLookScript.GetComponent(typeof(MouseLook));
       mod2 = (MouseLook)mouseLookScript2.GetComponent(typeof(MouseLook));
       if (x > 90)
       {
          shifted = true;
       }
       }
         Camera.main.transform.Translate(0, (float)0.1, (float)-0.1);
         mod.enabled = false;        
         mod2.enabled = false;
         if (shifted == false && shifteddone == false){
         Camera.main.transform.Rotate(((90-x)/100),0,0);
         }
         if (shifted == true){
         Camera.main.transform.Rotate((450 - x)/100,0,0);
              if (Camera.main.transform.localEulerAngles.x < 90){
                 shifted = false;
                 shifteddone = true;
              }
         }
         if (shifted == false && shifteddone == true)
         {
          Camera.main.transform.Rotate(((90-Camera.main.transform.localEulerAngles.x)/(100-i)),0,0);
         }

         i++;        
       }
       else if (i ==100 && actv == true)
       {
         complete = true;
         mod.enabled = true;       
         mod2.enabled = true;
         actv = false;
         i = 0;
         print("i == " + i);
         print("Active = " + actv);

       }


    }
}
more ▼

asked Nov 04 '11 at 10:17 PM

jwonesh gravatar image

jwonesh
1 1 1

(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

Dude, this is really a complicated set of scripts - there are so many flags! I would try something simpler: check the player health at Update; if it becomes <= zero, set a flag and start a coroutine to do the rotation/zoom job; this coroutine would do the respawn job after the camera effects and clear the flag, thus enabling future deaths - all of this in a single script attached to the player, something like this pseudo code:

using UnityEngine;
using System.Collections;

public class PlayerChampion : BaseChampion{

    // do the initialization stuff like before
    // (you will not need most flags)

    // Check the health in Update and start DeathCamera if
    // player died - DeathCamera has provision to ignore other
    // calls while it's running
    void Update(){
       // why are you decrementing health each update? The player
       // will have a very short life this way!
       champ.Health = champ.Health - 1;
       if (Health 
more ▼

answered Nov 05 '11 at 10:22 PM

aldonaletto gravatar image

aldonaletto
41.2k 16 42 195

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2984
x294
x183
x157

asked: Nov 04 '11 at 10:17 PM

Seen: 438 times

Last Updated: Nov 05 '11 at 10:32 PM