- Home /
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);
}
}
}
Answer by aldonaletto · Nov 05, 2011 at 10:22 PM
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
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 <= 0){ // if health gone, start DeathCamera coroutine
StartCoroutine(DeathCamera());
}
}
// this flag avoids start a new coroutine while DeathCamera is running
bool deathCam = false;
IEnumerator DeathCamera(){
if (deathCam) return; // does nothing if DeathCamera is already running
deathCam = true; // else set flag to indicate it's running now
for (i = 0; i < 100; i++){
// rotate and/or translate a little step
// based on Time.deltaTime and let Unity
// free till the next update with yield:
yield return 0;
}
// do the respawn thing here and
// make sure that champ.Health have
// been restored to avoid the loop
deathCam = false; // DeathCamera ended, clear the flag
}
} }
Your answer
Follow this Question
Related Questions
Loop a script ? 0 Answers
Over the Shoulder Camera Not Working 1 Answer
Making particles the only render on a camera? 1 Answer
my foreach loop and condition wont work 0 Answers
Crash on loop using Lerp. 1 Answer