• 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
Question by kevindo92 · Oct 24, 2017 at 03:07 AM · characterrespawnhealthbar

create a respawn after character die

hi guys ! i need some help on how to create a respawn after the character die. So i already create a health script, i just want when the health = 0 then the character will die and you need to wait 5 second then respawn back to a way point that i created at empty object. here is health script: public class playerHealth : MonoBehaviour {

     public int HeroMaxHealth;
     public int currentHealth;
     
     public float flashSpeed = 5f;
     public Color flashColor = new Color(1f, 0f, 0f, 0.1f);
     public float timer;
     public float BloodGenerateTime ;
 
     public Text HealthText;
     public Slider healthSlider;
     public Image damageImage;
     Animator anim;
     player playerMovement;
     public bool isDead;
     bool damaged;
     // Use this for initialization
     void Awake () {
 
         //Setting up the references.
         anim = GetComponent<Animator>();
         playerMovement = GetComponent<player>();
         healthSlider.maxValue = HeroMaxHealth;
         healthSlider.value = HeroMaxHealth;
         currentHealth = HeroMaxHealth;
        
 
 
     }
     
     // Update is called once per frame
     void Update () {
         //If the player has just been damagedd....
         GetHealthText();
         timer += Time.deltaTime;
         if(damaged)
         {
             // set the colour of the damageImage to the flash colour
             damageImage.color = flashColor;
         }
         else
         {
             // transition the color back to clear.
             damageImage.color = Color.Lerp(damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
         }
         damaged = false;
 
         HealthGenerate();
 
 
     }
 
     public void TakeDamge(int amount)
     {
         //Set the damaged flag so the screen will flash.
         damaged = true;
 
         //Reduce the current health by the damage amount.
         currentHealth -= amount;
 
         //Set the health bar's value to the current health.
         healthSlider.value = currentHealth;
 
         //If the player has lost all it's health and the death flag hasn't been set yet...
         if (currentHealth <= 0 && !isDead)
         {
             Death();
         }
 
     }
 
     void Death()
     {
         //Set the death flag so this function won't be called again.
         isDead = true;
 
         //Tell the animator that the player is dead play die clip
         anim.SetTrigger("Die");
 
         //Turn off the movement 
         playerMovement.enabled = false;
 
     }
 
     void HealthGenerate()
     {
         if (currentHealth < HeroMaxHealth && !isDead && timer >= BloodGenerateTime)
         {
             currentHealth += 1;
             healthSlider.value = currentHealth;
             timer = 0;
         }
     }
 
     void GetHealthText()
     {
         HealthText.text = currentHealth.ToString() + "/"  + HeroMaxHealth.ToString(); 
     }
 }
Comment

People who like this

0 Show 0
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

Answer by jchester07 · Oct 24, 2017 at 05:24 AM

You could create a respawn method and call it from your death method. You can use a coroutine with WaitForSeconds(). Get a reference to your spawn point either by having a transform or gameobject variable and assign it in the inspector or call it by its tag.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

95 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 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 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 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 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

how do u make diffrent chacters and give them commands 2 do things 2 Answers

Enemy HealthBar Problems 2 Answers

Character don't die 2 Answers

How do I respawn my character after I die? 1 Answer

how to disable animator after load in from checkpoint 0 Answers


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