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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
2
Question by Danao · Sep 21, 2014 at 01:25 AM · animationanimatortagbool

Triggering a Player Hit Animation

Hi. My hit/taking damage animation is not working. Here is the code I'm trying to use:

     void OnCollisionEnter2D(Collision2D collision)
     {
         if (collision.gameObject.tag == "Enemy") // If colliding with enemy, execute this block
         {
             HealthScript playerHealth = this.GetComponent<HealthScript> ();
             playerHealth.Damage (1);
             animator.SetTrigger ("Hit");
         }
     }
 }
                         
     
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Danao · Oct 13, 2014 at 03:29 PM

I realize there is a bit more here, but this is what ended up working for me (in case it can help anyone in the future):

  void OnCollisionEnter2D(Collision2D collision)
         {
             if (!invul && 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
                             StartCoroutine (InvulWait ());
             
             }
         }
         private IEnumerator InvulWait() {
             animator.SetTrigger ("Hit"); //play hit animation
             yield return new WaitForSeconds (2); //invul time
             invul = false;
             }

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
avatar image
1

Answer by hatake3 · Sep 21, 2014 at 02:24 AM

 bool playerHit = false;
 if (playerHit && collision.gameObject.tag == "Enemy"){
 animator.SetTrigger ("Hit");

You are setting playerHit to false, so the following if-statement will never execute.

Do you need the playerHit variable? If this script is on your player, then the fact that the OnCollisionEnter2D method is executing is enough to know that your player got "hit" by something. It seems counter-intuitive to set playerHit to false there. You should be fine by removing the

  bool playerHit = false;

line, and changing the if-statement to

 if (collision.gameObject.tag == "Enemy")
    animator.SetTrigger ("Hit");


Another simple fix is to just set 'playerHit' to true:

 bool playerHit = true;

It would make more sense to set it to true rather than false, but the variable is still redundant if you're not using it someplace else

UPDATE:

That can't be your entire method, because you're missing a couple of ending brackets. I'm going to assume this is how your code is structured (and I added comments):

 void OnCollisionEnter2D(Collision2D collision){
             bool damagePlayer = false;
 
             // Collision with enemy
             EnemyScript enemy = collision.gameObject.GetComponent<EnemyScript> ();
              /* You shouldn't do this, because if the gameobject you're colliding with doesn't have
              * the EnemyScript, this line will create a runtime exception. Since you're checking for
              * null in the following if-statement, I think it works, but it's not the best way to do it */
 
             if (enemy != null) {
 
                 // Damage the player
                  /* Your method runs line by line from top to bottom. Since you are setting damagePlayer
                  * to false at the start of the method (and you don't change it after that), the following
                  * if-statement will never be true, so that whole block will never execute */
                 if (damagePlayer) // This is always false {
                     HealthScript playerHealth = this.GetComponent<HealthScript> (); // This will never execute
 
                     if (playerHealth != null)    // Nor will this
                         playerHealth.Damage (1);// Nor that
                 }
                 
                 if (collision.gameObject.tag == "Enemy") {
                     animator.SetTrigger ("Hit");
                 }
             }
 }

Wont the following method suffice?

 void OnCollisionEnter2D(Collision2D collision)
         {
             if (collision.gameObject.tag == "Enemy") // If colliding with enemy, execute this block
             {
                 HealthScript playerHealth = this.GetComponent<HealthScript> ();
                 playerHealth.Damage (1);
                 animator.SetTrigger ("Hit");
             }
         }
Comment
Add comment · Show 6 · 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 21, 2014 at 03:58 AM 0
Share

I also have gone through the process from the beginning once again to see if there was any change, but I'm getting the same result.

avatar image hatake3 · Sep 21, 2014 at 08:59 AM 1
Share

Updated the answer :]

avatar image Danao · Sep 21, 2014 at 11:32 AM 0
Share

Thanks for the update hatake3. Sorry, I see I did miss the ending brackets in my original cut/paste. The health/damage here is from a tutorial and somehow it was working (at least to damage the player). Your code is much more efficient though and I replaced my code with yours. However, for some reason the animation still isn't playing despite it looking like everything should be in place! I'll edit my original post and replace the code as is now. Let me know if you have any other ideas of what to look at. Your help is greatly appreciated :)

avatar image hatake3 · Sep 21, 2014 at 12:27 PM 0
Share

Is it the pixelnest tutorial? :P Anyway, not sure why your animation is not working. From your original explanation, I couldn't see anything wrong with it... $$anonymous$$aybe upload a picture of the animator and the inspector?

avatar image hatake3 · Sep 21, 2014 at 03:13 PM 0
Share

Sorry, I can't see what's wrong :/

Show more comments

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

Double collision on "ground" tagged object results in player thinking they're in air? 1 Answer

2D Animation does not start 1 Answer

How can I setup my bool/trigger in animator? 1 Answer

Add a bool to animator & activate by key press 1 Answer

Blenderで作成したオブジェクトをUnityで正常に再生する方法 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