Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by vikash91 · Apr 16, 2018 at 12:31 PM · delay

I had a player with three hearts in thee beginning. When he collides one heart is reduced,but the heart is to be recovered after waiting for 5 seconds automatically.

I want if player collides -

with health =3 ( health+=1 after 5 seconds ) ...........................(func1) with health= 2 two times* (health+=1 after 5 seconds) - each going one after another .........................................(func2)

with health =1 ( GAMEOVER )- THIS I HAD ALREADY DONE

I also need, that only one of the above function should run at a time.

Let me explain this - Suppose the player collides when health =3, so function 1 will start.

CASE 1---- However in the meantime, if again the bird collides (health =1) but function 2 must not start until function 1 is done. Although here, in this case, function 2 never starts as after completion of function1 health=2, so function1 will repeat again.

So at all, I want if the collision occurs, health should recover in 5 seconds and recover should not take place when a collision occurs with health=1, as after collision health =0 and GAMEOVER.

Here is the malfunctioned script ---

public class Bird : MonoBehaviour { public GameObject heart1, heart2, heart3; public int health = 3; public float upForce = 200f;

 private bool isDead = false;
 private Rigidbody2D rb2d;
 private Animator anim;
 private bool used = false;

 void Awake()
 {
     heart1.gameObject.SetActive(true);
     heart2.gameObject.SetActive(true);
     heart3.gameObject.SetActive(true);
 }

 // Use this for initialization
 void Start()
 {
     rb2d = GetComponent<Rigidbody2D>();
     anim = GetComponent<Animator>();
      

 }

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

     if (isDead == false)
     {
         if (Input.GetMouseButtonDown(0))
         {
             rb2d.velocity = Vector2.zero;
             rb2d.AddForce(new Vector2(0, upForce));
             anim.SetTrigger("Flap");
         }
     }
     if (health >= 3)
         health = 3;
     switch (health)
     {
         case 3:
             heart1.gameObject.SetActive(true);
             heart2.gameObject.SetActive(true);
             heart3.gameObject.SetActive(true);
             break;
         case 2:
             heart1.gameObject.SetActive(true);
             heart2.gameObject.SetActive(true);
             heart3.gameObject.SetActive(false);
             break;
         case 1:
             heart1.gameObject.SetActive(true);
             heart2.gameObject.SetActive(false);
             heart3.gameObject.SetActive(false);
             break;
         case 0:
             heart1.gameObject.SetActive(false);
             heart2.gameObject.SetActive(false);
             heart3.gameObject.SetActive(false);
             break;
     }
 }
 void OnCollisionEnter2D(Collision2D temp)
 {
     if(health>=3)
     {
         health -= 1;
         StartCoroutine(recover());
     }

     if (health == 2)
     {
         health -= 1;
         StartCoroutine(recover2());            
     }


     else if (health <= 1)
     {
         health -= 1;

         rb2d.velocity = Vector2.zero;
         isDead = true;
         anim.SetTrigger("Die");
         GameControl.instance.BirdDied();
     }
 }
 IEnumerator recover()
 {
     yield return new WaitForSeconds(4);
     health += 1;
 }
 IEnumerator recover2()
 {
     yield return new WaitForSeconds(4);
     health += 1;
     yield return new WaitForSeconds(4);
     health += 1;
 }
   
     
 }
Comment
Add comment · Show 1
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 vikash91 · Apr 16, 2018 at 12:05 PM 0
Share

Please help

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by vikash91 · Apr 17, 2018 at 02:32 AM

@Hellium

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

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

[Lots of Pictures] Idle to Run Animation Delay? When Pressing Forward Slides forward without Animation 0 Answers

Pause between action 1 Answer

Mining in RTS 0 Answers

Making button function wait for function before it to finish / Using WaitForSeconds. 0 Answers

my game freeze when arriving at a specific level at the first trigger 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