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 Danao · Sep 27, 2014 at 02:24 AM · playerdamageyieldboolwait

How can I use a co-routine to yield player damage?

Hi, I am trying to use a co-routine to make my player invulnerable to damage for a few seconds after being hit. I tried the following code, but the player can still constantly take damage. Any suggestions would be great! Thanks!

I have my private bool invul = false; then...

 void OnCollisionEnter2D(Collision2D collision)
     {
                 if (collision.gameObject.tag == "Enemy") { // If colliding with enemy, execute this block
                         HealthScript playerHealth = this.GetComponent<HealthScript> (); //get player health script
                         playerHealth.Damage (1); //damage player
                         invul = true; //player is invulnerable
                         animator.SetTrigger ("Hit"); //play hit animation
                         StartCoroutine (InvulWait ());
                         }
         }
     private IEnumerator InvulWait() {
         yield return new WaitForSeconds (2); //invul time
         invul = false;
         }
         }
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
Best Answer

Answer by robertbu · Sep 27, 2014 at 02:39 AM

Assuming this is the only point where damage is given, you need to change line 3:

   if (!invul && collision.gameObject.tag == "Enemy") { // If colliding with enemy, execute this block
Comment
Add comment · Show 3 · 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
avatar image Danao · Sep 28, 2014 at 04:41 AM 0
Share

Thanks! That works perfectly. Now to figure out how to get my trigger to stop repeating during the invulnerable time. If I can't, I guess that would be best posted in a new question since I marked this one answered :)

avatar image robertbu · Sep 28, 2014 at 05:01 AM 0
Share

Actually the checkmark to the left of the question is used to mark a question as answered. As for your second issue, I don't do much with the animation system. Unless you did rewriting, the code I suggested avoids calling 'animator.SetTrigger()' on line 7, so I don't know what is causing your problem. Any chance your animation is looping?

avatar image Danao · Sep 28, 2014 at 07:10 AM 0
Share

Whoops, got the answer checked this time. I tried to move the 'animator.SetTrigger()' into the co-routine, but that didn't seem to help. I did double-check to make sure the animation isn't looping and the length of the animation is sufficient. The damage has yielded during the invulnerability time, but the trigger can be activated in rapid succession when the player collides with a moving enemy.

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

25 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

Related Questions

how to make a character flash red when damaged 1 Answer

Question on Player Damage 1 Answer

Problem with yield waitForSeconds in for loop 0 Answers

Yield to wait for delegate call 1 Answer

[Javascript] Creating waiting points for random movement with NavMesh Script 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