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 rOBY GAMES · May 17, 2015 at 09:02 AM · triggerenemyshot

Unity 2d count collisions ontriggerenter?

Hi.

I'm trying to write a script for my enemy. When shooting the enemy this script has to count when the bullet enters and leaves the box collider. I want to add a maximum of two shots that just fired at the enemy should make the death animation and then after 4 seconds is destroyed. Now it happens that firing only one shot the enemy is destroyed after 4 seconds, but does not do the animation death.

Some help?

script:

 using UnityEngine;
 using System.Collections;
 
 
 public class EnemyDead: MonoBehaviour {
 
 
     public float count = 2;
     
     public Animator Animator;
     
     void Start () {
         
         Animator = GetComponent<Animator> ();
         
     }
     
     
     void OnTriggerEnter2D(Collider2D col)
     {
         if(col.gameObject.tag == "HitEnemy");
             
         }
     
     void OnTriggerExit2D(Collider2D col)
     {
         count = -1;
         
         if(count > 0)
 
                 
             Animator.SetInteger ("AnimState", 3);
 
         
         Destroy(gameObject, 4);
         
         
     }
 
 }
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
0

Answer by barbe63 · May 17, 2015 at 10:59 AM

Many mistakes

count = -1; -> should be count -=1; or count --; or count = count-1; and you have to put brackets with your if because right now you destroy the object even if the condition is not fullfilled.

Like this:

          if(count == 0)
          {                
              Animator.SetInteger ("AnimState", 3);        
              Destroy(gameObject, 4);
          }

This part of your code:

      void OnTriggerEnter2D(Collider2D col)
      {
          if(col.gameObject.tag == "HitEnemy");
              
          }

is doing nothing. Replace all your code inside OnTriggerExit and put it in those brackets would be better like this: (remove the OnTriggerExit())

      void OnTriggerEnter2D(Collider2D col)
      {
          if(col.gameObject.tag == "HitEnemy")
          {
              count--;
              if(count == 0)
           {                
               Animator.SetInteger ("AnimState", 3);        
               Destroy(gameObject, 4);
           }
       }

You should also remove all the unnecessary spaces, somes are nice to separate blocks but too much is bad.

Edit: your condition is also bad. Replace it with if (count == 0), checking if it's >0 would'nt make any sense.

Too much mistakes in a small part of code... Review all your lessons ;)

Comment
Add comment · Show 9 · 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 rOBY GAMES · May 17, 2015 at 11:15 AM 0
Share

Hello Baber63 I'm glad to hear you again! :-)

Then I tried your code but it seems to work. When firing a shot, the death animation of the enemy does not work, and after 4 seconds to destroy the enemy. I do not know what the problem is, but it should work like this: After two shots that hit the enemy you should activate the animation death, the enemy falls to the ground and then destroyed after 4 seconds.

 using UnityEngine;
 using System.Collections;
 
 
 public class Enemy$$anonymous$$uore: $$anonymous$$onoBehaviour {
 
 
     public int count = 4;
     
     public Animator Animator;
     
     void Start () {
         
         Animator = GetComponent<Animator> ();
         
     }
     
     
     void OnTriggerEnter2D(Collider2D col)
     {
         if(col.gameObject.tag == "HitEnemy")
         {
             count--;
             if(count > 0)
             {                
                 Animator.SetInteger ("AnimState", 3);        
                 Destroy(gameObject, 4);
             }
         }
 
 }
 }    
avatar image rOBY GAMES · May 17, 2015 at 11:34 AM 0
Share

I also replaced:

 public float count = 2;

With

  public int count = 2;

But it does not change anything because one hit to kill the enemy.

avatar image barbe63 · May 17, 2015 at 11:49 AM 0
Share

I edited my post, the last mistake is "if(count > 0)" should be if(count==0)

avatar image barbe63 · May 17, 2015 at 11:50 AM 0
Share

Also try to not name variables as reserved space names like Animator. You should rename it animator (with lower case a)

avatar image rOBY GAMES · May 17, 2015 at 12:00 PM 0
Share

Now it works!

Except the animation after two rounds no soul. I put animator with a tiny.

modified script:

 using UnityEngine;
 using System.Collections;
 
 
 public class Enemy$$anonymous$$uore: $$anonymous$$onoBehaviour {
 
 
     public int count = 1;
     
     public Animator animator;
     
     void Start () {
         
         animator = GetComponent<Animator> ();
         
     }
     
     
     void OnTriggerEnter2D(Collider2D col)
     {
         if(col.gameObject.tag == "HitEnemy")
         {
             count -=1;
             if(count==0)
             {                
                 animator.SetInteger ("AnimState", 3);        
                 Destroy(gameObject, 4);
             }
         }
 
 }
 }    
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

20 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

Related Questions

Enemy trigger for player 1 Answer

How an Enemy could activate a Sound? 3 Answers

MAke an enemy attack and look at other enemies HELP? 1 Answer

Door open after destroying all the enemies. 1 Answer

Why is my character taking damage when attacking & why is it attacking multiple times per hit? 2 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