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 Wizardman290 · Nov 20, 2014 at 05:34 AM · c#respawn

With my respawn script, Player can't move for about 5 seconds.

(Tried asking this combined with another question, got declined by moderators because it is too basic apparently, so I am posting this one here, and I have searched on this, so don't decline it again please...)

The code I use to make the player die and respawn works nearly perfectly, except for the fact that after he respawns, he can only turn left and right (A & D keys), and he cannot walk backward or forward. This happens for about five seconds after the respawn, and after position is set to 0,0,0. Can anyone help me with this? Thanks in advance. (New to Unity/C#/Programming/Game Development)

Here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class AttributeDeclaration : MonoBehaviour {
     #region Public ints
     public int Health = 100;
     public int Magic = 100;
     public int Strength = 100;
     public int Stamina = 100;
     public int Defense = 100;
 
     //Maximum attributes
     public int MaxHealth = 100;
     public int MaxMagic = 100;
     public int MaxStrength = 100;
     public int MaxStamina = 100;
     public int MaxDefense = 100;
 
     //Testing Stuff
     public int DamageAmount = 20;
 
     //---------------------------------------------------------------------------------------------
     #endregion
 
     public PlayerLevelDeclaration PT;
     public PlayerMovement PM;
     public HealthPotion HP;
     public GUIs GU;
 
     //----------------------------------------------------------------------------------------
     void Awake(){
         newPos = transform.position;
         //newRot = transform.rotation;
     }
     //-------------------------
     // Use this for initialization
     void Start () {
     }
     //-----------------------------------------------------------------------------------------
     // Update is called once per frame
     void Update () {
         PT = gameObject.GetComponent<PlayerLevelDeclaration>();
         HP = gameObject.GetComponent<HealthPotion>();
         PM = gameObject.GetComponent<PlayerMovement>();
         GU = gameObject.GetComponent<GUIs>();
 
         TempDamageHealth();
         TempDamageStamina();
         TempDamageMagic();
         TempDamageStrength();
         TempDamageDefense();
 
         DisallowNegative();
         //KillPlayer();
     }
     //----------------------------------------------------------------------------------------
     void DisallowNegative(){
         if(Health <= 0)
         {
             Health = 0;
             StartCoroutine(KillPlayer());  // Or you can also use StartCoroutine("KillPlayer");
 
         }
         if(Health <= 0)
         {
             Health = 0;
         }
         if(Stamina <= 0)
         {
             Stamina = 0;
         }
         if(Magic <= 0)
         {
             Magic = 0;
         }
         if(Strength <= 0)
         {
             Strength = 0;
         }
         if(Defense <=0)
         {
             Defense = 0;
         }
     }
     //------------------------------------------------------------------------------------------
     //--------------------------------GUI Crap for player death---------------------------------
     //------------------------------------------------------------------------------------------
     void OnGUI(){
         ShowDeath();
         SorryMessage();
     }
 
 
     //-----------------------------------------------------------------------------------------
 
     void ShowDeath(){
         if(Health <= 0)
         {
             //LevelString = PT.PlayerLevel.ToString ();
             GUI.Box(new Rect(Screen.width / 2 - 250, Screen.height / 2 - 200, 500, 400), ("Oh dear! It appears you have died! Please wait 5 seconds to respawn"));
         }
     }
     //------------------------------------------------------------------------------------------
     void SorryMessage(){
         if(Health <= 0){
         GUI.Box(new Rect(Screen.width / 2 - 230, Screen.height / 2 - 170, 500, 370), ("Very sorry, may take a few extra seconds, working out some bugs."));
 
         }
     }
     //------------------------------------------------------------------------------------------
     //--------------------------------Temp Damage Attributes------------------------------------
     //------------------------------------------------------------------------------------------
     public int TempDamageHealth()
     {
 
         if(Input.GetKeyDown(KeyCode.Keypad1))
         {
             Health = Health - DamageAmount;
         }
         return Health;
     }
     //-------------------------------------------------------------------------------------
     public int TempDamageStamina()
     {
         
         if(Input.GetKeyDown(KeyCode.Keypad2))
         {
             Stamina = Stamina - DamageAmount;
         }
         return     Stamina;
     }
     //--------------------------------------------------------------------------------------
     public int TempDamageMagic()
     {
         
         if(Input.GetKeyDown(KeyCode.Keypad3))
         {
             Magic = Magic - DamageAmount;
         }
         return Magic;
     }
     //------------------------------------------------------------------------------------------
     public int TempDamageStrength()
     {
         
         if(Input.GetKeyDown(KeyCode.Keypad4))
         {
             Strength = Strength - DamageAmount;
         }
         return Strength;
     }
     //----------------------------------------------------------------------------------
     public int TempDamageDefense()
     {
         
         if(Input.GetKeyDown(KeyCode.Keypad9))
         {
             Defense = Defense - DamageAmount;
         }
         return Defense;
     }
 //--------------------------------------------------------------------
 
     Vector3 newPos;
     Vector3 newRot;
     IEnumerator KillPlayer(){
         if(Health <= 0)
         {
             Health = 0;
             //Hopefully this makes it so the player can't move as soon as Health = 0, but doesn't disappear and lose 
             //collision until after 2 seconds. Then, 5 seconds later, the player's position is set to 0, 0, 0, and 
             //He regains movement, reappears, and regains collision.
             
             //Kill Player
             PM.enabled = false;
             GU.enabled = false;
 
             yield return new WaitForSeconds(2f);
             renderer.enabled = false;
             collider.enabled = false;
             PM.enabled = false;
             
             //Wait 5 seconds
 
             yield return new WaitForSeconds(5f);
             PM.enabled = true;
 
             Vector3 PlayerSpawn = new Vector3(0f , 0f , 0f);
             if(transform.position != PlayerSpawn){
                 transform.position = PlayerSpawn;
             }
 
 
             renderer.enabled = true;
             collider.enabled = true;
             PM.enabled = true;
             GU.enabled = true;
             //Set position code;
 
             //Rest
 
             Health = MaxHealth;
         }
     }
 
 }
 
Comment
Add comment · Show 12
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 meat5000 ♦ · Nov 20, 2014 at 04:04 AM 0
Share
  //Wait 5 seconds
  
              yield return new WaitForSeconds(5f);
              P$$anonymous$$.enabled = true;

What's P$$anonymous$$? Does that enable your movement?

avatar image Wizardman290 · Nov 20, 2014 at 04:11 AM 0
Share

Yes, P$$anonymous$$ is my Player$$anonymous$$ovement script That enabled my movement, correct.

avatar image Wizardman290 · Nov 20, 2014 at 04:11 AM 0
Share

But I don't want the player to be able to move before they are shown again or "Respawn"

avatar image Sir-Irk · Nov 20, 2014 at 05:42 AM 1
Share

Did you make sure that you only have one instance of the $$anonymous$$illPlayer() coroutine running at a time like I said in your last question? If you call StartCoroutine in Update you have to be careful(I suggest using the technique in aldonaletto's answer on that page). I notice that $$anonymous$$illPlayer() sets health at 0 at the beginning. $$anonymous$$ake sure it isn't being started more than once at a time.

avatar image meat5000 ♦ · Nov 20, 2014 at 01:21 PM 1
Share

You have 2 yield WaitForSeconds in the same coroutine. You will get 2 pauses each time the routine is run.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Respawn Script Problem 1 Answer

How to undo destroying a component, after 5 seconds. 0 Answers

Distribute terrain in zones 3 Answers

Finding the player that walked into a ray? 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