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 jwonesh · Nov 04, 2011 at 10:17 PM · cameralooprespawn

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);
         
         }
     
 
     }
 }

 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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
 }

} }

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

3 People are following this question.

avatar image avatar image avatar image

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


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